How to Use Optional Chaining in Node.Js 12

How to use optional chaining in Node.js 12

Optional chaining is currently not supported in Node.js version 13 and below. It will be supported from Node.js version 14 and most of the browsers as it is moved to Stage 4. Currently, few platforms are supporting it. You can find the list of platforms supporting optional chaining in the given link. You can enable optional using --harmony flag.

Optional chaining in node v12.13.1

To use a variable property name with optional chaining, you have to use square brackets, just like normal dynamic property access.

let configTrait = overrideBids[traitType]?.[traitValue];

can't use optional chaining (?)

You have to enable this feature as it is not widely supported by applying the --harmony flag which applies the new ECMAScript 6 features.

According to node.green, optional chaining will be supported starting with Node 14, but will still require the --harmony flag.

Why is optional chaining not working in my Node REPL?

It's only coming for Node 14 under --harmony:
How to use optional chaining in Node.js 12

You can use babel optional chaining plugin if you want this feature in node:
https://babeljs.io/docs/en/next/babel-plugin-syntax-optional-chaining.html

Unexpected token . when using optional chaning ?. syntax when running mocha

Optional chaining is still behind a flag in Node.js v13. It isn't anymore in the latest Node.js (v14.9.0), though it still was in v14.4.0, so it got deflagged somewhere between those two.

Either update to the latest, or to enable it in v13 and earlier versions of v14:


node --harmony-optional-chaining ...


Related Topics



Leave a reply



Submit