Object.keys, Object.values, Object.entries0%
Object.assign()

Object.keys, Object.values, Object.entries

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Object.keys, Object.values, Object.entries in Modern JavaScript

Iterating over and introspecting objects has evolved significantly from the error-prone for...in loops of early JavaScript. Modern standards provide static methods on Object that return standard arrays of an object's enumerable own properties, integrating seamlessly with array methods like .map(), .filter(), and .forEach().


1. Object.keys()

Object.keys(obj) returns an array of a given object's own enumerable property names (keys):

JavaScript
const config = {
theme: 'dark',
version: '2.4.0',
autoSave: true
};
 
const keys = Object.keys(config);
console.log(keys); // ['theme', 'version', 'autoSave']
 
// Checking if an object is empty
const isEmpty = Object.keys(config).length === 0;

2. Object.values()

Object.values(obj) returns an array of a given object's own enumerable property values:

JavaScript
const monthlySales = {
january: 12000,
february: 15500,
march: 18200
};
 
const revenues = Object.values(monthlySales);
console.log(revenues); // [12000, 15500, 18200]
 
const quarterTotal = revenues.reduce((sum, val) => sum + val, 0);
console.log('Q1 Revenue:', quarterTotal); // 45700

3. Object.entries()

Object.entries(obj) returns an array of a given object's own enumerable string-keyed [key, value] pairs:

JavaScript
const userRoles = {
alice: 'Admin',
bob: 'Editor',
charlie: 'Viewer'
};
 
const entries = Object.entries(userRoles);
console.log(entries);
// [
// ['alice', 'Admin'],
// ['bob', 'Editor'],
// ['charlie', 'Viewer']
// ]

Destructuring in Loops with for...of

JavaScript
for (const [user, role] of Object.entries(userRoles)) {
console.log(`User ${user} has role permission: ${role}`);
}

4. Reconstructing Objects with Object.fromEntries()

Object.fromEntries() performs the inverse of Object.entries(), transforming a list of key-value pairs back into an object. This enables filtering and transforming objects using standard array methods:

Output
Original Object
│
▼
Object.entries() ───► [ [k1, v1], [k2, v2] ]
│
.filter() / .map()
│
▲ ▼
Object.fromEntries() ◄── [ [k1, newVal1] ]
│
Transformed Object
JavaScript
const inventory = {
apples: 15,
oranges: 0,
bananas: 28,
peaches: 0
};
 
// Filter out out-of-stock items (value > 0)
const inStockInventory = Object.fromEntries(
Object.entries(inventory).filter(([_, count]) => count > 0)
);
 
console.log(inStockInventory);
// { apples: 15, bananas: 28 }

5. Why Object.keys() is Superior to for...in

JavaScript
// AVOID: for...in iterates over inherited prototype properties!
for (const key in obj) {
if (Object.hasOwn(obj, key)) { // Required boilerplate to prevent prototype leakage
console.log(key);
}
}
 
// PREFERRED: Object.keys() only returns own enumerable properties
Object.keys(obj).forEach(key => console.log(key));

Interactive Quiz

Practice Quiz

Question 1
What does Object.keys({ a: 1, b: 2 }) return?
Question 2
How do you verify if a plain JavaScript object is completely empty ({})?
Question 3
Which method is the inverse of Object.entries(), converting [ [key, val], ... ] pairs back into an object?
Question 4
What does Object.entries({ score: 100 }) return?
Question 5
Why is Object.values() preferred over a manual for...in loop with obj[key]?
Next Lesson

Object.assign()

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext