Default parameters (and default destructuring values) are pretty strong features, however, they only work if the value is otherwise not defined (either omitted, or is undefined
).
Most of the uses of default values never distinguish between null
and undefined
(or 0 and false, for that matter, but I think it does not make sense to add those to the semantics of default parameters).
I remember a V8 post that introduced default parameters and the even that author got the example wrong. It showed code that uses something like -
var foo = fooArg || someDefaultValue
Or -
if (foo === undefined || foo === null)
{
foo = someDefaultValue;
}
And suggested that it can be changed to use default parameters as equivalent code -
function (foo = someDefaultValue)
{
/*... */
}
Which obviously is not the case (it will not be the case in this proposal as well for other falsy values, but that makes more sense).
I propose the default parameters/values with an additional null check syntax - ?=
.
function callMe(argument1 ?= "string")
{
return argument1;
}
callMe(null); // Returns "string", not null
Likewise, it can be used in destructuring -
const object = {foo: null};
const {foo ?= "string"} = someObject;
foo; // "string", not null