Lesson 7 — Optional chaining
Read without crashing
Reading a property of undefined (a value that is "not set") throws a TypeError and stops your program. The optional chaining operator ?. checks first: if the left side is null or undefined, the whole expression simply becomes undefined.
const order = {};
console.log(order.customer?.email); // undefined, no crash
// order.customer.email would throw
Why it matters: data from forms and servers is often incomplete; ?. lets you read it safely without a pile of if checks.
Want it another way?
Task
Fix the two log lines with ?. so the program prints undefined for Sam and Paris for Ann instead of crashing.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.