SyntaxStudy
Sign Up
JavaScript Object.keys(), values(), and entries()
JavaScript Intermediate 7 min read

Object.keys(), values(), and entries()

Object.keys(), values(), and entries()

Before ES2017, iterating over an object's properties required for...in loops and hasOwnProperty checks. The static methods Object.keys(), Object.values(), and Object.entries() provide clean, array-based iteration over own enumerable properties.

Object.keys(obj)

Returns an array of the object's own enumerable property names (keys) in insertion order for string keys. You can then use array methods like forEach, map, or filter on the result.

Object.values(obj)

Returns an array of the object's own enumerable property values in the same order as keys().

Object.entries(obj)

Returns an array of [key, value] pairs, which pairs beautifully with array destructuring in a for...of loop or a map call.

Object.fromEntries()

The inverse of entries() — converts an iterable of [key, value] pairs back into an object. Useful for transforming objects via array methods.

Example
const scores = { Alice: 90, Bob: 78, Carol: 85 };
console.log(Object.keys(scores));   // ['Alice','Bob','Carol']
console.log(Object.values(scores)); // [90,78,85]
console.log(Object.entries(scores));// [['Alice',90],['Bob',78],['Carol',85]]
for (const [name, score] of Object.entries(scores)) {
  console.log(name + ': ' + score);
}
const doubled = Object.fromEntries(
  Object.entries(scores).map(([k, v]) => [k, v * 2])
);
console.log(doubled); // { Alice: 180, Bob: 156, Carol: 170 }
Pro Tip

The Object.entries() + Object.fromEntries() pair is the idiomatic way to transform an object's values using array methods, replacing verbose manual loops.