Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript (Beginner Guide)

Learn how to create, access, update, and loop through objects in JavaScript with simple real-world examples.

Updated
3 min read
Understanding Objects in JavaScript (Beginner Guide)
S

Hi, I’m Shubham 👋 A learner sharing my journey in programming, tech, and self-growth. Learning every day and writing what I learn.

In JavaScript, objects are one of the most important concepts.

They help us store related data together in a structured way.

Let’s understand objects in the simplest way possible.


What Are Objects?

An object is a collection of key-value pairs.

Think of an object like a real-life person’s profile:

  • name → "Shubham"

  • age → 22

  • city → "Patna"

Instead of storing these values separately, we group them inside one object.


Why Do We Need Objects?

Imagine storing person details like this:

let name = "Shubham";
let age = 22;
let city = "Patna";

This becomes messy for large data.

Instead, we use an object:

let person = {
  name: "Shubham",
  age: 22,
  city: "Patna"
};

Now everything is organized inside one structure. ✅


Creating Objects

Basic syntax:

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

Example:

let person = {
  name: "Shubham",
  age: 22,
  city: "Patna"
};

Accessing Object Properties

There are two ways:


Dot Notation

console.log(person.name);

Simple and clean.


Bracket Notation

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

Useful when property name is dynamic.


Updating Object Properties

person.age = 23;
console.log(person.age);

The value gets updated.


Adding New Properties

person.country = "India";
console.log(person);

Deleting Properties

delete person.city;
console.log(person);

Looping Through Object Keys

We use for...in loop.

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

This prints:

  • key

  • value


Object vs Array (Simple Comparison)

Feature Object Array
Structure Key-value pairs Indexed values
Access By key By index
Use case Related data Ordered list

Example Array:

let colors = ["red", "blue", "green"];
console.log(colors[0]);

Example Object:

let person = {
  name: "Shubham",
  age: 22
};
console.log(person.name);

👉 Array → position-based

👉 Object → name-based


Visual Representation of Object

person
  ├── name: "Shubham"
  ├── age: 22
  └── city: "Patna"

Each key connects to a value.


Assignment Practice

Create a Student Object

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

Update One Property

student.age = 21;

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

Final Thoughts

Objects help us:

  • Organize related data

  • Access values easily

  • Update and manage information efficiently

They are everywhere in JavaScript — APIs, JSON data, configuration, user data, etc.

Mastering objects is a big step toward becoming a strong JavaScript developer.