Javascript Function Default Parameter Values

Introduction

Javascript 6 supports default parameter value.

function display(name = 'CSS') {
    return `Input is ${name}`;
}

console.log(display('HTML')); 
console.log(display()); //  www .ja v a2s  . c o  m

Multiple independent default variables:

function display(name = 'CSS', numerals = '123') {
    return `Input ${name} has ${numerals}`;
}

console.log(display()); /*from  www.  j  ava2  s .  c  om*/
console.log(display('HTML')); 
console.log(display(undefined, '321')); 

Arrow functions with default parameters.

let display = (name = 'CSS') => `Input ${name}`;

console.log(display()); //from  w w  w.j a  v  a  2  s. c o  m



PreviousNext

Related