In JavaScript, objects are used to store related information using key-value pairs.
For example:
const person = {
name: "John",
age: 25,
city: "Delhi"
};As our programs become more complex, we often need to inspect, copy, combine, or modify objects. JavaScript provides several built-in methods through the Object object to help us do exactly that.
Let's look at the most useful ones.
What are Object Methods?
Object methods are built-in JavaScript methods used to perform common operations on objects.
For example:
console.log(Object.keys(person));This gives us all the property names of the object.
Object.keys()
The Object.keys() method returns an array containing the object's own enumerable property names.
const person = {
name: "John",
age: 25,
city: "Delhi"
};
console.log(Object.keys(person));Output:
["name", "age", "city"]This is useful when we want to know what properties an object contains.
For example, we can use it with a loop:
for (let key of Object.keys(person)) {
console.log(key);
}Output:
name
age
cityObject.values()
Object.values() returns an array containing the object's property values.
console.log(Object.values(person));Output:
["John", 25, "Delhi"]This is useful when we care about the values rather than the property names.
For example:
for (let value of Object.values(person)) {
console.log(value);
}Output:
John
25
DelhiObject.entries()
Object.entries() returns an array containing the object's key-value pairs.
console.log(Object.entries(person));Output:
[
["name", "John"],
["age", 25],
["city", "Delhi"]
]This is particularly useful when we want both the key and value.
for (let [key, value] of Object.entries(person)) {
console.log(key, value);
}Output:
name John
age 25
city DelhiObject.hasOwn()
Sometimes we want to check whether an object contains a particular property.
For this, we can use Object.hasOwn().
const person = {
name: "John",
age: 25
};
console.log(Object.hasOwn(person, "name"));
console.log(Object.hasOwn(person, "city"));Output:
true
falseThis is useful when working with objects whose properties may vary.
Object.assign()
Object.assign() copies properties from one or more source objects into a target object.
For example:
const person = {
name: "John"
};
const details = {
age: 25,
city: "Delhi"
};
Object.assign(person, details);
console.log(person);Output:
{
name: "John",
age: 25,
city: "Delhi"
}It can also be used to create a new object:
const person = {
name: "John",
age: 25
};
const copy = Object.assign({}, person);
console.log(copy);For simple objects, however, the spread operator is usually cleaner:
const copy = { ...person };We'll cover the spread operator separately.
Object.create()
Object.create() creates a new object using another object as its prototype.
For example:
const person = {
greet() {
console.log("Hello!");
}
};
const student = Object.create(person);
student.greet();Output:
Hello!Here, student can access greet() through its prototype.
This method is useful when learning prototypes and inheritance, but you don't need to use it frequently as a beginner.
Object.freeze()
Object.freeze() prevents an object from being modified.
For example:
const person = {
name: "John",
age: 25
};
Object.freeze(person);
person.age = 30;
console.log(person.age);The object remains unchanged.
In strict mode, attempts to modify a frozen object can throw an error.
We can also try adding a property:
person.city = "Delhi";It won't be added because the object is frozen.
Object.freeze()is useful when you want to prevent changes to an object, but remember that freezing is shallow. Nested objects can still be modified unless they are frozen separately.
Object.seal()
Object.seal() prevents adding or deleting properties, but existing properties can still be changed.
const person = {
name: "John",
age: 25
};
Object.seal(person);
person.age = 30; // ✅ Allowed
person.city = "Delhi"; // ❌ Not added
delete person.name; // ❌ Not allowedSo the difference is:
Method | Change existing properties | Add properties | Delete properties |
|---|---|---|---|
| ❌ | ❌ | ❌ |
| ✅ | ❌ | ❌ |
Object.fromEntries()
Object.fromEntries() does almost the opposite of Object.entries().
It converts an array of key-value pairs into an object.
const entries = [
["name", "John"],
["age", 25]
];
const person = Object.fromEntries(entries);
console.log(person);Output:
{
name: "John",
age: 25
}This becomes particularly useful when transforming object data.
For example, we can combine it with other array methods:
const prices = {
laptop: 50000,
phone: 20000
};
const discounted = Object.fromEntries(
Object.entries(prices).map(([product, price]) => [
product,
price * 0.9
])
);
console.log(discounted);Output:
{
laptop: 45000,
phone: 18000
}This may look complicated right now, but the idea becomes much easier once you're comfortable with arrays, map(), and destructuring.
Object.is()
Object.is() checks whether two values are the same using a comparison algorithm similar to strict equality (===), but with a few special differences.
console.log(Object.is(10, 10));
console.log(Object.is("Hello", "Hello"));Output:
true
trueOne interesting difference is:
console.log(Object.is(NaN, NaN));Output:
trueWhile:
console.log(NaN === NaN);returns:
falseObject.is() is useful in some specialized cases, but === is still what you'll commonly use for ordinary comparisons.
Common Object Methods at a Glance
Method | Purpose |
|---|---|
| Gets property names |
| Gets property values |
| Gets key-value pairs |
| Checks for an own property |
| Copies/merges properties |
| Creates an object with a specified prototype |
| Prevents modifications |
| Prevents adding/deleting properties |
| Creates an object from key-value pairs |
| Compares two values |
A Practical Example
Suppose we have a product object:
const product = {
name: "Laptop",
price: 50000,
brand: "TechBrand"
};We can use object methods to work with it:
console.log(Object.keys(product));
console.log(Object.values(product));
console.log(Object.entries(product));
console.log(Object.hasOwn(product, "price"));Output:
["name", "price", "brand"]
["Laptop", 50000, "TechBrand"]
[
["name", "Laptop"],
["price", 50000],
["brand", "TechBrand"]
]
trueThis shows how easily we can inspect and work with object data.
Conclusion
JavaScript provides many built-in methods for working with objects. The ones you'll use most often are:
Object.keys()
Object.values()
Object.entries()
Object.hasOwn()
Object.assign()
Object.freeze()
Object.seal()
Object.fromEntries()The three especially important ones to remember are:
Object.keys()→ Get keysObject.values()→ Get valuesObject.entries()→ Get keys and values
Once you understand these methods, working with objects becomes much easier, especially when processing data received from APIs or databases.