DEV Community

Cover image for Understanding Objects in JavaScript
Souvik Guha Roy
Souvik Guha Roy

Posted on

Understanding Objects in JavaScript

Objects are one of the most important concepts in JavaScript. Almost everything in JavaScript eventually revolves around objects.

If arrays help us store lists of data, objects help us store structured data with meaningful labels.

In this blog, we’ll learn what objects are, why they are useful, and how to work with them in JavaScript.


Topics Covered

In this guide we will learn:

  • What objects are and why they are needed
  • How to create objects
  • How to access object properties
  • Updating object properties
  • Adding and deleting properties
  • Looping through object keys

What Are Objects?

An object is a data structure used to store related information together using key–value pairs.

Each piece of data inside an object has:

  • Key → the name of the property
  • Value → the data stored in that property

Example:

let person = {
  name: "John",
  age: 25,
  city: "New York"
};
Enter fullscreen mode Exit fullscreen mode

Here:

Key Value
name John
age 25
city New York

This structure makes it easier to organize related data.


Why Do We Need Objects?

Objects are useful because they allow us to:

  • Store related data together
  • Access data using meaningful property names
  • Represent real-world entities easily

For example, a person has:

  • name
  • age
  • city

Instead of creating separate variables, we can store everything in one object.


Creating Objects in JavaScript

Objects are created using curly braces {}.

Example:

let person = {
  name: "Alice",
  age: 22,
  city: "London"
};
Enter fullscreen mode Exit fullscreen mode

Each property is written as:

key: value
Enter fullscreen mode Exit fullscreen mode

Properties are separated using commas.


Accessing Object Properties

We can access object properties in two ways:

  1. Dot notation
  2. Bracket notation

Dot Notation

This is the most common way to access properties.

Example:

let person = {
  name: "Alice",
  age: 22,
  city: "London"
};

console.log(person.name);
console.log(person.age);
Enter fullscreen mode Exit fullscreen mode

Output:

Alice
22
Enter fullscreen mode Exit fullscreen mode

Bracket Notation

We can also access properties using square brackets.

Example:

console.log(person["city"]);
Enter fullscreen mode Exit fullscreen mode

Output:

London
Enter fullscreen mode Exit fullscreen mode

Bracket notation is useful when the property name is dynamic.

Example:

let key = "name";

console.log(person[key]);
Enter fullscreen mode Exit fullscreen mode

Updating Object Properties

We can easily update a property by assigning a new value.

Example:

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

person.age = 23;

console.log(person.age);
Enter fullscreen mode Exit fullscreen mode

Output:

23
Enter fullscreen mode Exit fullscreen mode

Adding New Properties

New properties can be added simply by assigning a value.

Example:

let student = {
  name: "Rahul",
  age: 20
};

student.course = "Computer Science";

console.log(student);
Enter fullscreen mode Exit fullscreen mode

Now the object becomes:

{
  name: "Rahul",
  age: 20,
  course: "Computer Science"
}
Enter fullscreen mode Exit fullscreen mode

Deleting Properties

We can remove properties using the delete keyword.

Example:

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

delete student.age;

console.log(student);
Enter fullscreen mode Exit fullscreen mode

Output:

{
  name: "Rahul",
  course: "Computer Science"
}
Enter fullscreen mode Exit fullscreen mode

Looping Through Object Keys

Sometimes we want to iterate through all properties of an object.

We can do this using a for...in loop.

Example:

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

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

Output:

name Rahul
age 20
course Computer Science
Enter fullscreen mode Exit fullscreen mode

Here:

  • key represents the property name
  • student[key] gives the corresponding value

Difference Between Array and Object

Both arrays and objects store data, but they work differently.

Feature Array Object
Structure Ordered list Key–value pairs
Access method Index (0,1,2...) Property name
Use case Storing lists Representing structured data

Example:

Array:

let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits[0]);
Enter fullscreen mode Exit fullscreen mode

Object:

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

console.log(person.name);
Enter fullscreen mode Exit fullscreen mode

Assignment Example

Let’s create a student object and perform some operations.

let student = {
  name: "Kunal",
  age: 21,
  course: "Web Development"
};

// update property
student.age = 22;

// loop through object
for (let key in student) {
  console.log(key + ":", student[key]);
}
Enter fullscreen mode Exit fullscreen mode

Output:

name: Kunal
age: 22
course: Web Development
Enter fullscreen mode Exit fullscreen mode

Visualizing an Object

You can imagine an object like a labeled container.

student
 ├── name   → "Kunal"
 ├── age    → 21
 └── course → "Web Development"
Enter fullscreen mode Exit fullscreen mode

Each key points to a specific value.


Final Thoughts

Objects are one of the core building blocks of JavaScript.

They allow us to represent real-world entities and organize data in a structured way.

In this article we learned how to:

  • Create objects
  • Access properties
  • Update values
  • Add and delete properties
  • Loop through object data

Once you understand objects well, many advanced JavaScript concepts become easier to learn.


Top comments (0)