Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

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

In JavaScript, we often need to store related information together.

For example, imagine storing details about a person:

  • Name

  • Age

  • City

If we use separate variables, the code becomes messy.
That’s where Objects come in.

Objects help us store data in a structured way using key–value pairs.

Let’s understand them step by step.


1. What objects are and why they are needed

An object is a collection of key–value pairs.

Each key represents a property name, and each value stores the data.

Example of a person object:

const person = {
  name: "Rahul",
  age: 25,
  city: "Mumbai"
};

Here:

  • name, age, city → keys

  • "Rahul", 25, "Mumbai" → values

In simple words:

Object = structured data using key–value pairs

Objects are useful when we want to store multiple related pieces of information together.


2. Creating objects

In JavaScript, objects are usually created using curly braces {}.

Example:

const person = {
  name: "Amit",
  age: 30,
  city: "Pune"
};

This object stores three properties about a person.

Objects can store different types of values:

  • Strings

  • Numbers

  • Arrays

  • Even functions

Example:

const car = {
  brand: "Toyota",
  model: "Innova",
  year: 2022
};

Difference between Array and Object

It is important to understand how arrays and objects differ.

Array example:

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

Array stores data using index numbers.

Example:

fruits[0] → apple

Object example:

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

Objects store data using keys instead of indexes.

Example:

person.name → Rahul

Short idea:

  • Array → index-based data

  • Object → key-value data


3. Accessing properties (dot notation and bracket notation)

We can access object properties in two ways.

Dot notation

Example:

const person = {
  name: "Rahul",
  age: 25
};

console.log(person.name);

Output:

Rahul

Dot notation is the most common way.


Bracket notation

Example:

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

Output:

25

Bracket notation is useful when:

  • Property names are dynamic

  • Keys contain spaces


4. Updating object properties

We can easily update values inside an object.

Example:

const person = {
  name: "Rahul",
  age: 25
};

person.age = 26;

console.log(person);

Output:

{ name: "Rahul", age: 26 }

So the property value gets updated.


5. Adding and deleting properties

Adding properties

We can add new properties anytime.

Example:

const person = {
  name: "Rahul",
  age: 25
};

person.city = "Delhi";

console.log(person);

Output:

{ name: "Rahul", age: 25, city: "Delhi" }

Deleting properties

We use the delete keyword.

Example:

delete person.age;

console.log(person);

Output:

{ name: "Rahul", city: "Delhi" }

6. Looping through object keys

To go through all properties of an object, we can use a for...in loop.

Example:

const person = {
  name: "Rahul",
  age: 25,
  city: "Delhi"
};

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

Output:

name Rahul
age 25
city Delhi

Here:

  • key represents the property name

  • person[key] gives the value

This helps us iterate through all object properties.


Conclusion

Objects are one of the most important concepts in JavaScript.

They allow us to store related data in a structured way using key–value pairs.

Key things to remember:

  • Objects store data using keys and values

  • Created using curly braces {}

  • Properties can be accessed using dot or bracket notation

  • Properties can be updated, added, or deleted

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

Once you understand objects, working with real-world data in JavaScript becomes much easier.
Thank you for giving your valuable time .....