DEV Community

Tamizh K
Tamizh K

Posted on

Objects in JavaScript

Here's a clean, ready-to-publish blog post for you on JavaScript Objects:

JavaScript Objects Explained: The Building Blocks You Actually Need to Understand

If arrays are lists, objects are dictionaries. Almost everything in JavaScript is an object or behaves like one.

An object is just a collection of key-value pairs.

1. How to Create an Object

There are 3 common ways:
// 1. Object literal - you'll use this 90% of the time
const user = {
name: "Arjun",
age: 24,
isDev: true
};

// 2. Using new Object()
const user2 = new Object();
user2.name = "Arjun";

// 3. Constructor function
function User(name) {
this.name = name;
}
const user3 = new User("Arjun");
2. Accessing and Modifying
console.log(user.name); // dot notation
console.log(user["age"]); // bracket notation - useful when key has space or is dynamic

user.age = 25; // update
user.city = "Chennai"; // add
delete user.isDev; // delete
3. Methods and this

When a function is inside an object, it's called a method. this refers to the object itself.
const user = {
name: "Arjun",
greet() {
return Hi, I'm ${this.name};
}
};

user.greet(); // "Hi, I'm Arjun"
4. The Real Power: Nesting and Advanced Tricks

Objects can contain other objects, arrays, functions - anything.
const appUser = {
id: 101,
profile: {
skills: ["JS", "React", "Node"],
socials: { github: "arjun-dev" }
}
};

console.log(appUser.profile.skills[1]); // React
5. Object Methods You Must Know

Don't manually loop everything. JS gives you tools:
const obj = { a: 1, b: 2, c: 3 };

Object.keys(obj); // ["a", "b", "c"]
Object.values(obj); // [1, 2, 3]
Object.entries(obj); // [["a", 1], ["b", 2], ["c", 3]]

Object.freeze(obj); // locks the object, no more changes
Object.seal(obj); // can't add/delete, but can update values

const newObj = {...obj, d: 4 }; // spread operator - cloning + merging
6. Common Mistakes Beginners Make

  • Objects are reference types: const a = {}; const b = a; Both point to the same memory. Change b, a changes too. Use {...a} to truly clone.
  • Don't compare with ==: {} == {} is always false.
  • JSON vs Object: JSON is a string format for objects. JSON.stringify() and JSON.parse() are your bridge to APIs.

Objects are not just a topic in JavaScript. They are JavaScript. Master how they hold data, how this works, and how to clone/merge them, and you've mastered 70% of the language.

Want me to make this SEO-optimized with a title, meta description, and keywords? Or convert it into a version for absolute beginners vs. interview prep?

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Want me to make this SEO-optimized with a title, meta description, and keywords? Or convert it into a version for absolute beginners vs. interview prep?

Hahahahahahaha.... Copy/pasted from an LLM maybe? There are rules about this kind of content: