最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Unexpected token '?' - Stack Overflow

programmeradmin3浏览0评论

Error:

SyntaxError: Unexpected token '?'

Here is the code:

const result1 = (null || undefined) ?? 'OK'; 
console.log(result1); // 'OK'

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

Share Improve this question edited May 15, 2024 at 10:49 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Dec 21, 2020 at 16:56 sairaj225sairaj225 711 gold badge1 silver badge2 bronze badges 5
  • What exactly is the error? – Wais Kamal Commented Dec 21, 2020 at 16:58
  • 2 You're using too old a version. If my memory serves me correctly, it was added in node 14. – Aplet123 Commented Dec 21, 2020 at 16:58
  • 2 This was added in Node.js 14.0.0. What version are you running? – Barmar Commented Dec 21, 2020 at 16:58
  • 1 The first place you should always look when you get an error involving a new language feature is the patibility chart in MDN. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Barmar Commented Dec 21, 2020 at 16:59
  • 1 Code Snippets are executed in the browser of whoever reads the question. Trying to demonstrate failures that are particular to your specific environment by running code in the reader's environment doesn't make much sense. – Jörg W Mittag Commented Dec 21, 2020 at 17:03
Add a ment  | 

2 Answers 2

Reset to default 7

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

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

发布评论

评论列表(0)

  1. 暂无评论