Are you confused about JavaScript Objects? You're not alone!
In this blog, we’ll break down JavaScript Objects in the simplest way possible — with real-life examples, code snippets, and clear explanations.
🧠 What is an Object in JavaScript?
An object is a collection of key-value pairs. Think of it as a mini container to hold related information together.
Javascript Code-
const person = {
name: "Anna",
age: 22,
isStudent: true
};
Here, person is an object with 3 properties.
🧩 Why Use Objects?
Objects help you group and organize data. For example, a user’s name, email, and password can be stored in one object.
js
const user = {
username: "annacodes",
email: "https://jsexplained.blogspot.com",
password: "secure123"
};
🔄 Accessing & Updating Object Values
js
console.log(person.name); // Output: Anna
person.age = 23; // Updating age
console.log(person.age); // Output: 23
You can also use bracket notation:
js
console.log(person["isStudent"]); // true
🧪 Object Methods
Objects can also store functions (called methods):
js
const car = {
brand: "Toyota",
start: function () {
console.log("Car has started!");
}
};
car.start(); // Car has started!
📦 Nested Objects
Objects can have other objects inside them:
js
const student = {
name: "Karan",
address: {
city: "Delhi",
pin: 110001
}
};
console.log(student.address.city); // Delhi
📌 Bonus: Object Destructuring
Destructuring is a clean way to extract values:
js
const { name, age } = person;
console.log(name); // Anna
🙌 Wrapping Up
Objects are the building blocks of JavaScript. Once you
understand them, working with real-world data becomes much easier.
💬 Was this helpful?
Let me know in the comments or connect with me!
📎 Original Blog Post
Top comments (0)