Search code snippets, questions, articles...

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?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet