We need alpha 7 version from Babel.
npm install --save-dev [email protected]
npm install --save-dev babel-plugin-transform-optional-chaining@^7.0.0-alpha.13.1
Create a .babelrc file in the project root:
{
"plugins": ["transform-optional-chaining"]
}
Create an index.js to test the optional chaining operator.
var user = { name: 'Joe' }
var zip = user?.address?.zip
console.log(zip)
Then run it:
node_modules/.bin/babel-node index.js
Or transpile the code:
node_modules/.bin/babel index.js