3 ms·
You are correct, except this will not work in the significant case where any falsy value is passed ("", 0, null, NaN... I exclude undefined because that is the
by STRML 14y ago
You are correct, except this will not work in the significant case where any falsy value is passed ("", 0, null, NaN... I exclude undefined because that is the intended behavior) or with booleans.
A slightly more verbose, yet hardy syntax would go like this:
function foo(arg1, arg2){
arg1 = arg1 === undefined ? "foo" : arg1;
arg2 = arg2 === undefined ? "bar" : arg2;
}
This would catch the intended behavior, which is to fill a parameter when it is not supplied at all.
- leeoniya 14y agoalready was updating :)