webdevCohort2026ChaiCodeJavaScript

What Are JavaScript Objects? A Simple Guide for Beginners

Hello everyone! If you are learning JavaScript, you will run into Objects all the time. It is one of the most important core concepts in the language…

Kenil Patel
Kenil PatelFull Stack Engineer
What Are JavaScript Objects? A Simple Guide for Beginners

Hello everyone! If you are learning JavaScript, you will run into Objects all the time. It is one of the most important core concepts in the language. Today, we are going to understand what objects are, how to create them, and why we need them. Let's decode this in simple language without any complex jargon!

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

Imagine the real world. If we talk about a "Person", they have certain properties—like their name, their age, and the city they live in. In JavaScript, we use Objects to represent these real-world entities.

An object is a data structure that stores data in Key-Value pairs.

  • Key: The name of the property (e.g., name)

  • Value: The detail of that property (e.g., "Rahul")

Why do we need Objects? If we wanted to store all the data of a single person using variables, our code would get very messy very quickly (let name = "Rahul", let age = 22, etc.). Objects pack all this related data into a single, organized bundle, making it much easier to manage and read.


2. Array vs. Object: What’s the Difference? ️

Beginners often get confused about when to use an Array and when to use an Object. Think of it like this:

  • Array ([]): This is like a list where the order matters. Each item is stored at a numbered index (0, 1, 2...). (e.g., ["Apple", "Banana", "Mango"])

  • Object ({}): This is like a dictionary. Here, the order doesn't matter, but the "keys" (labels) do. You can easily read the label to know what the value represents.

If you just need a list of items, use an Array. But if you need to describe detailed data with specific properties, an Object is the best choice!


3. Creating Objects ️

Creating an object in JavaScript is very simple. We use curly braces {}. Let's create a Person object:

JavaScript

const person = {
  name: "Aman",
  age: 22,
  city: "Delhi"
};

console.log(person);

That's it! This object has name, age, and city keys, followed by a colon (:), and then their respective values.


4. Accessing Object Properties (Dot & Bracket Notation)

Now that we have created our object, how do we read just the person's name? There are two ways to do this:

A. Dot Notation (The most common way): Simply write the object name, put a dot (.), and then the property name.

JavaScript

console.log(person.name); // Output: Aman
console.log(person.city); // Output: Delhi

B. Bracket Notation: Here, we write the key name inside square brackets [] as a string (in quotes). This is very useful when your key has spaces (like "first name") or when the key is stored inside a variable.

JavaScript

console.log(person["age"]); // Output: 22

let dynamicKey = "city";
console.log(person[dynamicKey]); // Output: Delhi

5. Updating Object Properties ️

Objects are flexible. If Aman has a birthday and turns 23, we can easily update his age:

JavaScript

// Updating using Dot notation
person.age = 23;

// Updating using Bracket notation
person["city"] = "Mumbai";

console.log(person); 
// Output: { name: "Aman", age: 23, city: "Mumbai" }

6. Adding and Deleting Properties ️

You can add new details or remove old ones even after the object has been created.

To Add:

JavaScript

person.country = "India"; // A new property is added

To Delete: If we want to remove the city entirely, we use the delete keyword:

JavaScript

delete person.city;
console.log(person); 
// Output: { name: "Aman", age: 23, country: "India" }

7. Looping Through Object Keys

If your object has 50 properties and you want to print them all one by one, we use a for...in loop. This loop goes through every single key inside the object:

JavaScript

const car = {
  brand: "Tata",
  model: "Nexon",
  color: "White"
};

for (let key in car) {
  // `key` will hold the property name
  // `car[key]` will fetch its value
  console.log(key + " : " + car[key]);
}

/* Output:
brand : Tata
model : Nexon
color : White
*/

Note: We used bracket notation car[key] here because key is a variable!