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
// 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; // remove3. 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
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
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.assignto 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
1. What does the following code print? const a = { x: 1 }; const b = a; b.x = 99; console.log(a.x);
2. Which notation must you use to access a property whose name is stored in a variable, e.g. `let key = 'age'`?
3. What is the result of `{ a: 1 } === { a: 1 }`?
4. Which operator removes a property from an object entirely?
5. What does `Object.assign({}, original)` produce?
6. What data type is returned by `typeof {}`?
Was this page helpful?
You May Also Like
Arrays in JavaScript
Understand how JavaScript arrays store ordered lists of values, how indexing and length work, and why `typeof` alone can't identify an array.
Classes in JavaScript
How ES6 class syntax provides a cleaner, structured way to create objects and implement object-oriented patterns on top of JavaScript's prototype system.
Destructuring in JavaScript
Learn how to unpack values from arrays and properties from objects into distinct variables using destructuring, including defaults and renaming.
Working with JSON in JavaScript
Learn how to convert JavaScript values to and from JSON text using JSON.stringify and JSON.parse, and understand their limitations.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics