Object.keys() / Object.values() / Object.entries()
Gets arrays of keys, values, or [key,value] pairs from an object.
Object.keys(obj)
Object.values(obj)
Object.entries(obj)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Gets arrays of keys, values, or [key,value] pairs from an object.
Object.keys(), Object.values(), and Object.entries() are static methods that return arrays containing an object's own enumerable string-keyed property names, values, or [key, value] pairs respectively. These methods are essential for bridging the gap between objects and array iteration methods like map(), filter(), and reduce(). Performance is generally O(N) relative to the number of properties, though creating large arrays from massive objects can lead to significant memory overhead. A key edge case is that these methods exclude inherited properties from the prototype chain and ignore Symbol-keyed properties. In modern JavaScript (ES2015+), the iteration order is deterministic: integer-like keys appear first in ascending order, followed by other string keys in their original insertion order. While Object.keys is available in ES5, Object.values and Object.entries were introduced in ES2017; therefore, IE is not supported for the latter two.
Quick reference
Syntax
Object.keys(obj)
Object.values(obj)
Object.entries(obj)
Inputs
Parameters
See it in practice
Examples
Summing Numeric Values
const expenses = { rent: 1200, food: 400, utilities: 150 };
const total = Object.values(expenses).reduce((acc, val) => acc + val, 0);
console.log(total);1750
Uses Object.values to extract all numbers into an array, then aggregates them using reduce. IE not supported for Object.values.
Mapping and Formatting Entries
const user = { name: 'Alice', role: 'Admin' };
const formatted = Object.entries(user).map(([key, value]) => {
return key.toUpperCase() + ': ' + value;
});
console.log(formatted);['NAME: Alice', 'ROLE: Admin']
Converts an object into a list of formatted strings by destructuring the [key, value] pairs provided by Object.entries. IE not supported.
Identifying Keys for Specific Values
const statusCodes = { success: 200, created: 201, badRequest: 400 };
const keys = Object.keys(statusCodes).filter(key => statusCodes[key] >= 400);
console.log(keys);['badRequest']
Iterates over the property names of an object to find those that match a specific condition based on their values.
Debug faster
Common Errors
TypeError
Cause: Passing null or undefined as the argument.
Fix: Verify the variable is an object or provide a default empty object fallback.
const data = null;
Object.keys(data || {}); // Returns [] instead of throwingLogicError
Cause: Expecting properties inherited via the prototype chain to be included in the array.
Fix: Use a for...in loop if inherited properties are needed, or define properties directly on the object.
const parent = { inherited: true };
const child = Object.create(parent);
child.own = true;
console.log(Object.keys(child)); // ['own'] (inherited is missing)Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Gets arrays of keys, values, or [key,value] pairs from an object.
obj: The object to inspect.
TypeError: Verify the variable is an object or provide a default empty object fallback. LogicError: Use a for...in loop if inherited properties are needed, or define properties directly on the object.