Object.keys, values, and entries
Objects are great for storing named data.
But sometimes you need to loop through that data.
For arrays, looping is straightforward because arrays are ordered lists.
For objects, you need a way to get their keys, values, or key-value pairs.
JavaScript gives you three very useful methods:
Object.keys()Object.values()Object.entries()
These methods turn object data into arrays so you can loop, map, filter, or reduce it.
Example Object
We will use this object:
const user = {
name: "Alice",
age: 25,
role: "admin",
};It has three properties:
name: Alice
age: 25
role: adminObject.keys()
Object.keys() returns an array of the object's property names.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
const keys = Object.keys(user);
console.log(keys); // ["name", "age", "role"]The returned value is an array.
That means you can use array methods on it.
keys.forEach((key) => {
console.log(key);
});Output:
name
age
roleUsing Keys to Read Values
Keys are useful because you can use bracket notation to access values.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
Object.keys(user).forEach((key) => {
console.log(`${key}: ${user[key]}`);
});Output:
name: Alice
age: 25
role: adminThis works because key is a variable.
So you use:
user[key]not:
user.keyObject.values()
Object.values() returns an array of the object's values.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
const values = Object.values(user);
console.log(values); // ["Alice", 25, "admin"]Use Object.values() when you only care about the values and do not need the property names.
Example:
const scores = {
math: 90,
science: 85,
english: 88,
};
const total = Object.values(scores).reduce((sum, score) => {
return sum + score;
}, 0);
console.log(total); // 263The object values become an array:
[90, 85, 88]Then reduce sums them.
Object.entries()
Object.entries() returns an array of key-value pairs.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
const entries = Object.entries(user);
console.log(entries);Output:
[
["name", "Alice"],
["age", 25],
["role", "admin"]
]Each entry is a two-item array:
[key, value]Looping Through Entries
Object.entries() is very useful with destructuring.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
for (const [key, value] of Object.entries(user)) {
console.log(`${key}: ${value}`);
}Output:
name: Alice
age: 25
role: adminThis is often the cleanest way to loop through both property names and values.
Comparing the Three Methods
const user = {
name: "Alice",
age: 25,
role: "admin",
};| Method | Returns |
|---|---|
Object.keys(user) |
["name", "age", "role"] |
Object.values(user) |
["Alice", 25, "admin"] |
Object.entries(user) |
[["name", "Alice"], ["age", 25], ["role", "admin"]] |
Use the method that gives you the data you need.
Checking Object Size
Objects do not have a .length property like arrays.
const user = {
name: "Alice",
age: 25,
};
console.log(user.length); // undefinedTo count properties, use Object.keys().
const propertyCount = Object.keys(user).length;
console.log(propertyCount); // 2This is a common pattern.
Checking if an Object Is Empty
const settings = {};
const isEmpty = Object.keys(settings).length === 0;
console.log(isEmpty); // trueIf an object has no own enumerable properties, Object.keys() returns an empty array.
Transforming an Object Into an Array
Sometimes object data is easier to display as an array.
const scores = {
math: 90,
science: 85,
english: 88,
};
const scoreList = Object.entries(scores).map(([subject, score]) => {
return `${subject}: ${score}`;
});
console.log(scoreList);Output:
["math: 90", "science: 85", "english: 88"]This pattern combines object methods with array methods.
Building an Object From Entries
There is also a reverse method: Object.fromEntries().
It turns key-value pairs back into an object.
const entries = [
["name", "Alice"],
["role", "admin"],
];
const user = Object.fromEntries(entries);
console.log(user); // { name: "Alice", role: "admin" }This is useful when you transform entries and want to rebuild an object.
Transforming Object Values
Example: increase all scores by 5.
const scores = {
math: 90,
science: 85,
english: 88,
};
const updatedEntries = Object.entries(scores).map(([subject, score]) => {
return [subject, score + 5];
});
const updatedScores = Object.fromEntries(updatedEntries);
console.log(updatedScores);Output:
{
math: 95,
science: 90,
english: 93
}The steps are:
- Convert object to entries.
- Map over the entries.
- Convert entries back into an object.
Filtering Object Properties
You can also filter object entries.
const user = {
name: "Alice",
password: "secret",
role: "admin",
};
const publicEntries = Object.entries(user).filter(([key]) => {
return key !== "password";
});
const publicUser = Object.fromEntries(publicEntries);
console.log(publicUser); // { name: "Alice", role: "admin" }This creates a new object without the password property.
Own Enumerable Properties
Object.keys, Object.values, and Object.entries return an object's own enumerable properties.
For beginner work, this usually means:
The normal properties you added directly to the object.const user = {
name: "Alice",
role: "admin",
};
console.log(Object.keys(user)); // ["name", "role"]Later, when you learn prototypes, you will see why the words "own" and "enumerable" matter.
For now, these methods are the standard way to inspect and loop through normal object data.
Property Order
Objects are not arrays, but modern JavaScript does preserve a predictable order for most normal object keys.
const user = {
name: "Alice",
age: 25,
role: "admin",
};
console.log(Object.keys(user)); // ["name", "age", "role"]Do not use objects when strict numeric ordering is the main purpose.
Use arrays for ordered lists.
Use objects for named properties.
Best Practices
Use Object.keys() when you need property names:
const keys = Object.keys(user);Use Object.values() when you need only values:
const scores = Object.values(scoreBySubject);Use Object.entries() when you need both:
for (const [key, value] of Object.entries(user)) {
console.log(key, value);
}Use Object.fromEntries() to rebuild objects after transforming entries:
const object = Object.fromEntries(entries);Use arrays when order is the main concern.
Use objects when names are the main concern.
Common Mistakes
Mistake 1: Expecting Objects to Have .length
const user = {
name: "Alice",
};
console.log(user.length); // undefinedUse:
Object.keys(user).length;Mistake 2: Using Dot Notation With a Dynamic Key
const user = {
name: "Alice",
};
const key = "name";
console.log(user.key); // undefinedUse bracket notation:
console.log(user[key]); // AliceMistake 3: Forgetting Entries Are Arrays
const user = {
name: "Alice",
};
for (const entry of Object.entries(user)) {
console.log(entry.key); // undefined
}Each entry is an array:
["name", "Alice"]Use destructuring:
for (const [key, value] of Object.entries(user)) {
console.log(key, value);
}Quick Check
What does this return?
const user = {
name: "Alice",
role: "admin",
};
Object.keys(user);It returns:
["name", "role"]What does this return?
Object.values(user);It returns:
["Alice", "admin"]What does this return?
Object.entries(user);It returns:
[
["name", "Alice"],
["role", "admin"]
]Summary
Object methods help you inspect and loop through object data.
Object.keys()returns property names.Object.values()returns property values.Object.entries()returns key-value pairs.- Objects do not have
.length; useObject.keys(object).length. - Use
Object.entries()with destructuring to loop through keys and values. - Use
Object.fromEntries()to turn key-value pairs back into an object. - These methods work well with array methods like
map,filter, andreduce.