0
0
Javascriptprogramming~5 mins

Nested objects in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a nested object in JavaScript?
A nested object is an object that is a value inside another object. It means one object contains another object as a property.
Click to reveal answer
beginner
How do you access a property inside a nested object?
You use dot notation or bracket notation for each level. For example, obj.innerObj.property accesses property inside innerObj which is inside obj.
Click to reveal answer
beginner
Given <code>const person = { name: 'Anna', address: { city: 'Paris', zip: 75000 } };</code>, how do you get the city?
You get the city by writing person.address.city. This goes inside the address object to get the city property.
Click to reveal answer
intermediate
Can nested objects contain arrays or other nested objects?
Yes! Nested objects can have arrays or even more objects inside them. This lets you build complex data structures.
Click to reveal answer
intermediate
How do you safely access a deeply nested property without errors if some parts might be missing?
You can use optional chaining like obj?.innerObj?.property. This stops and returns undefined if any part is missing, avoiding errors.
Click to reveal answer
What does the following code access?<br>user.profile.email
AThe user property inside the profile object of email
BThe profile property inside the email object of user
CThe email property inside the profile object of user
DThe profile property inside the user object called email
Which is the correct way to access the zip code in this object?<br>const data = { location: { zip: 12345 } };
Adata.zip
Bdata.location.zip
Cdata['zip']
Ddata.location['city']
What will obj?.inner?.value return if obj.inner is undefined?
AReturns false
BThrows an error
CReturns null
DReturns undefined
Can a nested object property be an array?
AYes, objects can contain arrays as properties
BNo, objects can only contain other objects
COnly if the array has objects inside
DOnly if the array is empty
How do you add a new nested object property to an existing object?
AUse dot notation to assign a new object, e.g., obj.newProp = {}
BYou cannot add nested properties after creation
CUse array methods
DUse JSON.stringify
Explain what a nested object is and how you access its properties in JavaScript.
Think about objects inside objects and how to reach inside.
You got /4 concepts.
    Describe how optional chaining helps when working with nested objects.
    It uses question marks to safely check each level.
    You got /4 concepts.