Path: blob/main/files/en-us/web/javascript/reference/functions/default_parameters/index.md
6520 views
------{{jsSidebar("Functions")}}
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
{{EmbedInteractiveExample("pages/js/functions-default.html")}}
Syntax
Description
In JavaScript, function parameters default to {{jsxref("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 {{jsxref("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.
Examples
Passing undefined vs. other falsy values
In the second call in this example, even if the first argument is set explicitly to undefined (though not null or other {{glossary("falsy")}} values), the value of the num argument is still the default.
Evaluated at call time
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:
Earlier parameters are available to later default parameters
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:
Destructured parameter with default value assignment
You can use default value assignment with the destructuring assignment syntax.
A common way of doing that is to set an empty object/array as the default value 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:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}