Can I declare default parameter like
function myFunc( a, b=0)
{
// b is my optional parameter
}
in JavaScript?
Can I declare default parameter like
function myFunc( a, b=0)
{
// b is my optional parameter
}
in JavaScript?
With ES6: This is now part of the language:
function myFunc(a, b = 0) {
// function body
}
Please keep in mind that ES6 checks the values against undefined
and not against truthy-ness (so only real undefined values get the default value - falsy values like null will not default).
With ES5:
function myFunc(a,b) {
b = b || 0;
// b will be set either to b or to 0.
}
This works as long as all values you explicitly pass in are truthy.
Values that are not truthy as per MiniGod's comment: null, undefined, 0, false, ''
It's pretty common to see JavaScript libraries to do a bunch of checks on optional inputs before the function actually starts.
if (typeof b === 'undefined') b = 0;
–
Skees b
into the global namespace when b is omitted? –
Orientalize b = b || 'foo'
. –
Ankh null, undefined, 0, false, '', NaN
WONT get the default value in ES6. Only undefined would. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… –
Erlond if(typeof b==='undefined')
work the same as if (b===undefined)
? –
Tseng typeof b==='undefined'
is generally more robust than b===undefined
. b===undefined
will not work as expected if undefined
is redefined - this should never be done but it is possible to do in some browsers. And while it's not relevant to this example because we're declaring b
in our function definition, in the general case typeof b==='undefined'
will also work without throwing an error in the event that b
were not defined. See this thread for more details: #4726103 –
Aphelion With ES6, this is possible in exactly the manner you have described; a detailed description can be found in the documentation.
Default parameters in JavaScript can be implemented in mainly two ways:
function myfunc(a, b)
{
// use this if you specifically want to know if b was passed
if (b === undefined) {
// b was not passed
}
// use this if you know that a truthy value comparison will be enough
if (b) {
// b was passed and has truthy value
} else {
// b was not passed or has falsy value
}
// use this to set b to a default value (using truthy comparison)
b = b || "default value";
}
The expression b || "default value"
evaluates the value AND existence of b
and returns the value of "default value"
if b
either doesn't exist or is falsy.
Alternative declaration:
function myfunc(a)
{
var b;
// use this to determine whether b was passed or not
if (arguments.length == 1) {
// b was not passed
} else {
b = arguments[1]; // take second argument
}
}
The special "array" arguments
is available inside the function; it contains all the arguments, starting from index 0
to N - 1
(where N
is the number of arguments passed).
This is typically used to support an unknown number of optional parameters (of the same type); however, stating the expected arguments is preferred!
Although undefined
is not writable since ES5, some browsers are known to not enforce this. There are two alternatives you could use if you're worried about this:
b === void 0;
typeof b === 'undefined'; // also works for undeclared variables
© 2022 - 2024 — McMap. All rights reserved.
null, undefined, 0, false, '', NaN
will all get the default value. – Skees