Academy

Objects

The Problem Objects Solve

An array stores an ordered list. But sometimes order isn't the point — relationship is.

A student has a name, marks, subject, and attendance. You could store these as four separate variables, or as four elements in an array. But student[0] tells you nothing. student.name tells you everything.

An object groups related values under meaningful names called properties.

Creating Objects

An object is written with curly braces {}. Each entry is a key-value pair separated by a colon, pairs separated by commas. The trailing comma after the last property is optional but recommended — it makes adding new properties easier.

Two ways to access properties:

  • Dot notation student.name — clean, readable, use this by default
  • Bracket notation student["name"] — necessary when the key is stored in a variable or contains special characters

Adding, Changing, Deleting Properties

Objects are mutable. Even if declared with const, you can change, add, or delete properties — const only prevents you from reassigning the variable to a completely different object. The contents are freely modifiable.

Methods — Functions Inside Objects

A property whose value is a function is called a method.

this refers to the object the method belongs to. Inside area(), this.radius means "the radius property of this same object." This is how an object's methods can access its own data.

Looping Over an Object

Object.entries(obj) returns an array of [key, value] pairs — which means you can immediately chain array methods onto it:

Destructuring

Destructuring is a clean way to extract properties into variables:

Destructuring is everywhere in modern JavaScript — in function parameters, in loops, in imports. It eliminates the repetitive obj.prop pattern and makes code dramatically more readable.

Destructuring in Function Parameters

This pattern — destructuring directly in the parameter list — is extremely common in React components, API handlers, and configuration functions.

Spreading and Copying Objects

Just like arrays, the spread operator works on objects:

This pattern — spread defaults then user overrides — is one of the most common patterns in all of JavaScript. Configuration objects, React props, API options — you will write this hundreds of times.

Arrays of Objects

The most common data structure in real applications: an array where each element is an object.

The reduce here builds an entirely new object — grouping students by city and accumulating totals. This pattern (called group by) appears in data analysis, dashboards, and reporting constantly.

Nested Objects

Objects can contain other objects:

What You Have Learned

Objects are how JavaScript represents structured, named data.

The key ideas:

  • { key: value } — create; .prop or ["prop"] — access
  • Objects are mutable even when declared with const
  • Methods are functions inside objects; this refers to the owning object
  • Object.keys(), Object.values(), Object.entries() — bridge to array methods
  • Destructuring { name, marks } = obj — the modern way to unpack properties
  • Spread { ...a, ...b } — merge or copy objects
  • Arrays of objects + chained array methods = the backbone of real data processing

Together, arrays and objects can represent any data structure in existence — a student record, a weather report, a social media post, a physics simulation. Everything from here builds on these two foundations.

In the next lesson: Closures — the most misunderstood concept in JavaScript, and the one that unlocks everything: private state, factory functions, callbacks, and the entire async model.