Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use optional chaining in Node.js 12

Optional chaining (obj?.param1?.param2) seems to be a great feature and I really wanted to see it implemented and finally get rid of nested ifs, arbitrary functions and what not for such a simple operation.

But there's a problem, it doesn't work. I updated to Node 12 and I still get an error:

var dude = res?.param?.params[0]
SyntaxError: Unexpected token '.'

or

var dude = res.param?.params[0]
SyntaxError: Unexpected token '.'

What is the problem?

Do I need to change some language config or download a library to enable this feature? Or is it simply not out yet?

like image 215
almarc Avatar asked Jan 03 '20 06:01

almarc


People also ask

Does Nodejs 12 support optional chaining?

Optional chaining is now supported in node version v14. 2.0.

How do you add optional chaining?

To enable optional chaining, you need to install a package. At the time of writing, optional chaining is not natively supported in Javascript, it is a new feature introduced in ES2020. Until it is fully adopted we can get all the optional goodness by installing a package!

Can I use optional chaining JavaScript?

You can use optional chaining when attempting to call a method which may not exist.

When was optional chaining added to Nodejs?

Optional chaining was introduced in ES2020. According to TC39 it is currently at stage 4 of the proposal process and is prepared for inclusion in the final ECMAScript standard. This means that you can use it, but note that older browsers may still require polyfill usage.


4 Answers

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.

like image 154
Balaj Khan Avatar answered Oct 20 '22 03:10

Balaj Khan


The spec for the optional chaining feature was just promoted to Stage 4 (Finished) on December 22, 2019. Node 12 came out before the spec was final - and so did Node 13, for that matter.

According to node.green, optional chaining will be supported starting with Node 14, but will still require the --harmony flag. (This seems to conflict with Node's description of the --harmony flag - V8's shipping features aren't supposed to require the flag - so I'm not sure what to make of that.) Still, whether it needs a flag or not, I wouldn't expect to see the feature until the Node 14 release around April 2020.

If you want to play with optional chaining today, your best bet is to use TypeScript (which added optional chaining in version 3.7) or a preprocessor like Babel.

like image 28
Joe White Avatar answered Oct 20 '22 02:10

Joe White


I was able to use nodejs v13.7.0 with --harmony flag.

node --harmony myCode.js

Dinah

undefined

undefined

//myCode.js

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const catName = adventurer.cat?.name;
console.log(catName);
// expected output: Dinah
const dogName = adventurer.dog?.name;
console.log(dogName);
//expected output: undefined

console.log(adventurer.someNonExistentMethod?.())
//expected output: undefined
like image 16
user3283069 Avatar answered Oct 20 '22 03:10

user3283069


Optional Chaining will be implemented with Node.js v14, which will be released on 20/04/2020. By now, you may use Babel with @babel/plugin-proposal-optional-chaining.

like image 4
Chema Avatar answered Oct 20 '22 04:10

Chema