Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  22] [ 3]  / answers: 1 / hits: 8647  / 4 Years ago, sat, september 19, 2020, 12:00:00

I literally copy pasted example code from MDN but optional chaining wont work in my node(v12.13.0) REPL. Throws out a syntax error saying that the dot after the question mark is invalid. What's going on ? I have already used this expression in a React App and it seems to be working fine.


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


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

console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined


More From » node.js

 Answers
2

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


[#2652] Tuesday, September 15, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
erinh

Total Points: 38
Total Questions: 100
Total Answers: 110

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;