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
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
- Node.js, to run the example. Install it from the Node.js download page (opens in a new tab).
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
const order = { customer: { name: 'Asha', city: 'Pune' } };
console.log(order.customer.name);
console.log(order.customer.city);
Step 2: Run It
node order.js
Asha
Pune
Each line went one level in: first to the customer, then to the value.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A name in the middle that doesn’t exist, such as order.shipping.city | The 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
- Property accessors (opens in a new tab) (MDN)
- Working with objects (opens in a new tab) (MDN)
- Run Node.js scripts from the command line (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments