Every other data type in JavaScript, numbers, strings, booleans, holds one single value. Objects are the odd one out, and honestly the most powerful one, because they let you group a bunch of related values together under one thing. A user isn't just a name, it's a name, an age, an email, and probably a dozen other details, and trying to track all of that as separate variables gets messy fast. Objects are how JavaScript actually organizes real-world information.
Let's go through how objects work, the handful of things about them that confuse people early on, and a few tools that make working with them a lot less painful.
Table of Contents
The Basics
An object is just a collection of key-value pairs, wrapped in curly braces.
let user = {
name: "Vi",
age: 22,
isStudent: true
};
console.log(user.name); // "Vi"
console.log(user["age"]); // 22
Both user.name and user["age"] do the same job, accessing a value by its key. Dot notation is more common day to day, but bracket notation matters the moment your key is stored in a variable or has spaces in it.
let key = "name"; console.log(user[key]); // "Vi", works with a variable console.log(user.key); // undefined, this looks for a property literally called "key"
Objects Are Mutable, Even When "Constant"
This one confuses a lot of people early on. Declaring an object with const doesn't freeze its contents, it just stops you from reassigning the variable to a completely different object.
const person = { name: "Vi" };
person.name = "Vineet"; // totally fine
console.log(person.name); // "Vineet"
person = { name: "Someone else" }; // TypeError!
const is really just locking the variable itself, not the object it points to. If you genuinely want an object's contents to be unchangeable, you need Object.freeze() instead.
const settings = Object.freeze({ theme: "dark" });
settings.theme = "light"; // silently fails (or throws in strict mode)
console.log(settings.theme); // "dark"
Comparing Objects Doesn't Work the Way You'd Expect
Here's a genuinely common source of confusion. Two objects that look identical are not considered equal.
let obj1 = { name: "Vi" };
let obj2 = { name: "Vi" };
console.log(obj1 === obj2); // false
That's because objects are compared by reference, not by their actual contents. JavaScript is checking "do these two variables point to the exact same object in memory," not "do these two objects look the same." If you assign one object to another variable, though, they do refer to the same thing.
let obj3 = obj1; console.log(obj1 === obj3); // true, same object, same reference
Useful Ways to Work With Objects
A few built-in methods make everyday object tasks a lot easier.
let product = { name: "Laptop", price: 55000 };
console.log(Object.keys(product)); // ["name", "price"]
console.log(Object.values(product)); // ["Laptop", 55000]
console.log(Object.entries(product)); // [["name", "Laptop"], ["price", 55000]]
Object.entries() is especially handy when you want to loop through both a key and its value at the same time.
for (let [key, value] of Object.entries(product)) {
console.log(`${key}: ${value}`);
}
// name: Laptop
// price: 55000
Combining two objects into one is easy with the spread operator, and it's genuinely one of the more commonly used patterns in modern JavaScript.
let base = { name: "Laptop" };
let details = { price: 55000, inStock: true };
let combined = { ...base, ...details };
console.log(combined); // { name: "Laptop", price: 55000, inStock: true }
Conclusion
Objects are really the backbone of how JavaScript represents anything with more than one piece of information attached to it. The trickiest parts aren't the syntax, they're the two quiet assumptions people bring in from other habits, thinking const locks the whole object, and expecting two similar-looking objects to be equal. Once those two ideas click, working with objects stops feeling unpredictable and starts feeling like the genuinely useful tool it actually is.Frequently Asked Questions (FAQs)
1.Why doesn't const stop me from changing an object's properties?2.Why does comparing two identical-looking objects return false?Because const only prevents reassigning the variable itself to a new value, it doesn't lock the contents of the object that variable points to. Use Object.freeze() if you actually need the contents to stay unchangeable.
3.What's the easiest way to check if an object has a specific property?Because === checks whether two variables point to the exact same object in memory, not whether their contents look the same. Two separately created objects are never considered equal, even with identical properties.
Use the in operator or Object.hasOwn(), like "name" in user or Object.hasOwn(user, "name"). Both return a straightforward true or false without you needing to check the value itself.
0 Comments