Classic JS Tutorial | JS in VS 2026 | JS Examples | jQuery
⚡ Lesson 08 of 30

Objects

Work with JavaScript objects — creation, access, destructuring, spread, and common patterns.

Creating Objects

Object literals are the most common way to create key-value stores in JavaScript:

const person = {
  name: "Alice",
  age: 30,
  city: "Kuala Lumpur",
};
console.log(person.name);     // "Alice"
console.log(person["age"]);   // 30

Adding, Updating, Deleting

const car = { make: "Toyota", model: "Camry" };
car.year = 2026;         // add property
car.model = "Corolla";   // update property
delete car.year;         // remove property
console.log(car); // { make:"Toyota", model:"Corolla" }

Destructuring

Extract properties into variables with a clean syntax:

const { name, age, city = "Unknown" } = person;
console.log(name); // "Alice"
console.log(city); // "Kuala Lumpur"

// Rename during destructuring
const { name: fullName } = person;
console.log(fullName); // "Alice"

Spread & Object.assign

Copy or merge objects without mutation:

const defaults = { theme: "dark", fontSize: 14 };
const userPrefs = { fontSize: 18 };

const settings = { ...defaults, ...userPrefs };
// { theme:"dark", fontSize:18 }

Iterating Over Objects

const scores = { Alice: 95, Bob: 82, Charlie: 78 };

for (const [name, score] of Object.entries(scores)) {
  console.log(`${name}: ${score}`);
}

console.log(Object.keys(scores));   // ["Alice","Bob","Charlie"]
console.log(Object.values(scores)); // [95, 82, 78]
← Lesson 07🏠 HomeLesson 09 →