Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript (Explained Simply)

Updated
4 min readView as Markdown

When we write programs, we often need to store many details about one thing.

Example: A person.

A person can have:

  • name

  • age

  • city

  • job

We could store these in separate variables, but that becomes messy.

Example:

let name = "Ravi";
let age = 25;
let city = "Hyderabad";

Instead, JavaScript gives us a better way to group related information.

That is called an Object.


1. What is an Object?

An object is a collection of key-value pairs.

Think of it like an ID card.

Name → Ravi
Age → 25
City → Hyderabad

In JavaScript:

let person = {
  name: "Ravi",
  age: 25,
  city: "Hyderabad"
};

Here:

  • name, age, citykeys

  • "Ravi", 25, "Hyderabad"values

So an object stores information about something in an organized way.


2. Creating an Object

We create objects using curly braces {}.

Example:

let person = {
  name: "Arjun",
  age: 22,
  city: "Delhi"
};

This object contains three properties.

You can print the entire object:

console.log(person);

Output:

{ name: "Arjun", age: 22, city: "Delhi" }

3. Accessing Object Properties

We can access values in two ways:

1️⃣ Dot Notation
2️⃣ Bracket Notation


Dot Notation

Example:

let person = {
  name: "Arjun",
  age: 22,
  city: "Delhi"
};

console.log(person.name);

Output:

Arjun

Here we accessed the name property.


Bracket Notation

Example:

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

Output:

22

Bracket notation is useful when the key is stored in a variable.

Example:

let key = "city";

console.log(person[key]);

Output:

Delhi

4. Updating Object Properties

We can change values inside an object.

Example:

let person = {
  name: "Arjun",
  age: 22,
  city: "Delhi"
};

person.age = 23;

console.log(person.age);

Output:

23

The value changed from 22 → 23.


5. Adding New Properties

We can also add new properties to an object.

Example:

let person = {
  name: "Arjun",
  age: 22
};

person.city = "Hyderabad";

console.log(person);

Output:

{ name: "Arjun", age: 22, city: "Hyderabad" }

A new property city was added.


6. Deleting Properties

We can remove properties using the delete keyword.

Example:

let person = {
  name: "Arjun",
  age: 22,
  city: "Hyderabad"
};

delete person.city;

console.log(person);

Output:

{ name: "Arjun", age: 22 }

The city property was removed.


7. Looping Through Object Keys

Sometimes we want to print all properties of an object.

We can use a for...in loop.

Example:

let person = {
  name: "Arjun",
  age: 22,
  city: "Hyderabad"
};

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

Output:

name Arjun
age 22
city Hyderabad

Here:

  • key → property name

  • person[key] → value


Object vs Array (Important Difference)

Arrays and objects both store data, but they work differently.

Array

Array stores values using index numbers.

Example:

let fruits = ["apple", "banana", "mango"];

Access using index:

fruits[0]

Object

Object stores values using keys.

Example:

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

Access using key:

person.name

Simple Comparison

Feature Array Object
Data Access Index Key
Structure Ordered list Key-value pairs
Example fruits person details

Visual Representation of an Object

person
│
├── name → Ravi
├── age → 25
└── city → Hyderabad

Each key is connected to a value.


Assignment Practice

Try this in your browser console.


1. Create a Student Object

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

2. Update One Property

student.age = 22;

console.log(student);

Output:

{ name: "Rahul", age: 22, course: "Computer Science" }

3. Print All Keys and Values

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

Output:

name Rahul
age 22
course Computer Science