25 min·intermediate

Lesson 4

Nested data

Build a practical mental model for nested data and use it in working JavaScript.

By the end, you will be able to…

  • Explain nested data in your own words
  • Use nested data in a small program
  • Recognize and debug a common failure mode

Build the mental model

Real data is rarely flat — objects contain arrays of objects, which contain more objects. Reading nested data means chaining property and index access one level at a time.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: Pen

Why this matters

API responses are almost always deeply nested JSON; comfortably navigating that shape is a daily skill, not an edge case.

Real-world example

A GitHub API response nests owner info, repository arrays, and license objects several levels deep — exactly like this order example.

Common mistakes

  • Assuming a nested property always exists and crashing on undefined
  • Losing track of which level of nesting you're currently reading
  • Writing one giant expression instead of naming intermediate variables for clarity

Try it yourself

Challenge

Write totalItems(order) that sums the qty across all items.

Practice

Knowledge check

What is the safest way to read a property that might not exist on a nested object?

Summary

Navigate nested data one level at a time, and guard against missing intermediate values.