Optional Chaining with the ?. Operator
Safely access deeply nested properties.
The Problem
Reading a property on something that is undefined or null throws a TypeError. Optional chaining lets you read nested values safely.
const user = {};
console.log(user.profile && user.profile.name);The ?. Operator
The optional chaining operator ?. stops and returns undefined if the value to its left is null or undefined, instead of throwing.
const user = {};
console.log(user.profile?.name); // undefined, no errorAll lessons in this course
- Optional Chaining with the ?. Operator
- Nullish Coalescing with ??
- Combining ?. and ??
- Optional Calls and Index Access