All topics
Objectsbeginner

Object.keys, values, entries, and Iteration

The three static methods for extracting an object's own enumerable properties as arrays, and how they interact with for...in and destructuring.

Plain objects aren't directly iterable with for...of the way arrays are, so Object.keys(), Object.values(), and Object.entries() exist specifically to convert an object's own enumerable properties into arrays that array methods and for...of can work with. This trio comes up constantly in real code for transforming or inspecting object data, making it a standard interview checkpoint.

Object.entries is like handing someone a spreadsheet export of a filing cabinet's own labeled folders (not the folders borrowed from a shared archive down the hall) — each row is a folder name paired with its contents, ready to be sorted, filtered, or rebuilt into a new cabinet.

Key Concepts

1
Object.keys(obj) returns an array of obj's own enumerable property names (strings only, symbol keys are excluded), in insertion order for string keys, except that integer-like keys are sorted numerically first regardless of insertion order — a quirk of the spec that occasionally surprises people working with object keys that happen to look like array indices. Object.values(obj) returns the corresponding array of values in the same order, and Object.entries(obj) returns an array of [key, value] pairs, which pairs naturally with destructuring in a for...of loop: for (const [key, value] of Object.entries(obj)).
Object.keys(obj)objObject.values(obj)Object.entries(obj)[key, value]
2
All three only include the object's *own* properties — not properties inherited via the prototype chain — which is usually the desired behavior and avoids the classic for...in loop pitfall, where for...in iterates inherited enumerable properties too, unless you guard it with Object.prototype.hasOwnProperty.call(obj, key) inside the loop body. This is one of the main reasons Object.keys/values/entries combined with for...of or array methods are generally preferred over for...in in modern code.
for...inObject.prototype.hasOwnProperty.call(obj, key)Object.keys/values/entriesfor...of
3
Object.fromEntries() is the inverse of Object.entries(), converting an array of [key, value] pairs back into a plain object — useful for round-tripping through array transformations (filtering, mapping) and back into object form, such as filtering out certain keys from an object by converting to entries, filtering the array, and converting back.
Object.fromEntries()Object.entries()[key, value]