The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.
The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null.
Example:
const customer = {
name: "Carl",
details: {
age: 82,
location: "Paradise Falls" // detailed address is unknown
}
};
const customerCity = customer.details?.address?.city;
// … this also works with optional chaining function call
const customerName = customer.name?.getName?.();
// method does not exist, customerName is undefined
Readmore: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining