Object.values()
ES2017+Returns an array of a given object's own enumerable property values.
Syntax
Object.values(obj)Parameters
obj Object The object whose enumerable own property values are to be returned
Return Value
An array containing the object's own enumerable property values
Examples
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj)); 📌 When to Use
Use Object.values() when you only need the values of an object without caring about the keys. Ideal for calculating sums, finding min/max values, filtering based on value conditions, or passing values to functions that expect arrays.
⚠️ Common Mistakes
Forgetting that Object.values() returns values in the same order as Object.keys() - property insertion order, not sorted order.
Not handling null or undefined values in the resulting array - always validate or filter if your object may contain these.
Using Object.values() when you also need keys - use Object.entries() instead to avoid a second iteration.
✅ Best Practices
Chain with array methods for powerful transformations: Object.values(prices).reduce((sum, price) => sum + price, 0)
Use with Set for unique values: new Set(Object.values(obj)) to get unique property values.
Prefer Object.values() over Object.keys().map(k => obj[k]) for cleaner, more readable code.
⚡ Performance Notes
Object.values() has similar performance characteristics to Object.keys() - O(n) time and space complexity. It is slightly slower than Object.keys() in some engines because it must retrieve property values, not just names. For objects with complex nested values, be aware that the returned array contains references, not copies.
🌍 Real World Example
Shopping Cart Total Calculator
Calculate the total price of items in a shopping cart object using Object.values() and reduce().
const cart = {
apple: { price: 1.50, quantity: 3 },
banana: { price: 0.75, quantity: 6 },
orange: { price: 2.00, quantity: 2 }
};
const total = Object.values(cart)
.reduce((sum, item) => sum + (item.price * item.quantity), 0);
console.log('Total: $' + total.toFixed(2));
// Total: $13.00
// Find the most expensive item
const mostExpensive = Object.values(cart)
.reduce((max, item) => item.price > max.price ? item : max);
console.log('Most expensive: $' + mostExpensive.price);
// Most expensive: $2.00