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?
Advertisement
Answer
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.