# 📘 JavaScript Objects and Their Manipulation

# **1\. Introduction**

In JavaScript, an **object** is a data structure that stores information in **key–value pairs**.  
Objects help represent real-world entities like:

* A User
    
* A Product
    
* A Car
    
* A Student
    

---

# **2\. What is an Object?**

### Example:

```javascript
const person = {
  name: "Niranjan",
  age: 23,
  isDeveloper: true,
  greet: function () {
    console.log("Hello, I am " + this.name);
  }
};
```

### Explanation:

* `name`, `age`, `isDeveloper` → **properties**
    
* `greet()` → **method**
    
* [`this.name`](http://this.name) → refers to [`person.name`](http://person.name)
    

---

# **3\. Ways to Create Objects**

---

## **3.1 Using Object Literal (Most Common)**

```javascript
const car = {
  brand: "Tata",
  model: "Safari",
  year: 2024
};

console.log(car.brand); // Tata
```

---

## **3.2 Using** `new Object()`

```javascript
const user = new Object();
user.name = "Niranjan";
user.role = "Developer";

console.log(user);  
```

---

## **3.3 Using Constructor Functions**

```javascript
function Person(name, age) {
  this.name = name;
  this.age = age;
}

const p1 = new Person("Niranjan", 23);
console.log(p1.name); // Niranjan
```

---

## **3.4 Using ES6 Classes**

```javascript
class Animal {
  constructor(type, sound) {
    this.type = type;
    this.sound = sound;
  }
}

const dog = new Animal("Dog", "Bark");
console.log(dog.type); // Dog
```

---

# **4\. Accessing Object Properties**

---

## **4.1 Dot Notation**

```javascript
const student = { name: "Rohan", marks: 80 };
console.log(student.name); // Rohan
```

---

## **4.2 Bracket Notation**

```javascript
const item = { "item price": 299 };

console.log(item["item price"]); // 299
```

Dynamic key:

```javascript
const key = "name";
const obj = { name: "Niranjan" };

console.log(obj[key]); // Niranjan
```

---

# **5\. Adding and Updating Properties**

---

## **5.1 Adding Properties**

```javascript
const person = { name: "Niranjan" };

person.city = "Kolkata";
person.country = "India";

console.log(person);
```

---

## **5.2 Updating Properties**

```javascript
person.name = "Niranjan Kumar Singh";
console.log(person.name);
```

---

# **6\. Deleting Properties**

```javascript
delete person.country;
console.log(person);
```

---

# **7\. Checking if a Property Exists**

---

## **7.1 Using** `in` operator

```javascript
console.log("city" in person); // true
```

---

## **7.2 Using** `hasOwnProperty()`

```javascript
console.log(person.hasOwnProperty("name")); // true
```

---

# **8\. Looping Through an Object**

---

## **8.1 Using** `for...in`

```javascript
const laptop = {
  brand: "HP",
  ram: "16GB",
  processor: "i5"
};

for (let key in laptop) {
  console.log(key, laptop[key]);
}
```

Output:

```javascript
brand HP
ram 16GB
processor i5
```

---

# **9\. Useful Object Methods**

---

## **9.1** `Object.keys()`

```javascript
console.log(Object.keys(laptop));
// ["brand", "ram", "processor"]
```

---

## **9.2** `Object.values()`

```javascript
console.log(Object.values(laptop));
// ["HP", "16GB", "i5"]
```

---

## **9.3** `Object.entries()`

```javascript
console.log(Object.entries(laptop));
```

Output:

```javascript
[
  ["brand", "HP"],
  ["ram", "16GB"],
  ["processor", "i5"]
]
```

---

# **10\. Copying and Cloning Objects**

---

## **10.1 Shallow Copy using Spread Operator**

```javascript
const newLaptop = { ...laptop };
console.log(newLaptop);
```

---

## **10.2 Shallow Copy using Object.assign()**

```javascript
const copy = Object.assign({}, laptop);
```

---

## **10.3 Deep Copy using JSON**

```javascript
const deepCopy = JSON.parse(JSON.stringify(laptop));
```

---

# **11\. Merging Objects**

```javascript
const a = { x: 10 };
const b = { y: 20 };

const merged = { ...a, ...b };

console.log(merged); // { x: 10, y: 20 }
```

---

# **12\. Object Immutability**

---

## **12.1 Using** `Object.freeze()`

```javascript
const carModel = { name: "Safari" };

Object.freeze(carModel);

carModel.name = "Nexon"; // Change fails
console.log(carModel.name); // Safari
```

---

## **12.2 Using** `Object.seal()`

```javascript
const emp = { name: "Ravi", salary: 20000 };

Object.seal(emp);

emp.salary = 25000; // Allowed
emp.city = "Delhi"; // Not allowed

console.log(emp);
```

---

# **13\. Nested Objects**

```javascript
const user = {
  name: "Niranjan",
  address: {
    city: "Kolkata",
    pin: 700001
  }
};

console.log(user.address.city); // Kolkata
```

---

# **14\. Summary Table**

| Operation | Example |
| --- | --- |
| Create object | `const obj = {}` |
| Access | `obj.key`, `obj["key"]` |
| Add | `obj.newKey = value` |
| Update | `obj.key = newValue` |
| Delete | `delete obj.key` |
| Loop | `for...in` |
| Keys | `Object.keys(obj)` |
| Values | `Object.values(obj)` |
| Entries | `Object.entries(obj)` |
| Copy | `{ ...obj }` |
| Merge | `{ ...obj1, ...obj2 }` |
| Freeze | `Object.freeze(obj)` |
