Is there a universal JavaScript function that checks that a variable has a value and ensures that it\'s not undefined
or null
? I\'ve got this code,
Take a look at the new ECMAScript Nullish coalescing operator
You can think of this feature - the ??
operator - as a way to “fall back” to a default value when dealing with null
or undefined
.
let x = foo ?? bar();
Again, the above code is equivalent to the following.
let x = (foo !== null && foo !== undefined) ? foo : bar();