Uncaught SyntaxError: Unexpected token :
Updated answer
The nullish-coalescing operator was added in ECMAScript 11.
The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is
null
orundefined
, and otherwise returns its left-hand side operand.
let x = undefined;
console.log(x ?? 'default');
x = null;
console.log(x ?? 'default');
Original answer
Currently, there is no ??
operator in javascript.
You can use the ternary operator:
let x = undefined;
console.log(x == undefined ? 'default' : x);
which will print 'default'
if x
is undefined, otherwise, it will print the value of x
.
However, you've found the proposal for the nullish coalescing operator. This is currently a proposal in stage 3, but it looks like it will make it into a future version of the ECMAScript.
As mentioned in @Uncle's comment, that feature is only a proposal at this point and is not currently implemented in Javascript. To use it before it is implemented in modern browsers, you'll need to add this Babel plugin: @babel-plugin-proposal-nullish-coalescing-operator.