Skip to main content

Understanding Objects in JavaScript

Updated 8 min readMarkdown
Part of seriesJavascript
Understanding Objects in JavaScript

Objects are one of the most important concepts in JavaScript. In real applications, most of the data we work with is stored inside objects. When you understand objects properly, it becomes much easier to build applications, manage data, and organize your code.

In this article we will learn:

  • What objects are and why they are needed

  • How to create objects

  • How to access object properties

  • How to update properties

  • How to add and delete properties

  • How to loop through object keys and values

The examples will be simple so that beginners can clearly understand the concept.


1. What Are Objects and Why Do We Need Them?

In programming, we often need to store information about something. That “something” could be a person, a student, a product, a car, or any real-world entity.

Let’s imagine we want to store information about a person.

A person may have:

  • name

  • age

  • city

  • profession

If we store them separately, it may look like this:

let name = "Rahul";
let age = 25;
let city = "Delhi";

This works, but there is a problem.

All these variables belong to the same person, but they are stored separately. As the program grows, it becomes difficult to manage related data.

This is where objects help us.

An object allows us to group related information together in one structure.

Example:

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

Now all the information related to the person is stored inside a single object.

So an object is a unordered collection of data stored in key-value pairs.


2. Understanding Key-Value Pair Structure

Objects store data in a key-value format.

The structure looks like this:

key : value

Example:

let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2022
};

Here:

  • brand is the key

  • "Toyota" is the value

  • model is the key

  • "Corolla" is the value

  • year is the key

  • 2022 is the value

We can visualize the object like this:

car object
-------------------------
brand  →  Toyota
model  →  Corolla
year   →  2022

Keys act like labels, and values store the actual data.


3. Difference Between Array and Object

Both arrays and objects can store multiple values, but they work in different ways.

Array Example

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

Arrays store values using indexes.

Index    Value
0        Apple
1        Mango
2        Banana

To access values we use indexes:

console.log(fruits[0]);

Output:

Apple

Object Example

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

Objects store values using keys instead of indexes.

Key     Value
name    Rahul
age     25

To access values we use keys:

console.log(person.name);

Output:

Rahul

Simple Comparison

Feature Array Object
Structure Ordered list of values Key-value pairs
Access method Index (0,1,2) Key name
Example ["A","B","C"] {name:"Rahul"}
Best used for Lists of items Representing real-world entities

Arrays are best for collections of items, while objects are best for describing something with properties.


4. Creating Objects

Objects in JavaScript are created using curly braces {}.

Basic syntax:

let objectName = {
  key1: value1,
  key2: value2
};

Example:

let person = {
  name: "Amit",
  age: 28,
  city: "Mumbai"
};

Here:

  • person is the object name

  • name, age, city are properties (keys)

  • "Amit", 28, "Mumbai" are values

Each key-value pair is separated by a comma.

Objects can store many types of values:

let student = {
  name: "Ankit",
  age: 20,
  isEnrolled: true
};

Values can be:

  • strings

  • numbers

  • booleans

  • arrays

  • even other objects

But for now we will keep things simple.


5. Accessing Object Properties

Once an object is created, we often need to read the values stored inside it.

JavaScript provides two ways to access object properties:

  1. Dot notation

  2. Bracket notation

Dot Notation

Dot notation is the most common and readable way to access properties.

Syntax:

objectName.propertyName

Example:

let person = {
  name: "Amit",
  age: 28,
  city: "Mumbai"
};

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

Output:

Amit
28

Here we accessed the name and age properties using dot notation.

This method works well when the property name is known and simple.

Bracket Notation

The second method uses square brackets.

Syntax:

objectName["propertyName"]

Example:

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

Output:

Mumbai

The property name must be written as a string inside the brackets.


When Bracket Notation Is Useful

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

Example:

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

let key = "name";

console.log(person[key]);

Output:

Rahul

It is also useful when property names contain spaces.

Example:

let car = {
  "car brand": "Toyota"
};

console.log(car["car brand"]);

Dot notation cannot be used in this case.


6. Updating Object Properties

Updating a property means changing its value.

Example:

let person = {
  name: "Amit",
  age: 28
};

person.age = 30;

console.log(person.age);

Output:

30

Here we changed the value of the age property from 28 to 30.

You can also update properties using bracket notation.

Example:

person["name"] = "Rohit";

Now the name value will be updated.


7. Adding New Properties

Objects in JavaScript are dynamic, which means we can add new properties anytime.

Example:

let person = {
  name: "Amit",
  age: 28
};

person.city = "Pune";

console.log(person);

Output:

{ name: "Amit", age: 28, city: "Pune" }

The city property did not exist earlier, but JavaScript allows us to add it easily.

This flexibility makes objects very useful when working with real data.


8. Deleting Object Properties

Sometimes we may want to remove a property from an object.

JavaScript provides the delete keyword for this.

Example:

let person = {
  name: "Amit",
  age: 28,
  city: "Mumbai"
};

delete person.city;

console.log(person);

Output:

{ name: "Amit", age: 28 }

The city property has been removed from the object.


9. Looping Through Object Keys

Sometimes an object may have many properties, and we want to access all of them.

JavaScript provides a loop called for...in for this purpose.

Example:

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

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

Output:

name
age
city

This loop goes through all keys inside the object.


Printing Keys and Values

We can also print both keys and values.

Example:

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 of that property

This is a common pattern used when working with objects.


Assignment Practice

Try the following tasks to practice objects.

1. Create a Student Object

Create an object that represents a student.

Properties:

  • name

  • age

  • course

Example:

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

2. Update One Property

Change the course value.

Example:

student.course = "Information Technology";

3. Add a New Property

Add a property called city.

Example:

student.city = "Indore";

4. Print All Keys and Values

Use a loop to print all properties.

Example:

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

Summary

Objects are a fundamental concept in JavaScript. They allow us to organize related data in a structured way using key-value pairs.

In this article we learned:

  • Objects group related data together

  • Objects store data using keys and values

  • Properties can be accessed using dot or bracket notation

  • Values can be updated, added, or deleted

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

Once you become comfortable working with objects, you will find that many JavaScript features and real-world applications rely heavily on them.

No comments yet. Be the first to comment.

Javascript

Part 3 of 12

This series explains core JavaScript concepts in a clear and beginner-friendly way. Each article focuses on one important topic with simple explanations, practical examples, and real development use cases. The goal is to make JavaScript easier to understand for developers who are building their foundation in web development.

Up nextUnderstanding Object-Oriented Programming in JavaScriptAs programs grow bigger, organizing code properly becomes very important. Writing everything in random variables and functions can quickly become difficult to manage. To solve this problem, programmin