⛓ Optional Chaining
Call object properties safely
What is the return value when calling a property that does not exist with optional chaining?
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