Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
In the following example, if no value is provided for b when multiply is called, b's value would be undefined when evaluating a * b and multiply would return NaN.
In the past, the general strategy for setting defaults was to test parameter values in the function body and assign a value if they are undefined. In the following example, b is set to 1 if multiply is called with only one argument:
With default parameters, checks in the function body are no longer necessary. Now, you can assign 1 as the default value for b in the function head:
Parameters are still set left-to-right, overwriting default parameters even if there are later parameters without defaults.
Note: The first default parameter and all parameters after it will not contribute to the function's length.
The default parameter initializers live in their own scope, which is a parent of the scope created for the function body.
This means that earlier parameters can be referred to in the initializers of later parameters. However, functions and variables declared in the function body cannot be referred to from default value parameter initializers; attempting to do so throws a run-time ReferenceError. This also includes var-declared variables in the function body.
For example, the following function will throw a ReferenceError when invoked, because the default parameter value does not have access to the child scope of the function body:
This function will print the value of the parameter a, because the variable var a is hoisted only to the top of the scope created for the function body, not the parent scope created for the parameter list, so its value is not visible to b.
The default parameter allows any expression, but you cannot use await or yield that would pause the evaluation of the default expression. The parameter must be initialized synchronously.
Note: Because the default parameter is evaluated when the function is called, not when the function is defined, the validity of the await and yield operators depends on the function itself, not its surrounding function. For example, if the current function is not async, await will be parsed as an identifier and follow normal identifier syntax rules, even when this function is nested in an async function.
In the second call in this example, even if the first argument is set explicitly to undefined (though not null or other falsy values), the value of the num argument is still the default.
The default argument is evaluated at call time. Unlike with Python (for example), a new object is created each time the function is called.
This even applies to functions and variables:
Parameters defined earlier (to the left) are available to later default parameters:
This functionality can be approximated like this, which demonstrates how many edge cases are handled:
You can use default value assignment with the destructuring syntax.
A common way of doing that is to set an empty object/array as the default value for the destructured parameter; for example: [x = 1, y = 2] = []. This makes it possible to pass nothing to the function and still have those values prefilled:
| ECMAScript® 2027 Language Specification # sec-function-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.