
Mastering JavaScript Objects: From Basics to Advanced
When you think about the real world, everything around you is an object:
- A car has a brand, color, and model.
- A student has a name, roll number, and age.
- A mobile phone has a company, price, and features.
JavaScript uses the same idea. An object in JavaScript is like a box where we can store related information together in the form of key–value pairs.
What is an Object?
An object is like a real-life item that has properties (features) and methods (actions it can do).
Example: A student in real life
- Name: Priya
- Age: 22
- Course: B.Tech
- Can introduce herself (action).
In JavaScript:
let student = {
name: "Priya",
age: 22,
course: "B.Tech",
introduce: function () {
console.log("Hello, my name is " + this.name);
}
};
Here,
- name, age, course → Properties (features of student).
- introduce → Method (an action).
How to Create Objects?
There are multiple ways to create objects in JavaScript. Let’s look at each one with simple examples.
1️⃣ Object Literal { }
This is the easiest and most common way. You simply use curly braces and directly write properties.
Example: A Car Object
let car = {
brand: "Tesla",
model: "Model 3",
year: 2025
};
console.log(car.brand);
console.log(car.year);
Output:
Tesla
2025
2️⃣ Using new Object()
This is a built-in constructor that creates an empty object. Then, we add properties one by one.
Example: A Mobile Phone
let phone = new Object();
phone.brand = "Apple";
phone.model = "iPhone 15";
phone.price = 120000;
console.log(phone.model);
console.log(phone.price);
Output:
iPhone 15
120000
3️⃣ Constructor Function
We can create a function as a blueprint to make multiple objects of the same type.
Example: A Student Blueprint
function Student(name, age, course) {
this.name = name;
this.age = age;
this.course = course;
}
let student1 = new Student("Priya", 22, "B.Tech");
let student2 = new Student("Amit", 21, "MBA");
console.log(student1.name, student1.course);
console.log(student2.name, student2.course);
Output:
Priya B.Tech
Amit MBA
4️⃣ ES6 class
Introduced in ES6 (2015), class is a cleaner way of writing constructor functions.
Example: A Book Class
class Book {
constructor(title, author, year) {
this.title = title;
this.author = author;
this.year = year;
}
}
let book1 = new Book("Ramayana", "Valmiki", "500 BC");
let book2 = new Book("Mahabharata", "Vyasa", "400 BC");
console.log(book1.title, book1.author);
console.log(book2.title, book2.year);
Output:
Ramayana Valmiki
Mahabharata 400 BC
5️⃣ Object.create()
This method creates a new object from an existing object (prototype).
Example: A Prototype Animal
let animal = {
eats: true,
sleep: function() {
console.log("Sleeping...");
}
};
let dog = Object.create(animal);
dog.bark = function() {
console.log("Woof! Woof!");
};
console.log(dog.eats);
dog.bark();
dog.sleep();
Output:
true
Woof! Woof!
Sleeping...
Now you know 5 different ways to create objects in JavaScript.
Accessing Object Properties
Objects store data in the form of key–value pairs.
To get the value, we use the property’s name (key).
We have already used it in the object's creation examples.
There are two main ways to access properties:
1️⃣ Dot Notation (.)
This is the most common way.
We simply write the object name, a dot, and the property name.
Example:
let person = {
name: "Priya",
age: 22,
city: "Delhi"
};
// Accessing
console.log(person.name);
console.log(person.city);
Output:
Priya
Delhi
Here, the name & city property of the person object is accessed using dot operator.
2️⃣ Bracket Notation ([" "])
This works like looking up a word in a dictionary.
We put the property name inside quotes.
Example:
let book = {
title: "Ramayana",
author: "Valmiki"
};
// Accessing
console.log(book["title"]);
console.log(book["author"]);
Output:
Ramayana
Valmiki
Modifying Objects in JavaScript
Objects in JavaScript are mutable, meaning we can change them even if they are declared with const.
1️⃣ Updating Existing Properties
We can directly assign a new value to a property.
Example: A shop updates product price
let product = { item: "Shoes", price: 2000 };
// Update price
product.price = 1800;
console.log(product.price);
Output:
1800
2️⃣ Adding New Properties
We can add new key–value pairs anytime.
Example: A student adds a course
let student = { name: "Amit", age: 21 };
// Add new property
student.course = "B.Tech";
console.log(student);
Output:
{ name: 'Amit', age: 21, course: 'B.Tech' }
3️⃣ Deleting Properties
We use the delete keyword.
Example: Removing an expired product
let item = { name: "Milk", price: 50, expiry: "2024-09-10" };
// Delete expiry
delete item.expiry;
console.log(item);
Output:
{ name: 'Milk', price: 50 }
4️⃣ Preventing Modification
Sometimes we want objects to be read-only.
- Object.freeze(obj) → Cannot add, remove, or update properties.
- Object.seal(obj) → Can update existing properties but cannot add or remove.
Example: Freezing
let company = { name: "Ujjwalit", year: 2025 };
Object.freeze(company);
company.year = 2030; // ignored
company.city = "Noida"; // ignored
console.log(company);
Output:
{ name: 'Ujjwalit', year: 2025 }
Example: Sealing
let course = { title: "JavaScript", duration: "3 months" };
Object.seal(course);
course.duration = "4 months"; // allowed
course.price = 5000; // not added
delete course.title; // not deleted
console.log(course);
Output:
{ title: 'JavaScript', duration: '4 months' }
Nested Objects
A nested object simply means an object inside another object.
Think of it like folders inside a computer folder 📁.
1️⃣ Creating Nested Object
Example:
let company = {
name: "Ujjwalit",
founded: 2020,
team: {
lead: "Priya",
members: 10,
skills: {
frontend: "React",
backend: "Node.js"
}
}
};
Here:
- company is the main object.
- team is a nested object inside company.
- skills is another nested object inside team.
2️⃣ Accessing Nested Objects
We use dot notation or bracket notation repeatedly.
Example:
console.log(company.team.lead); // Priya
console.log(company.team.members); // 10
console.log(company.team.skills.backend); // Node.js
console.log(company["team"]["skills"]["frontend"]); // React
Output:
Priya
10
Node.js
React
3️⃣ Modifying Nested Objects
We can update, add, or delete properties at any level.
(a) Updating Values
company.team.members = 12;
company.team.skills.backend = "Express.js";
console.log(company.team.members); // 12
console.log(company.team.skills.backend); // Express,js
(b) Adding New Properties
company.team.location = "Delhi";
company.team.skills.database = "MongoDB";
console.log(company.team.location); // Delhi
console.log(company.team.skills.database); //MongoDB
(c) Deleting Properties
delete company.team.lead;
delete company.team.skills.frontend;
console.log(company.team); // { members: 12, skills: { backend: 'Express.js', database: 'MongoDB' }, location: 'Delhi' }
JavaScript objects are not just data containers — they are the foundation of the language. From simple key-value pairs to prototypes and ES6+ features, mastering objects gives you control over data modeling, API handling, and OOP in JavaScript.
Happy Learning !!
2 Reactions
0 Bookmarks