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

javascript - Nullish coalescing operator not working or not enabled? - Stack Overflow

programmeradmin1浏览0评论

I was looking into how to set a variable in javascript and I found out about the Nullish coalescing operator (??) in one of the posts I saw on here.

But now, when I try to use it, I get this error:

Unexpected token, expected ,

for this line:

var oldBookScript = (localStorage.getItem(book.id) ?? newBookScript);

I want the variable oldBookScript to have the value of localStorage.getItem(book.id) but if that is null or empty, I want it to have the value of newBookScript

Do I need to enable it somehow?

Thanks!

I was looking into how to set a variable in javascript and I found out about the Nullish coalescing operator (??) in one of the posts I saw on here.

But now, when I try to use it, I get this error:

Unexpected token, expected ,

for this line:

var oldBookScript = (localStorage.getItem(book.id) ?? newBookScript);

I want the variable oldBookScript to have the value of localStorage.getItem(book.id) but if that is null or empty, I want it to have the value of newBookScript

Do I need to enable it somehow?

Thanks!

Share Improve this question asked Jul 16, 2021 at 18:48 SkyeBoniwellSkyeBoniwell 7,14215 gold badges101 silver badges219 bronze badges 8
  • 1 is this a vanilla js project or are you using a js framework like react, vue, etc. – LudacrisX1 Commented Jul 16, 2021 at 18:50
  • 1 What browser are you using? Have you checked the patibility table? – Barmar Commented Jul 16, 2021 at 18:51
  • 1 That feature requires an up-to-date JavaScript runtime. How did you test your code? In a browser? In Node.js? – Pointy Commented Jul 16, 2021 at 18:51
  • 4 @SkyeBoniwell it has nothing to do with React, it's a JavaScript version thing. – Pointy Commented Jul 16, 2021 at 18:51
  • 1 are you sure the error is in this line? – Elias Soares Commented Jul 16, 2021 at 18:59
 |  Show 3 more ments

1 Answer 1

Reset to default 4

It is a syntactical construct, which means that in order to run code with it, you need to be in an environment that's up-to-date enough to support it.

For example, the most recent versions of Node, Chrome, and Firefox (among others) will all run fine with your code, but older environments (like IE11) will throw a SyntaxError.

In standard scripts, given that it's syntax, there's no way to "enable" it on a particular environment - either it's supported or it isn't. The usual approach in this situation is to use Babel to transpile the source code (which has all the modern syntax) down to ES6 or ES5 so that obsolete browsers can understand it.

But if you're using React, your code's JSX is already being transpiled. The most up-to-date versions of create-react-app does support the nullish coalescing operator. Upgrade to a more recent version of create-react-app, if possible. If you have a more manual setup, you'll need to make sure your code is being transpiled with Babel and use Babel 7.8.0 or later.

See these docs from Babel on integrating it into React via the @babel/preset-react preset.

发布评论

评论列表(0)

  1. 暂无评论