javascript

Optional Chaining on Javascript Object

The optional chaining can be implemented using ?. It stops evaluation of expression after ? sign. It will return undefined if the expression retuns undefined before the ? sign.

const user = {
  admin: {
    name: "John Deo"
  }
};
user.admin?.name;
// "John Deo"
user.admi?.name;
// undefined
user.admi.name
// TypeError: Cannot read property 'name' of undefined
Was this helpful?