In JavaScript, an Object is a non-primitive data type used to store data in key-value pairs.
Objects are one of the most important concepts in JavaScript because almost everything in JavaScript is based on objects.
1. Creating Objects
Using Object Literal (Most Common)
const person = {
name: "John",
age: 25,
city: "New York"
};
console.log(person);
Using new Object()
const person = new Object();
person.name = "John";
person.age = 25;
console.log(person);
2. Accessing Object Properties
Dot Notation
console.log(person.name);
console.log(person.age);
Bracket Notation
console.log(person["name"]);
console.log(person["age"]);
Useful when property names are dynamic.
let key = "name";
console.log(person[key]);
3. Adding Properties
person.country = "USA";
console.log(person);
4. Updating Properties
person.age = 30;
console.log(person.age);
5. Deleting Properties
delete person.city;
console.log(person);
6. Nested Objects
const student = {
name: "Sam",
marks: {
math: 90,
science: 85
}
};
console.log(student.marks.math);
7. Object Methods
Methods are functions inside objects.
const user = {
name: "Alex",
greet: function () {
console.log("Hello");
}
};
user.greet();
Important Object Methods
1. Object.keys()
Returns all object keys.
const person = {
name: "John",
age: 25
};
console.log(Object.keys(person));
Output:
["name", "age"]
2. Object.values()
Returns all values.
console.log(Object.values(person));
Output:
["John", 25]
3. Object.entries()
Returns key-value pairs as arrays.
console.log(Object.entries(person));
Output:
[
["name", "John"],
["age", 25]
]
4. Object.assign()
Copies properties from one object to another.
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const result = Object.assign({}, obj1, obj2);
console.log(result);
Output:
{ a: 1, b: 2 }
5. Object.freeze()
Prevents modification.
const car = {
brand: "BMW"
};
Object.freeze(car);
car.brand = "Audi";
console.log(car.brand);
Output:
BMW
6. Object.seal()
Allows updating existing properties but prevents adding/removing.
const user = {
name: "John"
};
Object.seal(user);
user.name = "Sam"; // allowed
user.age = 30; // not allowed
console.log(user);
7. Object.hasOwn()
Checks if property exists.
const person = {
name: "John"
};
console.log(Object.hasOwn(person, "name"));
Output:
true
8. Object.create()
Creates a new object using another object as prototype.
const animal = {
sound: "Bark"
};
const dog = Object.create(animal);
console.log(dog.sound);
9. Object.fromEntries()
Converts array to object.
const arr = [
["name", "John"],
["age", 25]
];
console.log(Object.fromEntries(arr));
Output:
{
name: "John",
age: 25
}
10. Object.is()
Compares two values.
console.log(Object.is(10, 10));
Output:
true
Looping Through Objects
Using for...in
const person = {
name: "John",
age: 25
};
for (let key in person) {
console.log(key, person[key]);
}
Object Destructuring
const person = {
name: "John",
age: 25
};
const { name, age } = person;
console.log(name);
console.log(age);
Spread Operator with Objects
const obj1 = {
a: 1
};
const obj2 = {
...obj1,
b: 2
};
console.log(obj2);
Shallow Copy vs Reference
Reference Copy
const obj1 = {
name: "John"
};
const obj2 = obj1;
obj2.name = "Sam";
console.log(obj1.name);
Output:
Sam
Shallow Copy
const obj1 = {
name: "John"
};
const obj2 = { ...obj1 };
obj2.name = "Sam";
console.log(obj1.name);
Output:
John
Real-World Example
const employee = {
id: 101,
name: "David",
department: "QA",
skills: ["JavaScript", "Playwright"],
displayInfo() {
console.log(`${this.name} works in ${this.department}`);
}
};
employee.displayInfo();
Common Interview Questions
Difference Between Object and Array
| Object | Array |
|---|---|
| Stores key-value pairs | Stores ordered values |
| Uses keys | Uses indexes |
{} | [] |
Difference Between == and Object.is()
console.log(NaN == NaN); // false
console.log(Object.is(NaN, NaN)); // true
Practice Programs
1. Count Object Properties
const user = {
name: "John",
age: 25,
city: "NY"
};
console.log(Object.keys(user).length);
2. Merge Two Objects
const a = { x: 1 };
const b = { y: 2 };
const c = { ...a, ...b };
console.log(c);
3. Convert Object to Array
const person = {
name: "John",
age: 25
};
console.log(Object.entries(person));
Summary
JavaScript objects are used to:
- Store structured data
- Create reusable methods
- Represent real-world entities
- Manage application state
Most commonly used object methods:
Object.keys()Object.values()Object.entries()Object.assign()Object.freeze()Object.seal()Object.hasOwn()
10 JavaScript Object Programs with Solutions
1. Count Number of Properties in an Object
Problem
Find total number of keys in an object.
Solution
const user = {
name: "John",
age: 25,
city: "New York"
};
const count = Object.keys(user).length;
console.log(count);
Output
3
2. Iterate Through Object Properties
Problem
Print all keys and values from an object.
Solution
const student = {
name: "Sam",
marks: 90,
grade: "A"
};
for (let key in student) {
console.log(key + " : " + student[key]);
}
Output
name : Sam
marks : 90
grade : A
3. Merge Two Objects
Problem
Combine two objects into one.
Solution
const obj1 = {
a: 1,
b: 2
};
const obj2 = {
c: 3,
d: 4
};
const merged = { ...obj1, ...obj2 };
console.log(merged);
Output
{
a: 1,
b: 2,
c: 3,
d: 4
}
4. Check if Property Exists
Problem
Check whether a key exists in object.
Solution
const employee = {
id: 101,
name: "David"
};
console.log("name" in employee);
console.log("salary" in employee);
Output
true
false
5. Remove Property from Object
Problem
Delete a property from object.
Solution
const car = {
brand: "BMW",
color: "Black"
};
delete car.color;
console.log(car);
Output
{
brand: "BMW"
}
6. Convert Object to Array
Problem
Convert object into array of key-value pairs.
Solution
const person = {
name: "John",
age: 30
};
const result = Object.entries(person);
console.log(result);
Output
[
["name", "John"],
["age", 30]
]
7. Find Sum of Object Values
Problem
Calculate sum of numeric values in object.
Solution
const marks = {
math: 90,
science: 80,
english: 85
};
let sum = 0;
for (let key in marks) {
sum += marks[key];
}
console.log(sum);
Output
255
8. Clone an Object
Problem
Create copy of object without affecting original.
Solution
const original = {
name: "Alex",
age: 28
};
const copy = { ...original };
copy.name = "Sam";
console.log(original);
console.log(copy);
Output
{ name: "Alex", age: 28 }
{ name: "Sam", age: 28 }
9. Freeze an Object
Problem
Prevent object modification.
Solution
const user = {
name: "John"
};
Object.freeze(user);
user.name = "Sam";
console.log(user.name);
Output
John
10. Nested Object Access
Problem
Access values inside nested objects.
Solution
const company = {
name: "TechSoft",
employee: {
id: 101,
details: {
name: "David",
role: "QA Engineer"
}
}
};
console.log(company.employee.details.name);
console.log(company.employee.details.role);
Output
David
QA Engineer
Bonus Program — Object Destructuring
Problem
Extract object properties into variables.
Solution
const user = {
name: "John",
age: 25
};
const { name, age } = user;
console.log(name);
console.log(age);
Output
John
25
Concepts Covered
These programs cover:
- Object creation
- Object iteration
- Object methods
- Nested objects
- Destructuring
- Spread operator
- Property checking
- Cloning
- Freezing objects
- Data transformation