Default parameters
A parameter's value when no argument is passed.
Available since: ES2015
Syntax
function name(param = defaultValue) { … }Examples
A default value:
const greet = (name = 'Guest') => `Hello, ${name}`;
console.log(greet());
console.log(greet('Anna'));Output:
Hello, Guest
Hello, AnnaIt applies only for undefined:
const greet = (name = 'Guest') => `Hello, ${name}`;
console.log(greet(undefined));
console.log(greet(null));
console.log(greet(''));Output:
Hello, Guest
Hello, null
Hello, Notes
The default is evaluated on every call and can use earlier parameters: (a, b = a * 2) => ....
See also
- Rest parameters — Collects the remaining function arguments into an array.
- Destructuring assignment — Unpacks values from an array or object into separate variables.
- Nullish coalescing (??) — Returns the right-hand value if the left is null or undefined.
In the tutorial
- Parameters — Function parameters and arguments in JavaScript — several parameters, order, missing arguments and default values.