0% found this document useful (0 votes)
10 views6 pages

Understanding JavaScript Objects

In JavaScript, an object is a collection of unique key-value pairs that can store various data types, including numbers, strings, other objects, and functions. Objects can be created using object literals or the Object constructor, and they allow for basic operations such as accessing, modifying, adding, deleting properties, and merging objects. The object literal syntax is preferred for its simplicity and performance advantages.

Uploaded by

shubham280407
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Understanding JavaScript Objects

In JavaScript, an object is a collection of unique key-value pairs that can store various data types, including numbers, strings, other objects, and functions. Objects can be created using object literals or the Object constructor, and they allow for basic operations such as accessing, modifying, adding, deleting properties, and merging objects. The object literal syntax is preferred for its simplicity and performance advantages.

Uploaded by

shubham280407
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Objects in JavaScript

🔹 What is an Object?

An object in JavaScript is a collection of key-value pairs.

Each key (also called a property name) is unique and stores a

value.

Values can be:

• Numbers

• Strings

• Other objects

• Functions (called methods when inside an object)

Objects help in grouping related data and functions together.

🔹 Example:

let student = {

name: "Sourav",

age: 23,
course: "BCA"

};

[Link](student);

Output:

{ name: 'Sourav', age: 23, course: 'BCA' }

• Ways to Create an Object

1️⃣ Using Object Literal (Recommended)

let person = { name: "Sourav", age: 23, job: "Developer" };

2️⃣ Using new Object() Constructor

let person = new Object();

[Link] = "Sourav";

[Link] = 23;
[Link] = "Developer";

Output (for both):

{ name: 'Sourav', age: 23, job: 'Developer' }

Basic Operations on Objects

1️⃣ Accessing Object Properties

let obj = { name: "Sourav", age: 23 };

// Dot notation

[Link]([Link]);

// Bracket notation

[Link](obj["age"]);

Output:

Sourav

23
2️⃣ Modifying Object Properties

let obj = { name: "Sourav", age: 22 };

[Link] = 23;

[Link](obj);

Output:

{ name: 'Sourav', age: 23 }

3️⃣ Adding New Properties

let car = { model: "Tesla" };

[Link] = "Red";

[Link](car);

Output:

{ model: 'Tesla', color: 'Red' }

4️⃣ Deleting Properties


let car = { model: "Tesla", color: "Red" };

delete [Link];

[Link](car);

Output:

{ model: 'Tesla' }

5️⃣ Merging Two Objects

let obj1 = { name: "Sourav" };

let obj2 = { age: 23 };

let merged = { ...obj1, ...obj2 };

[Link](merged);

Output:

{ name: 'Sourav', age: 23 }


{} vs new Object()

Feature {} (Literal) new Object()

Ease of Use Simple and clear Longer syntax

Performance Faster Slightly slower

Common Usage Most used Rarely used

Example let a = {}; let a = new Object();

✅ Preferred: {} (object literal) because it is simpler and

faster.

You might also like