Skip to main content
0 reactions

How to Read a Nested Object Property in JavaScript

Card titled Nested Properties in JavaScript, labelled JavaScript basics, with four ticked lines: order.customer.name gives Asha and order.customer.city gives Pune.

A property can hold another object. To read a value inside it, chain the dots: order.customer.city reads customer from order, then city from customer.

Every command and output on this page is real, run with Node.js 26.13.1 on macOS.

Syntax

SyntaxJavaScript
object.inner.property
  • object: the outer object.
  • inner: the property that holds the inner object.
  • property: the property to read from the inner object. Add another dot and name for each deeper level.

See Property accessors (opens in a new tab) on MDN.

Prerequisites

The Example

An order whose customer property is an object with a name and a city, in a file named order.js.

Step 1: Write the Code

order.jsJavaScript
const order = { customer: { name: 'Asha', city: 'Pune' } };

console.log(order.customer.name);
console.log(order.customer.city);

Step 2: Run It

TerminalBash
node order.js
OutputPlain text
Asha
Pune

Each line went one level in: first to the customer, then to the value.

Common Mistakes

MistakeWhat happensDo this instead
A name in the middle that doesn’t exist, such as order.shipping.cityThe program stops with TypeError: Cannot read properties of undefined (reading 'city')Check every name in the chain against the object

FAQ

Why does a missing middle name stop the program?

Because order.shipping is undefined, and undefined has no properties to read. A missing name at the end only gives undefined.

Conclusion

Chain dots to go one object deeper each time, and make sure every name before the last one exists.

Additional Resources

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

0 comments