Learning object 25 of 100
Nested object explorer
Follow a path through nested objects and arrays one step at a time, and see exactly where a plain chain would throw and what optional chaining returns.
Loading…
What it shows
Each . or [ ] step reads one level deeper. If any step produces undefined or null, the next step throws a TypeError. Reading the path step by step shows which level is missing; the optional chain ?. stops at that point and returns undefined.
Guided experiment
- Follow
customer.phone.mobile. Which step fails? - Follow
lines[3].name. Why does it fail at name and not at [3]? - Add a phone object to the data so the path works.
Predict
For const d = { a: {} }, what does d.a.b.c do?
Related
Lessons: Lesson 38: Nested objects and arrays, Lesson 40: Modeling real-world information
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-025.js in the companion repository.