Reputation: 71
Error:
SyntaxError: Unexpected token '?'
Here is the code:
const result1 = (null || undefined) ?? 'OK';
console.log(result1); // 'OK'
It raises the unexpected token error when I run this program in Node.js
Upvotes: 6
Views: 27633
Reputation: 369584
The nullish coalescing operator was newly added in ECMAScript 2020, in June 2020 (about six months ago as of the date of the original question).
If you are using ECMAScript 2020 features in your code, make sure you use an ECMAScript implementation that fully implements ECMAScript 2020 (or at least the features you are using).
Upvotes: 7
Reputation: 1761
Assuming you are on Windows, download the LTS or current.
After this reboot your machine and make sure in your package json you do not specify an engine
that is less than 13 (optional)
{ "engines" : { "node" : ">14" } }
run your script
Upvotes: 2