Skip to content
Arrays & ObjectsChapter 22 / 47Beginner4 min read

Objects

Learn what JavaScript objects are, including properties, methods, dot and bracket access, Object.entries, structuredClone, and how references work.

On this page

1. What are objects?

An array stores items by number (index), while an object stores them by name (key). For example, a user's name, email, and age. When a property's value is a function, it is called a method.

In JavaScript, almost everything is an object or behaves like one: arrays, functions, and dates all included.

2. Why does it matter?

When related data is scattered across separate variables, it is hard to manage and pass around.

Separate variablesOne object
userName, userEmail, userAgeuser.name, user.email, user.age
5 arguments for a functionOne user object for a function
The relationship between the data is unclearAll the information in one group
Does not match JSON from an APIThe API's JSON becomes an object directly

3. Where is it used?

AreaObject
User profile{ name, email, role }
API responseJSON data from the server
Settings{ theme: "dark", language: "ur" }
Lookup table{ PK: "Pakistan", AE: "UAE" }
ReactProps and state objects

4. How does it work?

TaskSyntax
Read (dot)user.name
Read (bracket, dynamic key)user[key]
Add / updateuser.age = 25
Deletedelete user.age
Check"email" in user or Object.hasOwn(user, "email")
LoopObject.entries(user)

The reference concept

A variable does not hold the object itself, it holds a reference (address) to it. Assigning it to another variable does not make a copy, both point to the same object.

Reference vs copy
const a = user object
const b = a
a and b both point to the same object
Changing b.name also changes a.name
For a separate copy, use spread or structuredClone

5. Code example

Creating, reading, and methods

js
const user = {
  name: "Ali",
  age: 24,
  skills: ["js", "react"],
  greet() {
    return `Hi, I am ${this.name}`;
  },
};

const field = "age";
console.log(user.name, user[field]);
console.log(user.greet());

user.city = "Lahore";
console.log(Object.hasOwn(user, "city"));

for (const [key, value] of Object.entries(user)) {
  console.log(key, typeof value);
}

Copying: shallow vs deep

js
const original = { name: "Sara", address: { city: "Karachi" } };

const shallow = { ...original };
const deep = structuredClone(original);

shallow.address.city = "Quetta";

console.log(original.address.city);
console.log(deep.address.city);
console.log(Object.keys(original), Object.values(deep).length);

How does the code work?

PartMeaning
user[field]Bracket notation when the key is in a variable, 24
greet()A method, this = user
Object.hasOwnWhether it is an own property, true
Object.entriesAn array of [key, value] pairs
{ ...original }A shallow copy, the nested address is still shared
structuredCloneA deep copy, nested objects are separate too
OutputQuetta (the original changed) and Karachi (the deep copy is safe)

Interview tip

{ ...obj } and Object.assign only copy the first level. For a full copy of nested data, use structuredClone. It does not copy functions or class methods, only data.

6. Common mistakes

Mistake: Thinking assignment makes a copy

Why? const copy = user only copies the reference, so changing the copy changes user too.

Better: Use spread for a shallow copy, and structuredClone(user) for a deep copy.

Mistake: Using dot notation for a dynamic key

Why? user.key literally means the property named key, not the value of the variable.

Better: When the key is in a variable, write user[key].

Mistake: Comparing objects with == or ===

Why? Two different objects are not equal even with the same data, because their references differ.

Better: Compare specific fields, or use a deep-equal utility when needed.

Mistake: Deep copying with JSON.parse(JSON.stringify(obj))

Why? A Date becomes a string, and undefined, Map, and Set are lost.

Better: Use structuredClone in modern code.

7. What to learn before and after

Suggested order
Arrays
Objects
Destructuring
Spread & Rest
JSON

Final quick revision

PointDetail
ObjectKey-value pairs
Accessobj.key or obj[key]
Property checkObject.hasOwn(obj, key)
LoopObject.entries, Object.keys, Object.values
Shallow copy{ ...obj }
Deep copystructuredClone(obj)