Skip to main content
Madhukar
All Articles

Understanding Objects in JavaScript

August 7, 20267 min read
javascriptbeginnerprogrammingweb-development
Understanding Objects in JavaScript

What Objects Are and Why They Are Needed

A single variable can hold one value: a name, an age, a city. But real-world things rarely have just one property — a person has a name and an age and a city, all belonging together as one unit. Tracking these as separate, disconnected variables quickly becomes unwieldy:

const personName = "Aarav";
const personAge = 21;
const personCity = "Delhi";

An object solves this by grouping related data together, as a single value made up of key-value pairs:

const person = {
name: "Aarav",
age: 21,
city: "Delhi",
};

Each key (name, age, city) is a label; each corresponding value ("Aarav", 21, "Delhi") is the actual data. Together, person represents one coherent thing, instead of three scattered variables that happen to be related only by convention.

Creating Objects

The most common way to create an object is with object literal syntax — curly braces containing key-value pairs:

const student = {
name: "Priya",
age: 20,
course: "Computer Science",
};

Each key is followed by a colon, then its value, with commas separating the pairs. Property keys in object literals are commonly written without quotes when they are valid identifier names. Quotes are required when the key contains spaces, hyphens, or other characters that cannot be used in an unquoted property name. Property keys can also be written using computed expressions.

const key = "course";

const student = {
name: "Priya",
[key]: "Computer Science",
};

console.log(student.course);
// "Computer Science"

Here, [key] is a computed property name. JavaScript evaluates key and uses its value ("course") as the property name.

Accessing Properties: Dot Notation and Bracket Notation

There are two ways to read a value out of an object.

Dot notation

console.log(student.name);   // "Priya"
console.log(student.age); // 20

This is the most common, most readable form — used when the property name is known in advance and can be written as a valid identifier name after the dot (for example, no spaces or hyphens).

Bracket notation

console.log(student["name"]);   // "Priya"
console.log(student["age"]); // 20

Bracket notation becomes necessary in two common situations:

When the key is stored in a variable:

const key = "course";
console.log(student[key]); // "Computer Science" — dot notation can't do this

When the key contains characters dot notation can’t handle, like a space:

const info = { "full name": "Priya Sharma" };
console.log(info["full name"]); // dot notation (info.full name) would be invalid syntax

Updating Object Properties

Changing an existing property’s value uses the same dot or bracket notation, on the left side of an assignment:

const student = { name: "Priya", age: 20, course: "Computer Science" };

student.age = 21;
console.log(student.age); // 21

student["course"] = "Data Science";
console.log(student.course); // "Data Science"

Note that student itself was declared with const — that only prevents reassigning student to a completely different object; it doesn't prevent modifying properties within the object, which is exactly what's happening above.

Adding and Deleting Properties

Adding a new property

Simply assign a value to a key that doesn’t exist yet — no special syntax needed:

student.email = "priya@example.com";
console.log(student);
// { name: "Priya", age: 21, course: "Data Science", email: "priya@example.com" }

Deleting a property

The delete operator removes a property entirely:

delete student.email;
console.log(student);
// { name: "Priya", age: 21, course: "Data Science" }

Looping Through Object Keys

Unlike arrays, objects are commonly traversed by their property names. A for...in loop iterates over enumerable string-keyed properties, including inherited enumerable properties.

const student = { name: "Priya", age: 21, course: "Data Science" };

for (const key in student) {
console.log(`${key}: ${student[key]}`);
}
// name: Priya
// age: 21
// course: Data Science

Notice student[key] uses bracket notation here — key is a variable holding the current key name on each pass through the loop, which is exactly the situation bracket notation is required for.

An alternative: Object.keys()

const keys = Object.keys(student);
console.log(keys); // ["name", "age", "course"]

keys.forEach(key => {
console.log(`${key}: ${student[key]}`);
});

Object.keys() returns an array containing an object's own enumerable string-keyed property names.
It does not include inherited properties or symbol keys. These keys can then be iterated over using familiar array methods like forEach() — a common alternative to for...in.

Array vs Object: A Clear Comparison

A simple way to decide which one fits: if the data is a collection of similar things (a list of student names), reach for an array. If the data is one thing with multiple distinct properties (one student’s name, age, and course), reach for an object. Real applications very often combine both — an array of objects — like a list of multiple students, each represented as their own object:

const students = [
{ name: "Priya", age: 20 },
{ name: "Aarav", age: 21 },
];

Practice Assignment

1. Create an object representing a student

const student = {
name: "Meera",
age: 19,
course: "Web Development",
};

2. Add another property

Already included above — try adding one more, like grade:

student.grade = "A";

3. Update one property

student.age = 20;
console.log(student.age); // 20

4. Print all keys and values using a loop

for (const key in student) {
console.log(`${key}: ${student[key]}`);
}
// name: Meera
// age: 20
// course: Web Development
// grade: A

Try adding a new property, deleting one, and re-running the loop to see exactly how the output changes each time — a fast, hands-on way to build real confidence with objects.

Final Takeaway

Objects exist to group related data together as one coherent thing, using clear, named keys instead of scattered, disconnected variables. Dot notation is the everyday way to read and write properties when the key is known upfront; bracket notation steps in when the key is dynamic or contains special characters. Adding a property is as simple as assigning to a new key; delete removes one entirely. And for...in (or Object.keys()) lets you walk through every property without needing to know the key names in advance. Once this clicks, the difference from arrays becomes obvious too: arrays are for ordered collections of similar things; objects are for one thing with several distinct, named properties — and real applications lean on both, constantly, often together.

Frequently Asked Questions

Can an object property hold another object or an array?

> Yes — object values can be any type, including other objects or arrays. This is exactly how more complex, nested data (like a student object containing an array of their enrolled courses) gets represented in JavaScript.

Why does const allow me to modify an object’s properties?

> const only prevents reassigning the variable itself to point to a different value entirely. The object it points to can still be freely modified — its properties added, changed, or deleted — since that doesn't change what const is actually protecting.

When should I use for…in versus Object.keys()?

> Both work for looping through an object’s keys. Object.keys() is often preferred in modern code since it returns a real array, letting you use familiar array methods like .map() or .forEach() directly, rather than being limited to a plain loop.

Is there a risk in using bracket notation instead of dot notation?

> Not inherently — they’re functionally equivalent when the key is a valid identifier. Bracket notation is simply required in specific situations (dynamic keys, keys with special characters); using dot notation as the default for everyday, known keys is just a matter of readability convention.

Originally published by Mr Madhukar

Read the complete article on Medium with full formatting & reader responses.