Khoa Nguyen
Pressure creates diamonds
Skip to main content

JavaScript Advanced Concept Optional chaining

Last updated: 10 May 2024 (8 month ago)

Optional Chaining

Optional chaining ? is a relatively new operator that was introduced in ES2020. It allows you to call object properties safely, without throwing an error. When calling properties without this operator, you many crash your applcation with the error Cannot read property ‘foo’ of undefined.

const person = { };

const dude = person.name;
console.log(foo); // Uncaught TypeError: Cannot read property 'bar' of undefined

const dude = person?.name; // undefined
  • E-commerce
  • Landing page
  • Usability
  • Accessibility
  • Information Architecture
  • Responsive Design