The Object.values() method
Object.values() is a built-in JavaScript method that returns an array of the values of an object's own (non-inherited) enumerable properties. It is the mirror image of Object.keys(): that one gives you property names, this one gives you the data itself.
Theory
TL;DR
- Returns an array of values of the object's properties.
- Own properties only, nothing from the prototype.
- Enumerable properties only.
- The order of the values matches the order of the keys in
Object.keys()exactly. - Integer-like keys are sorted ascending, so values may not follow the order you wrote them in.
- Together with
Object.keys()andObject.entries()it forms the trio for treating an object as a collection.
Quick example
const user = {
name: 'Maria',
age: 25,
city: 'Kyiv'
};
console.log(Object.values(user));
// ['Maria', 25, 'Kyiv']Only the values of the object's properties came back, without their names.
Syntax
Object.values(obj)objis the object you want the values from.- It returns an array of values in the same order as
Object.keys().
Types are preserved: numbers stay numbers, booleans stay booleans, and nested objects end up in the array by reference.
Which properties end up in the result
-
It returns own properties only, never inherited ones:
javascriptconst person = { species: 'human' }; const user = Object.create(person); user.name = 'Maria'; console.log(Object.values(user)); // ['Maria'] -
It returns enumerable properties only:
javascriptconst obj = {}; Object.defineProperty(obj, 'hidden', { value: 42, enumerable: false }); obj.visible = 10; console.log(Object.values(obj)); // [10] -
The order of the values follows the order of the keys:
javascriptObject.keys(obj); // ['visible'] Object.values(obj); // [10]
Values stored under symbol keys are not returned either: Symbol takes no part in this trio of methods.
An example with numeric keys
const fruits = { 3: 'orange', 1: 'apple', 2: 'banana' };
console.log(Object.values(fruits));
// ['apple', 'banana', 'orange'], sorted by the numeric keysThe engine always orders integer-like keys ascending, so the values do not come out in the order they were written in the literal. When the order matters, use an array or a Map.
Iterating the values
const product = { name: 'Shirt', price: 2500, inStock: true };
Object.values(product).forEach(value => console.log(value));Output:
Shirt
2500
trueBecause the result is a plain array, every array method works on it. The classic example is summing the values:
const cart = { apples: 120, bread: 45, milk: 80 };
const total = Object.values(cart).reduce((sum, price) => sum + price, 0);
console.log(total); // 245The trio: Object.keys, Object.values and Object.entries
| Method | What it returns |
|---|---|
Object.keys(obj) | an array of keys |
Object.values(obj) | an array of values |
Object.entries(obj) | an array of [key, value] pairs |
A summary of the method itself:
| What it does | Returns | Sees the prototype | Data type |
|---|---|---|---|
| Collects all property values | An array of values | No | Array |
Common mistakes
- Expecting inherited values. Anything living on the prototype is excluded, exactly as with
Object.keys(). - Relying on declaration order with numeric keys. Keys like
1,2,3are always sorted ascending and the values follow them. - Reaching for a value by index.
Object.values(obj)[0]is not "the first property added" but the first key in the order the engine defines. Reading it by name,obj.name, is safer. - Confusing it with
Object.entries().valuesthrows the property names away, so when you need the key inside the loop useentries. - Passing
nullorundefined.Object.values(null)throws aTypeError. For a string you get the individual characters:Object.values('ab')returns['a', 'b']. - Treating the result as a copy of the data. Object values are copied by reference, so mutating such an element of the array mutates the original object too.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.