Understanding Objects in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators. If you’ve ever done basic ma

When learning JavaScript, you’ll eventually encounter a keyword that can feel confusing at first: this. The value of this changes depending on how a function is called. JavaScript also provides specia

Functions are one of the most important concepts in JavaScript. They help you organize your code and avoid repeating the same logic again and again. In JavaScript, there are multiple ways to create fu

When writing JavaScript programs, you often need to store multiple values together. For example, imagine you want to store a list of fruits: Apple Mango Banana Orange You could store them like t

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programmi

ygshjm
24 posts
As your JavaScript programs grow, you often need to store related pieces of information together.
For example, imagine storing details about a person:
Name
Age
City
You could store them in separate variables, but that quickly becomes messy. This is where objects become useful.
Objects allow you to group related data together in a structured way.
An object in JavaScript is a collection of key–value pairs.
Here:
Key → the label (name, age, city)
Value → the actual data stored
Instead of storing everything separately like this:
let name = "Rahul";
let age = 25;
let city = "Guwahati";
You can group them into one object:
let person = {
name: "Rahul",
age: 25,
city: "Guwahati"
};
Now all related information is stored in one structure.
Objects are created using curly braces {}.
Inside the braces, we write key-value pairs separated by commas.
Example:
let person = {
name: "Aman",
age: 22,
city: "Delhi"
};
In this object:
name is a key and "Aman" is its value
age is a key and 22 is its value
city is a key and "Delhi" is its value
You can think of an object as a container that organizes related information.
Once an object is created, you often want to read the values stored inside it.
There are two common ways to access object properties.
This is the most common and readable way.
let person = {
name: "Aman",
age: 22,
city: "Delhi"
};
console.log(person.name);
console.log(person.age);
Output:
Aman
22
Here we access properties using:
object.property
Another way is using square brackets.
console.log(person["city"]);
Output:
Delhi
Bracket notation is useful when the property name is stored in a variable.
Example:
let key = "name";
console.log(person[key]);
Object values can be updated anytime.
Example:
let person = {
name: "Aman",
age: 22,
city: "Delhi"
};
person.age = 23;
Now the object becomes:
{
name: "Aman",
age: 23,
city: "Delhi"
}
This is useful when information changes over time.
For example:
updating a user's age
changing a user's city
updating account information
Objects are flexible. You can add new properties or remove existing ones.
You can add a new property simply by assigning a value.
let person = {
name: "Aman",
age: 22
};
person.city = "Delhi";
Now the object becomes:
{
name: "Aman",
age: 22,
city: "Delhi"
}
To remove a property, use the delete keyword.
delete person.age;
Now the object becomes:
{
name: "Aman",
city: "Delhi"
}
Sometimes you need to go through every property inside an object.
JavaScript provides a loop called for...in for this purpose.
Example:
let person = {
name: "Aman",
age: 22,
city: "Delhi"
};
for (let key in person) {
console.log(key, person[key]);
}
Output:
name Aman
age 22
city Delhi
Here:
key represents the property name
person[key] gives its value
This is useful when you want to process or display all object data.
Beginners often confuse arrays and objects because both store multiple values.
The key difference is how the values are organized.
Arrays store values using numeric indexes.
Example:
let colors = ["red", "green", "blue"];
Accessing values:
colors[0]; // red
Objects store values using named keys.
Example:
let person = {
name: "Aman",
age: 22
};
Accessing values:
person.name; // Aman
So in simple terms:
Arrays → ordered lists
Objects → structured data with named properties