I would like a JavaScript function to have optional arguments which I set a default on, which gets used if the value isn't defined. In Ruby you can do it like this:
def read_file(file, delete_after = false)
# code
end
Does this work in JavaScript?
function read_file(file, delete_after = false) {
// Code
}
Yeah this is referred to as a default parameter
Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.
Syntax:
Description:
Parameters of functions default to undefined However, in situations it might be useful to set a different default value. This is where default parameters can help.
In the past, the general strategy for setting defaults was to test parameter values in the body of the function and assign a value if they are undefined. If no value is provided in the call, its value would be undefined. You would have to set a conditional check to make sure the parameter is not undefined
With default parameters in ES2015, the check in the function body is no longer necessary. Now you can simply put a default value in the function head.
Example of the differences:
Different Syntax Examples:
Padding undefined vs other falsy values:
Even if the value is set explicitly when calling, the value of the num argument is the default one.
Evaluated at call time:
The default argument gets evaluated at call time, so unlike some other languages, a new object is created each time the function is called.
Default parameters are available to later default parameters:
Params already encountered are available to later default parameters
Functions defined inside function body:
Introduced in Gecko 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30). Functions declared in the function body cannot be referred inside default parameters and throw a ReferenceError (currently a TypeError in SpiderMonkey, see bug 1022967). Default parameters are always executed first, function declarations inside the function body evaluate afterwards.
Parameters without defaults after default parameters:
Prior to Gecko 26 (Firefox 26 / Thunderbird 26 / SeaMonkey 2.23 / Firefox OS 1.2), the following code resulted in a SyntaxError. This has been fixed in bug 777060 and works as expected in later versions. Parameters are still set left-to-right, overwriting default parameters even if there are later parameters without defaults.
Destructured paramet with default value assignment:
You can use default value assignment with the destructuring assignment notation
Default Parameter Values
With ES6, you can do perhaps one of the most common idioms in
JavaScript
relates to setting a default value for a function parameter. The way we’ve done this for years should look quite familiar:This pattern is most used, but is dangerous when we pass values like
Why? Because the
0 is falsy
, and so thex || 11 results in 11
, not the directly passed in 0. To fix this gotcha, some people will instead write the check more verbosely like this:we can now examine a nice helpful syntax added as of
ES6
to streamline the assignment of default values to missing arguments:x = 11
in a function declaration is more likex !== undefined ? x : 11
than the much more common idiomx || 11
Default Value Expressions
Function
default values can be more than just simple values like 31; they can be any valid expression, even afunction call
:As you can see, the default value expressions are lazily evaluated, meaning they’re only run if and when they’re needed — that is, when a parameter’s argument is omitted or is undefined.
A default value expression can even be an inline function expression call — commonly referred to as an Immediately Invoked Function Expression
(IIFE)
:If you are using
ES6+
you can set default parameters in the following manner:If you need
ES5
syntax you can do it in the following manner:In the above syntax the
OR
operator is used. TheOR
operator always returns the first value if this can be converted totrue
if not it returns the righthandside value. When the function is called with no corresponding argument the parameter variable (bar
in our example) is set toundefined
by the JS engine.undefined
Is then converted to false and thus does theOR
operator return the value 0.To anyone interested in having there code work in Microsoft Edge, do not use defaults in function parameters.
In that example Edge will throw an error "Expecting ')'"
To get around this use
As of Aug 08 2016 this is still an issue
In ECMAScript 6 you will actually be able to write exactly what you have:
This will set
delete_after
tofalse
if it s not present orundefined
. You can use ES6 features like this one today with transpilers such as Babel.See the MDN article for more information.