Nested Objects

Objects inside objects.

Examples

Accessing Nested Object

Accessing deep properties.

const myObj = {
  name: "John",
  cars: {
    car1: "Ford",
    car2: "BMW"
  }
}
console.log(myObj.cars.car2);

Modifying Nested Object

Changing a value inside a nested object.

const myObj = {
  cars: {
    car1: "Ford"
  }
}
myObj.cars.car1 = "Tesla";
console.log(myObj.cars.car1);

Complex Nesting

Using bracket notation for nested access.

const person = {
    name: "John",
    address: {
        city: "New York",
        zip: 10001
    }
};
console.log(person["address"]["city"]);

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.