Skip to main content

Command Palette

Search for a command to run...

Objects in JavaScript :

Updated
•4 min read•View as Markdown
Objects in JavaScript :

When learning JavaScript, objects are one of the most important concepts. They help us store and organize related information together in a structured way. In this blog, we will understand objects step-by-step in a simple way with examples.


1. What Are Objects and Why Are They Needed?

In JavaScript, an object is a collection of key-value pairs.

Think of an object like a student ID card.

A student card contains different details:

  • Name

  • Age

  • Class

  • Roll Number

Instead of storing these separately, we can store them together inside an object.

Example:

let student = {
  name: "Radhika",
  age: 17,
  course: "BTech"
};

Here:

  • name, age, course → keys (properties)

  • Radhika, 17, B.Tech → values

So the object student stores all related information together.

Why Objects Are Needed ?

Objects help us:

  • Organize data properly

  • Store related data in one place

  • Represent real world things (user, product, car, etc.)

  • Work with APIs and databases easily

Without objects, managing complex data becomes messy.


2. Creating Objects

There are multiple ways to create objects in JavaScript.

Method 1: Object Literal (Most Common)

let car = {
  brand: "Toyota",
  model: "Fortuner",
  year: 2022
};

Method 2: Using new Object()

let car = new Object();

car.brand = "Toyota";
car.model = "Fortuner";
car.year = 2022;

But developers mostly use object literal because it is simpler.


3. Accessing Object Properties

There are two ways to access object values.

1. Dot Notation

console.log(student.name);

Output:

Radhika

2. Bracket Notation

console.log(student["age"]);

Output:

17

When to Use Bracket Notation

Bracket notation is useful when:

  • Property name is stored in a variable

Example:

let key = "course";
console.log(student[key]);

4. Updating Object Properties

We can update any property easily.

Example:

student.age = 21;

Now the object becomes:

{
  name: "Radhika",
  age: 21,
  course: "BTech"
}

5. Adding New Properties

We can add new properties to an object anytime.

Example:

student.city = "Delhi";

Now the object becomes:

{
  name: "Radhika",
  age: 21,
  course: "BTech",
  city: "Delhi"
}

6. Deleting Properties

We can remove properties using the delete keyword.

Example:

delete student.course;

Now the object becomes:

{

name: "Radhika",
age: 21,
city: "Delhi"
}


7. Looping Through Object Keys

Sometimes we want to see all the properties of an object. For that we use for...in loop.

Example:

let student = {
  name: "Radhika",
  age: 17,
  course: "BTech"
};

for (let key in student) {
  console.log(key + ": " + student[key]);
}

Output:

name: Radhika
age: 17
course: BTech

Explanation:

  • key → property name

  • student[key] → value of that property


Extra Things You Should Also Know

1. Checking if Property Exists

console.log("name" in student);

Output:

true

2. Getting All Keys

console.log(Object.keys(student));

Output:

["name", "age", "course"]

3. Getting All Values

console.log(Object.values(student));

Output:

["Radhika", 17, "BTech"]

Real Life Example

let product = {
  name: "Laptop",
  price: 60000,
  brand: "HP",
  inStock: true
};

console.log(product.name);
product.price = 55000;
product.color = "Silver";

for (let key in product) {
  console.log(key + ": " + product[key]);
}

Conclusion

By understanding how to create objects, access properties, update values, and loop through keys, you can build more complex and organized programs.

Key takeaways:

  • Objects store data as key-value pairs

  • Use dot notation or bracket notation to access properties

  • Properties can be updated, added, or deleted

  • for...in loop helps iterate through object keys

Mastering objects is an important step toward learning advanced JavaScript concepts like APIs, JSON, and object-oriented programming. Thank you for reading this blog. I am open to feedback and suggestion.