Objects Overview in JavaScript

Introduction

An object groups related data and behavior as properties (key–value pairs). Objects model users, products, configuration, and API responses. Unlike arrays, object property order is not the main idea—fast lookup by name is. This chapter builds intuition before you create and mutate objects in depth.

Prerequisites

Object Literals

The most common way to create objects:

javascript
// User profile object
const user = {
  id: 1,
  name: "Ada Lovelace",
  active: true,
};
 
console.log(user.name);
console.log(user["active"]);

Keys are usually strings (or Symbols). Values can be any type, including other objects and functions.

Dot vs Bracket Access

javascript
// Dot notation for valid identifiers
const config = { theme: "dark", lang: "en" };
console.log(config.theme);
 
// Bracket notation for dynamic keys
const field = "lang";
console.log(config[field]);

Use brackets when the key is computed or contains special characters.

Objects Are Mutable

You can change properties after creation:

javascript
// Update property
const cart = { itemCount: 1, total: 9.99 };
cart.itemCount = 2;
cart.total = 19.98;
console.log(cart);

The reference stored in cart stays the same; inner data changes.

Reference Semantics

Objects are assigned and passed by reference:

javascript
// Two variables, one object
const a = { score: 10 };
const b = a;
b.score = 99;
console.log(a.score);

Copying often needs intentional cloning (spread or structured clone)—covered when extending objects.

Methods (Functions as Properties)

When a property holds a function, it is called a method:

javascript
// Method shorthand in object literal
const counter = {
  value: 0,
  increment() {
    this.value += 1;
  },
};
 
counter.increment();
console.log(counter.value);

this behavior is important in later OOP chapters.

Nested Objects

javascript
// Nested address
const order = {
  id: "A-100",
  customer: {
    name: "Lin",
    city: "Austin",
  },
};
 
console.log(order.customer.city);

Combine with optional chaining from operators: order.customer?.city.

Objects vs Arrays

ObjectsArrays
Named keysOrdered numeric indices
Great for records, configsGreat for lists, sequences
for...in / Object.keysfor...of, .map

Many APIs return objects containing arrays and vice versa.

typeof and Objects

javascript
// typeof object (including arrays)
console.log(typeof {});
console.log(typeof []);
console.log(Array.isArray([]));

typeof null is "object" (historical quirk)—use strict null checks.

FAQ

Are objects the same as JSON?

JSON is a text format inspired by object syntax. JSON.parse / JSON.stringify convert between text and objects—see serialization.

Can object keys be numbers?

Keys are strings (or Symbols). Numeric keys become string properties: obj[1] is obj["1"].

Should I use Map instead of objects?

Plain objects are default for records. Map helps when keys are not strings or you need frequent add/remove—advanced topic.

What comes next?

Creating objects and property access patterns.