Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

Updated
5 min readView as Markdown

In JavaScript, objects are used to store related information together.

They help us represent real-world entities like:

  • a person

  • a student

  • a car

  • a product

Instead of storing data in many separate variables, we can group it inside one object.


1. What Are Objects?

An object is a collection of key-value pairs.

Example:

let person = {
  name: "Pankaj",
  age: 25,
  city: "Patna"
};

Here:

Key Value
name "Pankaj"
age 25
city "Patna"

So an object stores information about something in a structured way.


Real-World Example

Imagine storing information about a person.

Instead of doing this:

let name = "Pankaj";
let age = 25;
let city = "Patna";

We can group everything inside an object.

let person = {
  name: "Pankaj",
  age: 25,
  city: "Patna"
};

This makes the data organized and easier to manage.


Object Structure Visualization

person
   ↓
+----------------+
| name : Pankaj  |
| age  : 25      |
| city : Patna   |
+----------------+

Each property has:

key : value

2. Creating Objects

Objects are created using curly braces {}.

Example:

let car = {
  brand: "BMW",
  color: "Black",
  speed: 200
};

Now we have an object called car with three properties.


3. Accessing Object Properties

We can access properties in two ways:

  • Dot notation

  • Bracket notation


Dot Notation

This is the most common method.

Example:

let person = {
  name: "Pankaj",
  age: 25
};

console.log(person.name);

Output

Pankaj

Bracket Notation

We can also access properties using brackets.

Example:

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

Output

25

Difference Between Dot and Bracket

Method Example
Dot notation person.name
Bracket notation person["name"]

Bracket notation is useful when the property name is dynamic.

Example:

let key = "name";

console.log(person[key]);

4. Updating Object Properties

We can change property values easily.

Example:

let person = {
  name: "Pankaj",
  age: 25
};

person.age = 26;

console.log(person);

Output

{ name: "Pankaj", age: 26 }

Here we updated age from 25 → 26.


5. Adding New Properties

We can add properties anytime.

Example:

let person = {
  name: "Pankaj"
};

person.city = "Patna";

console.log(person);

Output

{ name: "Pankaj", city: "Patna" }

6. Deleting Properties

We can remove properties using delete.

Example:

let person = {
  name: "Pankaj",
  age: 25
};

delete person.age;

console.log(person);

Output

{ name: "Pankaj" }

7. Looping Through Object Keys

To access all properties, we can use a for...in loop.

Example:

let person = {
  name: "Pankaj",
  age: 25,
  city: "Patna"
};

for (let key in person) {
  console.log(key, person[key]);
}

Output

name Pankaj
age 25
city Patna

Explanation:

key → property name
person[key] → value

Array vs Object

Sometimes beginners get confused between arrays and objects.

Feature Array Object
Structure Ordered list Key-value pairs
Access Index Key
Example [10,20,30] {name:"Pankaj"}

Example

Array:

let fruits = ["Apple", "Banana", "Mango"];

console.log(fruits[0]);

Object:

let person = {
  name: "Pankaj"
};

console.log(person.name);

Visual Comparison

Array

[ Apple , Banana , Mango ]
   0        1        2
Object

{
 name : "Pankaj"
 age  : 25
 city : "Patna"
}

Arrays use index numbers, objects use keys.


Practical Example

let student = {
  name: "Rahul",
  age: 21,
  course: "Computer Science"
};

console.log(student.name);
console.log(student.course);

Output

Rahul
Computer Science

Assignment

Open your console:

Right Click → Inspect → Console

1️⃣ Create a Student Object

let student = {
  name: "Amit",
  age: 20,
  course: "BCA"
};

2️⃣ Update One Property

student.age = 21;

3️⃣ Add a New Property

student.city = "Delhi";

4️⃣ Print All Keys and Values

for (let key in student) {
  console.log(key, student[key]);
}

Expected output example

name Amit
age 21
course BCA
city Delhi

Final Tip

Objects are one of the most important data structures in JavaScript.

They are used everywhere, including:

  • user profiles

  • API responses

  • configurations

  • databases

  • real-world application data

Mastering objects will make JavaScript much more powerful and practical for you.