🧰 JavaScript Object Methods
Last Updated: 15th October 2025
Object methods are built-in functions that help you work with objects —
like finding keys, values, merging, freezing, or copying objects.
Hinglish Tip 🗣: Object methods aise tools hain jo object ke andar ka data samajhne, badalne, ya copy karne me help karte hain.
🗂 Commonly Used Object Methods
| Method | Description |
|---|---|
Object.keys() | Returns an array of all keys |
Object.values() | Returns an array of all values |
Object.entries() | Returns an array of [key, value] pairs |
Object.assign() | Copies properties from one object to another |
Object.freeze() | Makes an object read-only (can’t be changed) |
Object.seal() | Prevents adding/removing properties (but can update values) |
Object.hasOwn() | Checks if a key exists in object |
✏ Example
const user = {
name: "Riya",
age: 25,
city: "Delhi",
};
console.log(Object.keys(user)); // ["name", "age", "city"]
console.log(Object.values(user)); // ["Riya", 25, "Delhi"]
console.log(Object.entries(user)); // [["name", "Riya"], ["age", 25], ["city", "Delhi"]]
const target = { a: 1 };
const source = { b: 2, c: 3 };
const merged = Object.assign(target, source);
console.log(merged); // { a: 1, b: 2, c: 3 }
Object.freeze(user);
user.age = 30; // ❌ will not change
console.log(user.age); // 25
Object.seal(user);
user.age = 30; // ❌ will not change
user.city = "Mumbai";
console.log(user.age); // 25
console.log(Object.hasOwn(user, "name")); // true
⚡ Mini Notes
- 🔒 freeze() → nothing can be changed.
- 🔐 seal() → no new keys, no delete, but update allowed.
- 🔍 hasOwn() → better than hasOwnProperty() in modern JS (ES2022).
💡 Quick Practice
- Create an object car with brand, model, price.
- Use Object.keys() and Object.values() to print details.
- Use Object.assign() to merge it with another object color: "red".
- Use Object.freeze() and try to change one value.
- Use Object.hasOwn(car, "model") to verify key presence.
- Exercise Set 1
- Exercise Set 2