Skip to main content

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() and Object.entries() it forms the trio for treating an object as a collection.

Quick example

javascript
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

javascript
Object.values(obj)
  • obj is 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

  1. It returns own properties only, never inherited ones:

    javascript
    const person = { species: 'human' }; const user = Object.create(person); user.name = 'Maria'; console.log(Object.values(user)); // ['Maria']
  2. It returns enumerable properties only:

    javascript
    const obj = {}; Object.defineProperty(obj, 'hidden', { value: 42, enumerable: false }); obj.visible = 10; console.log(Object.values(obj)); // [10]
  3. The order of the values follows the order of the keys:

    javascript
    Object.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

javascript
const fruits = { 3: 'orange', 1: 'apple', 2: 'banana' }; console.log(Object.values(fruits)); // ['apple', 'banana', 'orange'], sorted by the numeric keys

The 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

javascript
const product = { name: 'Shirt', price: 2500, inStock: true }; Object.values(product).forEach(value => console.log(value));

Output:

javascript
Shirt 2500 true

Because the result is a plain array, every array method works on it. The classic example is summing the values:

javascript
const cart = { apples: 120, bread: 45, milk: 80 }; const total = Object.values(cart).reduce((sum, price) => sum + price, 0); console.log(total); // 245

The trio: Object.keys, Object.values and Object.entries

MethodWhat 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 doesReturnsSees the prototypeData type
Collects all property valuesAn array of valuesNoArray

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, 3 are 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(). values throws the property names away, so when you need the key inside the loop use entries.
  • Passing null or undefined. Object.values(null) throws a TypeError. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.