Default parameters
Sets fallback values for missing or undefined parameters.
function greet(name = 'there') { ... }This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Sets fallback values for missing or undefined parameters.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. Unlike the ES5 pattern using the logical OR operator (||), which triggers on any falsy value (0, '', false), default parameters specifically check for undefined. This prevents the 'falsy value bug' where passing 0 would accidentally trigger a default value of 1. Performance-wise, default parameters are evaluated at call time, meaning a new instance of an object or a new result of an expression is created for every invocation that relies on the default. Furthermore, parameters are evaluated from left to right, allowing later parameters to reference earlier ones, though referencing a parameter to its right results in a ReferenceError due to the Temporal Dead Zone.
Quick reference
Syntax
function greet(name = 'there') { ... }
See it in practice
Examples
Basic Parameter Initialization
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5, 2));
console.log(multiply(5));10 5
If the second argument is omitted or passed as undefined, the default value of 1 is used.
Referencing Previous Parameters
function calculateTotal(price, tax = price * 0.1) {
return price + tax;
}
console.log(calculateTotal(100));
console.log(calculateTotal(100, 20));110 120
Parameters defined earlier in the list are available to subsequent default expressions.
Dynamic Default Values via Functions
function getInitialValue() {
return 10;
}
function add(a, b = getInitialValue()) {
return a + b;
}
console.log(add(5));15
A function call can be used as a default value. It is executed only when the argument is missing.
Debug faster
Common Errors
LogicError
Cause: Passing null does not trigger the default parameter value because null is considered a defined object value in JavaScript.
Fix: Pass undefined instead of null if you want the default value to be used, or add a manual check within the function body.
function test(val = 'default') { return val; }
test(null); // returns null, not 'default'ReferenceError
Cause: Attempting to use a parameter as a default value before it has been initialized (forward-referencing).
Fix: Ensure that default expressions only reference parameters defined to their left.
function errorFunc(a = b, b = 5) { return a + b; }
errorFunc(); // ReferenceError: Cannot access 'b' before initializationRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Sets fallback values for missing or undefined parameters.
LogicError: Pass undefined instead of null if you want the default value to be used, or add a manual check within the function body. ReferenceError: Ensure that default expressions only reference parameters defined to their left.