Objects in JavaScript
Objects in JavaScript: A Complete Overviewâ
In JavaScript, an object is a collection of key-value pairs where keys are strings (or Symbols) and values can be of any type.
1. Declaring an Objectâ
Using Object Literals (Most Common)â
let person = {
name: "Rahul",
age: 25,
city: "Delhi",
};
Using the Object Constructorâ
let person = new Object();
person.name = "Rahul";
person.age = 25;
2. Accessing & Updating Propertiesâ
console.log(person.name); // "Rahul"
console.log(person["age"]); // 25
person["age"] = 26; // Update existing
person.city = "Mumbai"; // Update existing
person.country = "India"; // Add new property
4. Object Reference Conceptâ
- When you create an object, JavaScript:
- Allocates memory in the heap
- Creates a reference in the stack
- The variable holds this reference, not the actual object
- When you assign an object to another variable, only the reference is copied
let obj1 = { name: "A" }; // 1. Object created in heap
// 2. Reference stored in stack
// 3. obj1 points to heap location
let obj2 = obj1; // Only the reference is copied
// Both variables point to same heap location
5. Shallow Copy vs Deep Copyâ
-
Shallow Copy (nested objects not include)
let objA = { name: "Rahul", details: { age: 25 } };
let copyByAssign = Object.assign({}, objA); // Shallow copy
let copyBySpread = { ...objA }; // Shallow copy -
Deep Copy (Copies everything)
let objA = { name: "Rahul", details: { age: 25 } };
let byJson = JSON.parse(JSON.stringify(objA)); // Deep copy
let byClone = structuredClone(objA);
6. Object Comparison (== vs ===)â
Objects are compared by reference, not by value.
let obj1 = { name: "Rahul" };
let obj2 = { name: "Rahul" };
console.log(obj1 === obj2); // false (Different memory references)
console.log(obj1 == obj2); // false
7. Freezing & Sealing Objectsâ
-
Object.freeze(obj)â Prevents adding, modifying, or deleting properties.let obj = { name: "Rahul" };
Object.freeze(obj);
obj.name = "Amit"; // No effect
obj.age = 30; // No effect
console.log(obj); // { name: "Rahul" } -
Object.seal(obj)â Prevents adding or deleting properties but allows modifications.let obj = { name: "Rahul" };
Object.seal(obj);
obj.name = "Amit"; // Allowed
delete obj.name; // Not allowed
obj.age = 30; // Not allowed
console.log(obj); // { name: "Amit" }