Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

Updated
4 min read

JavaScript is one of the most popular programming languages used for web development. While learning JavaScript, one of the most important concepts you will come across is Objects.

Objects help us store and organize related data together. In this article, we will understand what objects are, why they are useful, and how to work with them in JavaScript using simple examples.


What are Objects in JavaScript?

An object in JavaScript is a collection of key-value pairs.

  • A key (also called a property name) identifies the data.

  • A value is the actual data stored.

Think of an object like a real-world person profile.

Example:

Name: John
Age: 22
City: Mumbai

Here:

  • Name, Age, and City are keys

  • John, 22, Mumbai are values

In JavaScript, we can represent this as an object.

let person = {
  name: "John",
  age: 22,
  city: "Mumbai"
};

This object stores all information related to a person in one place.

Why Do We Need Objects?

Objects help us group related information together.

Without objects, we might store data like this:

let name = "John";
let age = 22;
let city = "Mumbai";

But these variables are separate and harder to manage.

Using an object:

let person = {
  name: "John",
  age: 22,
  city: "Mumbai"
};

Now all related data is organized inside one structure.

This makes code:

  • easier to read

  • easier to manage

  • more scalable

Creating Objects in JavaScript

Objects are created using curly braces {}.

Example:

let person = {
  name: "John",
  age: 22,
  city: "Mumbai"
};

Structure:

{
  key: value,
  key: value
}

Accessing Object Properties

We can access object values in two ways.

1. Dot Notation

The most common method.

console.log(person.name);
console.log(person.age);

Output:

John
22

2. Bracket Notation

This method uses square brackets.

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

Output:

Mumbai

Bracket notation is useful when the property name is stored in a variable.

Example:

let key = "name";
console.log(person[key]);

Updating Object Properties

We can easily update values inside an object.

Example:

person.age = 23;

console.log(person.age);

Output:

23

Here we updated the age property.

Adding New Properties

We can also add new properties to an object.

Example:

person.country = "India";

console.log(person);

Now the object becomes:

{
  name: "John",
  age: 23,
  city: "Mumbai",
  country: "India"
}

Deleting Object Properties

If we want to remove a property, we use the delete keyword.

Example:

delete person.city;

console.log(person);

Now the object becomes:

{
  name: "John",
  age: 23,
  country: "India"
}

Looping Through Object Keys

We can loop through object properties using a for...in loop.

Example:

let person = {
  name: "John",
  age: 22,
  city: "Mumbai"
};

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

Output:

name : John
age : 22
city : Mumbai

Here:

  • key represents the property name

  • person[key] gives the value

Difference Between Arrays and Objects

Both arrays and objects store data, but they are used differently.

Feature Array Object
Structure Ordered list Key-value pairs
Access Index Key
Example ["apple", "banana"] {name: "John"}

Example:

Array:

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

console.log(fruits[0]);

Output:

Apple

Object:

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

console.log(person.name);

Output:

John

Objects are one of the most powerful features of JavaScript. They allow us to organize data in a clear and structured way.

In this article we learned:

  • What objects are

  • How to create objects

  • Accessing properties

  • Updating values

  • Adding and deleting properties

  • Looping through object keys

  • Difference between arrays and objects

Mastering objects will make it much easier to work with real-world applications, APIs, and complex data structures in JavaScript.