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.
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.
| Method | Returns | Example |
|---|---|---|
Object.keys(obj) | Array of property names | name, age |
Object.values(obj) | Array of property values | Alex, 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:
const user = { name: "Alex", age: 28, role: "admin" };
const keys = Object.keys(user);
console.log(keys); // ["name", "age", "role"]
console.log(keys.length); // 3This is useful for checking what properties an object has, iterating over them, or validating that expected keys are present before you rely on them:
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:
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:
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:
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:
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:
name: Laptop
price: 999
inStock: trueFiltering 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:
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:
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:
const config = { theme: "dark", fontSize: 16 };
const configMap = new Map(Object.entries(config));
console.log(configMap.get("theme")); // "dark"
console.log(configMap.size); // 2For 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:
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 to | Use |
|---|---|
| Check if an object is empty | Object.keys(obj).length === 0 |
| Get an array of values to sum or average | Object.values(obj) |
| Loop with both key and value | Object.entries(obj) with for...of |
| Filter an object by condition | Object.entries() plus filter and fromEntries |
| Transform all values | Object.entries() plus map and fromEntries |
| Convert object to Map | new 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:
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
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.
Frequently Asked Questions
What is the difference between Object.keys() and Object.values()?
Does Object.entries() include inherited properties?
Can I use Object.entries() on arrays?
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.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.