Understanding Objects in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
Introduction If you've ever built something with vanilla JavaScript and had to update the UI frequently, you already know the pain. You call document.getElementById, change some text, update a class,
Introduction If you've ever installed and tried out Linux after seeing a cool hacking sequence in a sci-fi movie, you know that opening it feels like stepping into a strange city with many roads named

Introduction Most JavaScript confusion around this comes from one wrong assumption: that this depends on where a function is defined. It doesn't. It depends on who called the function. That one shift

Introduction JavaScript gives you objects and arrays out of the box, and for a long time those two were the default answer to almost every data storage problem. Objects for key-value pairs, arrays for

Introduction Most JavaScript codebases are full of code like this: const user = { name: "Sara", age: 28, city: "Mumbai" }; const name = user.name; const age = user.age; const city = user.city; Three

If you've written any JavaScript, you've probably used an array to store a list of values. Arrays are great for ordered data. But what if you need to describe something, not just list things?
That's where objects come in.
An object in JavaScript is a collection of key-value pairs. Each key is a string (called a property), and each value can be anything: a number, a string, a boolean, even another object or a function.
Think of it like a contact card. A contact card doesn't just hold a list, it holds labeled information: a name, a phone number, a city. That labeled structure is exactly what an object gives you.
Here's the difference made concrete:
// An array: ordered, but no labels
const person = ["Alice", 30, "Mumbai"];
// An object: labeled, descriptive
const person = {
name: "Alice",
age: 30,
city: "Mumbai"
};
With the array, you have to remember that index 0 is the name and index 2 is the city. With the object, the data explains itself.
The most common way to create an object is with curly braces, also called object literal syntax.
const person = {
name: "Alice",
age: 30,
city: "Mumbai"
};
Each line inside is a property. The key is on the left, the value is on the right, and they're separated by a colon. Properties are separated by commas.
You can also create an empty object and add properties later:
const person = {};
person.name = "Alice";
person.age = 30;
Both approaches are valid. Object literals are more common when you know the data upfront.
There are two ways to read a property from an object.
console.log(person.name); // "Alice"
console.log(person.age); // 30
This is the standard way. Clean, readable, and works most of the time.
console.log(person["name"]); // "Alice"
console.log(person["city"]); // "Mumbai"
Bracket notation is useful when the property name is stored in a variable:
const key = "age";
console.log(person[key]); // 30
You can't do that with dot notation. person.key would look for a property literally named "key", not the value stored in the variable.
To change a property's value, just reassign it:
person.age = 31;
console.log(person.age); // 31
Works the same way with bracket notation:
person["city"] = "Pune";
console.log(person.city); // "Pune"
There's nothing special about updating vs. setting. If the property exists, the value gets replaced. If it doesn't exist, the property gets created.
You can add new properties to an object at any time, even after it's been created:
person.email = "alice@example.com";
console.log(person.email); // "alice@example.com"
To remove a property, use the delete keyword:
delete person.city;
console.log(person.city); // undefined
After deletion, accessing that property returns undefined. The key is gone.
If you want to go through all the properties in an object, for...in is the most direct way:
for (let key in person) {
console.log(key, ":", person[key]);
}
// name : Alice
// age : 31
// email : alice@example.com
On each iteration, key holds the property name as a string. person[key] gives you the value for that property. Notice bracket notation here; dot notation won't work because key is a variable.
You can also use Object.keys(), Object.values(), and Object.entries() if you want to work with the keys or values as arrays:
console.log(Object.keys(person));
// ["name", "age", "email"]
console.log(Object.values(person));
// ["Alice", 31, "alice@example.com"]
Object.entries(person).forEach(([key, value]) => {
console.log(key, ": ", value);
});
Object.entries() is particularly handy when you need both the key and value together without the for...in syntax.
Here's everything applied to one object:
const person = {
name: "Alice",
age: 30,
city: "Mumbai"
};
// Access
console.log(person.name); // "Alice"
console.log(person["city"]); // "Mumbai"
// Update
person.age = 31;
// Add
person.email = "alice@example.com";
// Delete
delete person.city;
// Loop
for (let key in person) {
console.log(key + ": " + person[key]);
}
// name: Alice
// age: 31
// email: alice@example.com
Objects are one of the most-used structures in JavaScript. Once you're comfortable with creating, reading, updating, and looping through them, a lot of JavaScript code starts making more sense because objects show up everywhere: in API responses, in function arguments, in component state, and more.