100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Objects in JavaScript

Learn how to create, access, and modify JavaScript objects, and understand why objects behave as reference types.

Objects & ArraysBeginner10 min readJul 8, 2026
Analogies

1. Introduction

An object in JavaScript is a collection of key-value pairs used to represent structured data, such as a user, a product, or a configuration. Keys (also called properties) are strings (or Symbols), and values can be any type, including numbers, strings, arrays, functions, or other objects. Objects are one of the most fundamental building blocks in JavaScript, and almost everything in the language, including arrays and functions, is a special kind of object.

🏏

Cricket analogy: A player's scorecard entry (name, runs, strike-rate, team) is a set of key-value pairs bundling structured stats about that player, and even a full team roster is really a specialized version of the same object idea.

2. Syntax

javascript
// Object literal syntax
const person = {
  name: "Alice",
  age: 25,
  isActive: true
};

// Accessing properties
person.name;        // dot notation
person["age"];       // bracket notation

// Adding, updating, and deleting properties
person.email = "alice@example.com"; // add
person.age = 26;                    // update
delete person.email;                 // remove

3. Explanation

Objects can be created using the object literal syntax { key: value }, which is by far the most common approach. Properties can be read or written using dot notation (obj.key) when the key name is a valid identifier, or bracket notation (obj["key"]) when the key is dynamic or contains special characters. New properties can be added at any time simply by assigning to a new key, existing properties can be reassigned, and the delete operator removes a property entirely.

🏏

Cricket analogy: Writing out a scorecard as {runs: 45, fours: 6} is like the object literal shorthand; you read runs via scorecard.runs (dot notation) but must use scorecard["strike-rate"] (bracket notation) when the stat name has a hyphen, and a dropped player is simply deleted from the lineup.

Unlike primitive types (numbers, strings, booleans), objects are reference types. A variable holding an object doesn't store the object itself — it stores a reference (a pointer) to the object's location in memory. This has an important consequence when you assign one object variable to another.

🏏

Cricket analogy: Handing a team's official scorebook to a second scorer doesn't create a duplicate book — both scorers now point to the same physical scorebook, so any edit either makes updates the one shared record.

Aliasing trap: when you assign an existing object to a new variable (const b = a), you do NOT create a copy — both variables point to the exact same object in memory. Mutating the object through either variable is visible through the other. To get an independent copy, use the spread operator ({ ...a }) or Object.assign({}, a) — though be aware these only produce a shallow copy.

4. Example

javascript
const user = { name: 'Alice', age: 25 };

// Accessing properties
console.log(user.name);
console.log(user['age']);

// Adding and modifying properties
user.email = 'alice@example.com';
user.age = 26;

// Reference type demonstration
const userAlias = user;
userAlias.name = 'Alicia';

console.log(user.name);
console.log(user === userAlias);

// Deleting a property
delete user.email;
console.log(user);

5. Output

text
Alice
25
Alicia
true
{ name: 'Alicia', age: 26 }

6. Key Takeaways

  • Objects store data as key-value pairs and can be created with object literal syntax { }.
  • Use dot notation for known keys and bracket notation for dynamic or special-character keys.
  • Properties can be added, updated, or removed at any time with assignment or delete.
  • Objects are reference types — assigning one variable to another copies the reference, not the data, so both variables point to the same object.
  • Use the spread operator or Object.assign to make a shallow copy that avoids the aliasing trap.
  • === on objects compares references, not contents, so two objects with identical properties are not === equal unless they are the same reference.

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#ObjectsInJavaScript#Objects#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris