JavaScript Object Keys Values and Entries Guide

Learn Object.keys(), Object.values(), and Object.entries() to iterate, transform, and inspect JavaScript objects. Practical patterns for real code.

5 min read

Object.keys(), Object.values(), and Object.entries() are three static methods that convert an object's properties into arrays. Once your data is in array form, you can iterate, filter, map, sort, and transform it using the full set of array methods.

MethodReturnsExample
Object.keys(obj)Array of property namesname, age
Object.values(obj)Array of property valuesAlex, 28
Object.entries(obj)Array of key-value pairs[name, Alex], [age, 28]

All three methods return only the object's own enumerable properties, in the same order. Properties inherited through the prototype chain are not included in the result.

Object.keys(): Get All Property Names

Object.keys() returns an array of the object's own enumerable property names as strings:

javascriptjavascript
const user = { name: "Alex", age: 28, role: "admin" };
 
const keys = Object.keys(user);
 
console.log(keys);        // ["name", "age", "role"]
console.log(keys.length); // 3

This is useful for checking what properties an object has, iterating over them, or validating that expected keys are present before you rely on them:

javascriptjavascript
const requiredFields = ["name", "email"];
const formData = { name: "Sam", email: "", age: 25 };
 
const missing = requiredFields.filter(
  field => !formData[field]
);
 
console.log(missing); // ["email"] (falsy empty string)

Object.values(): Get All Property Values

Object.values() returns an array of the object's property values in the same order as the keys:

javascriptjavascript
const scores = { math: 92, science: 87, english: 95 };
 
const values = Object.values(scores);
 
console.log(values); // [92, 87, 95]

Since values is now a plain array, you can combine it with array methods like reduce to compute statistics that would otherwise need a manual loop:

javascriptjavascript
const total = values.reduce((sum, val) => sum + val, 0);
const average = total / values.length;
 
console.log(average); // 91.33...

Object.entries(): Get Key-Value Pairs

Sometimes you need the property name and its value together, not as two separate arrays. Object.entries() returns an array where each element is a key-value pair, so you never have to line up two arrays by index yourself:

javascriptjavascript
const product = { name: "Laptop", price: 999, inStock: true };
 
const entries = Object.entries(product);
 
console.log(entries);
// [["name", "Laptop"], ["price", 999], ["inStock", true]]

This is the most versatile of the three. With for...of destructuring, you get both the key and value in one loop:

javascriptjavascript
for (const [key, value] of Object.entries(product)) {
  console.log(key + ": " + value);
}

The destructured key-value pair gives you both pieces on every pass, so the loop prints one line per property without any extra lookups:

texttext
name: Laptop
price: 999
inStock: true

Filtering an Object

Since entries gives you an array of pairs, you can filter it the same way you would filter any other array. Use Object.entries() to filter an object by its values, then Object.fromEntries() to rebuild it back into an object:

javascriptjavascript
const inventory = { apples: 5, bananas: 0, oranges: 12 };
 
const inStock = Object.fromEntries(
  Object.entries(inventory).filter(([_, qty]) => qty > 0)
);
 
console.log(inStock); // { apples: 5, oranges: 12 }

Object.fromEntries() is the reverse of Object.entries(). It takes an array of key-value pairs and builds a new object.

Transforming an Object's Values

Map over entries to transform every value while keeping the same keys:

javascriptjavascript
const prices = { apple: 1.0, banana: 0.5, orange: 1.2 };
 
const doubled = Object.fromEntries(
  Object.entries(prices).map(([key, value]) => [key, value * 2])
);
 
console.log(doubled); // { apple: 2, banana: 1, orange: 2.4 }

Converting an Object to a Map

A Map constructor accepts an array of key-value pairs directly, which makes Object.entries() the standard way to convert a plain object into a Map when you want Map-specific features like a built-in size property or reliable iteration regardless of key type:

javascriptjavascript
const config = { theme: "dark", fontSize: 16 };
 
const configMap = new Map(Object.entries(config));
 
console.log(configMap.get("theme")); // "dark"
console.log(configMap.size);        // 2

For more on Map, see the article on /javascript/javascript-map-object-complete-guide.

Converting to Query Strings

A common real-world pattern is building URL query strings from an object:

javascriptjavascript
const params = { search: "javascript", page: 2, limit: 10 };
 
const queryString = Object.entries(params)
  .map(([key, value]) => key + "=" + encodeURIComponent(value))
  .join("&");
 
console.log(queryString); // "search=javascript&page=2&limit=10"

Which Method to Use

You want toUse
Check if an object is emptyObject.keys(obj).length === 0
Get an array of values to sum or averageObject.values(obj)
Loop with both key and valueObject.entries(obj) with for...of
Filter an object by conditionObject.entries() plus filter and fromEntries
Transform all valuesObject.entries() plus map and fromEntries
Convert object to Mapnew Map(Object.entries(obj))

Common Mistake: Forgetting These Are Static Methods

Object.keys() is a static method on the Object constructor. You cannot call it on an object instance:

javascriptjavascript
const obj = { a: 1 };
 
// Wrong
// obj.keys();  // TypeError
 
// Correct
Object.keys(obj); // ["a"]

These methods live on Object, not on individual objects. Calling obj.keys() throws a TypeError because individual objects do not have these methods.

For more on iterating over objects with traditional loops, see the article on /javascript/looping-through-objects-in-javascript-complete-guide.

Rune AI

Rune AI

Key Insights

  • Object.keys(obj) returns an array of the object's own enumerable property names.
  • Object.values(obj) returns an array of the corresponding property values.
  • Object.entries(obj) returns an array of [key, value] pairs.
  • All three return only own enumerable properties, not inherited ones.
  • Combine with array methods like map, filter, and forEach to transform objects.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between Object.keys() and Object.values()?

Object.keys() returns an array of the object's own enumerable property names. Object.values() returns an array of the corresponding property values in the same order.

Does Object.entries() include inherited properties?

No. All three methods (keys, values, entries) return only the object's own enumerable properties. Inherited properties from the prototype chain are excluded.

Can I use Object.entries() on arrays?

Yes. On an array, Object.entries() returns index-value pairs: [["0", "a"], ["1", "b"]]. However, for arrays you usually want the array methods or for...of instead.

Conclusion

Object.keys(), Object.values(), and Object.entries() bridge the gap between objects and arrays. They give you the ability to iterate, filter, map, and transform objects using the full power of array methods. Use keys() when you need property names, values() when you need just the data, and entries() when you need both.