Read and update nested object data
Quick answer
Objects can contain other objects and arrays as property values. A chain of property accesses navigates one level at a time.
How it works
Use order.customer.name only when each intermediate object exists. Guard absent levels with explicit conditions. A shallow copy of an outer object retains nested object identities unless you also replace or copy those values.
const order = { customer: { name: "Mina" }, items: ["pen", "book"] };console.log(order.customer.name, order.items[1]);Expected output in Node.js:
Mina bookEach access selects a field or an array position.
const a = { detail: { count: 2 } };const b = Object.assign({}, a);b.detail.count = 9;console.log(a.detail.count, a === b, a.detail === b.detail);Expected output in Node.js:
9 false trueThe outer objects differ, but their nested detail is shared.
Common mistakes
Guard nullish intermediate values before reading a deeper key. Avoid mutating shared nested state by accident. Optional chaining is covered next chapter.
Practice
Create a nested address record and test a guarded lookup when the address is missing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read and update nested object data — Knowledge Check
Apply the taught concept and reason about the result.
Given const a={detail:{n:3}}, which reads 3?
For const a={items:["pen","book"]}, what is a.items[1]?
What is needed before reading an optional intermediate object in a plain access chain?
After const a={child:{n:1}}; const b=Object.assign({},a); b.child.n=8; what is a.child.n?
How can a shallow copy avoid modifying the original nested record when changing a nested field?