A default value is a fallback assigned to a function parameter when its argument is omitted. It can make common API calls simpler while still letting callers supply a different value. section 5.4.2
JavaScript default parameters let you:
- Write the fallback directly in the parameter list instead of assigning it with an explicit `undefined` check. section 5.4.2
- Use values including objects, arrays, or functions; defaults are evaluated on each call, from left to right, and later defaults can refer to earlier parameters. section 5.4.2
- Keep defaults relatively simple: complex expressions that reference other parameters may make code harder to read. section 5.4.2
Listing 5.10 Using default parameters
function performAction(ninja, action = "skulking") { #A
return ninja + " " + action;
}
console.assert(performAction("Fuma") === "Fuma skulking", #B
"The default value is used for Fuma"); #B
#B
console.assert(performAction("Yoshi") === "Yoshi skulking", #B
"The default value is used for Yoshi"); #B
#B
console.assert(performAction("Hattori") === "Hattori skulking", #B
"The default value is used for Hattori"); #B
console.assert(performAction("Yagyu", "sneaking") === "Yagyu sneaking", #C
"Yagyu can do whatever he pleases, even sneak!"); #C
See omitted arguments use “skulking” while an explicitly supplied action overrides the default. section 5.4.2






















