Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected token '?'

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

like image 819
sairaj225 Avatar asked Jul 27 '26 08:07

sairaj225


2 Answers

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).

like image 73
Jörg W Mittag Avatar answered Jul 28 '26 21:07

Jörg W Mittag


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

like image 34
rags2riches-prog Avatar answered Jul 28 '26 21:07

rags2riches-prog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!