JavaScript Optional Chaining


const name = person == null ? undefined : person.name



// Simpler syntax with JavaScript Optional Chaining

const name = person?.name;

const street = person?.address?.street;

Optional Chaining Functions:


const windowCount = house.getWindowCount && house.getWindowCount();



const windowCount = house.getWindowCount?.();

Optional Chaining Arrays:


const firstElement = arr && arr[0];



const firstElement = arr?.[0];

Optional Chaining Objects:


const name = person && person['name'];



const name = person?.['name'];

Leave a Comment