Is there any shorter notation for the following test?
(typeof x != "undefined") ? x : y;
A kind of x || y
but that operates on undefined only (and not falsey values)
Something like ??
of C#
Is there any shorter notation for the following test?
(typeof x != "undefined") ? x : y;
A kind of x || y
but that operates on undefined only (and not falsey values)
Something like ??
of C#
In fact the question should be rephrase as is there a null coalescing operator in javascript ?
And the answer is No, not now but it should be coming soon. See proposal here and implementation status here.
If you are sure that undefined will never be overwritten, you can use something like x === undefined
. This will return either true
or false
which you can test against.