JavaScript Course Lesson 12 of 16
The Codex / JavaScript / Objects

Objects

JavaScript's key-value store — creating, accessing, modifying, and the methods for working with object data.

What it is, in plain English: An object is a collection of named values — key-value pairs. You look up a value by its name (key) rather than its position. Objects are how JavaScript represents structured data: a user has a name, an age, and an email — those three facts live together in one object. Almost everything in JavaScript is an object, which makes this the single most important data structure to understand.

Storing named information in objects

Arrays are great for ordered lists. But what about a person? A person has a name, an age, an email address — information that belongs together but doesn't have a natural order. That's what an object is for: a collection of named values.

Creating an object

// Curly braces, key: value pairs, commas between them
const user = {
  name: "Alice",
  age: 30,
  email: "alice@example.com",
  isActive: true
};

// The keys are the names. The values are the data.
// Keys don't need quotes unless they contain spaces or special characters.

Accessing properties — two ways

const user = { name: "Alice", age: 30 };

// Dot notation — use this most of the time
console.log(user.name);    // "Alice"
console.log(user.age);     // 30

// Bracket notation — use when the key is in a variable, or has spaces
const key = "name";
console.log(user[key]);    // "Alice" — key comes from a variable

const weirdObj = { "first name": "Bob" };
console.log(weirdObj["first name"]);  // "Bob" — spaces need brackets

Adding, updating, and deleting properties

const profile = { name: "Alice" };

profile.age = 30;             // add a new property
profile.name = "Alice Smith"; // update an existing property
delete profile.age;           // remove a property

console.log(profile);         // { name: "Alice Smith" }

Checking if a property exists

const user = { name: "Alice", age: 30 };

"name" in user            // true
"email" in user           // false
user.email !== undefined  // false (same idea, less reliable)

// Safe access with optional chaining:
console.log(user?.address?.city);  // undefined (no error)

Object.keys, Object.values, Object.entries

The three methods for iterating over an object's data:

const scores = { alice: 95, bob: 87, charlie: 72 };

Object.keys(scores)    // ["alice", "bob", "charlie"] — just the keys
Object.values(scores)  // [95, 87, 72]                — just the values
Object.entries(scores) // [["alice",95],["bob",87],["charlie",72]] — pairs

// Loop over key-value pairs:
for (const [name, score] of Object.entries(scores)) {
  console.log(`${name}: ${score}`);
}
// alice: 95
// bob: 87
// charlie: 72

Destructuring — unpack values into variables

const user = { name: "Alice", age: 30, city: "Mumbai" };

// Unpack specific properties:
const { name, city } = user;
console.log(name);  // "Alice"
console.log(city);  // "Mumbai"

// Rename while destructuring:
const { name: userName, age: userAge } = user;
console.log(userName);  // "Alice"

// Default values (used if property doesn't exist):
const { country = "India" } = user;
console.log(country);  // "India" — user has no country, default used

// In function parameters — very common pattern:
function greet({ name, city }) {
  console.log(`${name} lives in ${city}`);
}
greet(user);  // "Alice lives in Mumbai"

Spread — copy or merge objects

const defaults = { theme: "dark", lang: "en", fontSize: 14 };
const userPrefs = { lang: "hi", fontSize: 16 };

// Merge: later properties win over earlier ones
const settings = { ...defaults, ...userPrefs };
console.log(settings);
// { theme: "dark", lang: "hi", fontSize: 16 }

// Shallow copy:
const copy = { ...defaults };
copy.theme = "light";
console.log(defaults.theme);  // "dark" — original unaffected

Methods — functions inside an object

const counter = {
  count: 0,

  // Method shorthand (modern syntax):
  increment() {
    this.count += 1;    // 'this' refers to the counter object
  },
  reset() {
    this.count = 0;
  },
  value() {
    return this.count;
  }
};

counter.increment();
counter.increment();
counter.increment();
console.log(counter.value());  // 3
counter.reset();
console.log(counter.value());  // 0
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.