Chrome Extension

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
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Was this helpful?
0 Comments
Join Devsheet Ask a Question