JavaScript Objects Explained Simply
When working with JavaScript, we often need to store related pieces of information together.
For example, imagine we want to store information about a person:
Name
Age
City
We could store them in separate variables:
let name = "Alex";
let age = 25;
let city = "Mumbai";
But this becomes messy as the program grows. A better way is to group related data together.
This is where objects come in.
In this article, we’ll learn:
What objects are and why they are useful
How to create objects
How to access properties (dot notation and bracket notation)
How to update object properties
How to add and delete properties
How to loop through object keys
All examples are simple and beginner-friendly, so you can try them in your browser console or Node.js.
What Are Objects?
An object is a way to store data in key-value pairs.
Think of it like a dictionary:
| Key | Value |
|---|---|
| name | "Alex" |
| age | 25 |
| city | "Mumbai" |
Each piece of information has:
a key (the label)
a value (the data)
In JavaScript, an object looks like this:
let person = {
name: "Alex",
age: 25,
city: "Mumbai"
};
Here:
name,age,city→ keys"Alex",25,"Mumbai"→ values
Together they form a person object.
Objects vs Arrays
Before going further, it’s helpful to understand the difference between arrays and objects.
Array
Arrays store values using index numbers.
let fruits = ["apple", "banana", "mango"];
Accessing elements:
fruits[0]; // apple
Object
Objects store values using keys.
let person = {
name: "Alex",
age: 25
};
Accessing elements:
person.name; // Alex
So the key difference:
Arrays → index based
Objects → key based
Creating Objects
Objects are created using curly braces {}.
Example:
let person = {
name: "Rahul",
age: 22,
city: "Kolkata"
};
console.log(person);
Output:
{ name: 'Rahul', age: 22, city: 'Kolkata' }
This object stores three properties.
Accessing Object Properties
There are two ways to access object properties:
Dot notation
Bracket notation
Dot Notation
This is the most common and simplest way.
let person = {
name: "Rahul",
age: 22,
city: "Kolkata"
};
console.log(person.name);
console.log(person.age);
Output:
Rahul
22
We simply write:
object.property
Bracket Notation
Another way is using square brackets.
console.log(person["city"]);
Output:
Kolkata
Bracket notation is useful when the property name is stored in a variable.
Example:
let key = "name";
console.log(person[key]);
Output:
Rahul
Updating Object Properties
We can change the value of an existing property easily.
Example:
let person = {
name: "Rahul",
age: 22
};
person.age = 23;
console.log(person.age);
Output:
23
Here we updated the age from 22 to 23.
Adding New Properties
We can also add new properties to an object.
Example:
let person = {
name: "Rahul",
age: 22
};
person.city = "Delhi";
console.log(person);
Output:
{ name: 'Rahul', age: 22, city: 'Delhi' }
The object now contains a new property called city.
Deleting Properties
We can remove properties using the delete keyword.
Example:
let person = {
name: "Rahul",
age: 22,
city: "Delhi"
};
delete person.city;
console.log(person);
Output:
{ name: 'Rahul', age: 22 }
The city property was removed.
Looping Through Object Keys
Sometimes we want to loop through all properties of an object.
We can do this using a for...in loop.
Example:
let person = {
name: "Rahul",
age: 22,
city: "Kolkata"
};
for (let key in person) {
console.log(key, person[key]);
}
Output:
name Rahul
age 22
city Kolkata
How it works:
keyrepresents each property nameperson[key]gives the value
So the loop goes through every key-value pair.
Objects are one of the most important concepts in JavaScript because they help us organize related data.
Key takeaways:
Objects store data in key-value pairs
They are created using
{}Properties can be accessed using dot notation or bracket notation
You can update, add, or delete properties
You can loop through object properties using for...in
