optional chaining babel code example

Example 1: optional chaining in javascript

let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});

let nameBar = myMap.get("bar")?.name;

Example 2: @babel/plugin-proposal-optional-chaining

npm install --save-dev @babel/plugin-proposal-optional-chaining
// Add it to the plugins array
require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-proposal-optional-chaining"]
});

Example 3: optional chaining

const array = [1,2,3,4,5];
let arrItem = array?.[4]; 

console.log(arrItem); /// 5