JavaScript Object.values()

Summary: in this tutorial, you will learn how to use the JavaScript Object.values() method to access the own enumerable properties of an object.

Before ES2017, you used a for...in loop and Object.hasOwnProperty()  method to access values of own enumerable properties of an object. For example:

const person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 25
};

for (const key in person) {
    if (person.hasOwnProperty(key)) {
        const value = person[key];
        console.log(value);

    }
}Code language: JavaScript (javascript)

Output:

John
Doe
25

ES2017 introduces a new method called Object.values() method that allows you to return an array of own enumerable property’s values of an object.

The following shows the syntax of the Object.values():

Object.values(obj);Code language: JavaScript (javascript)

The Object.values() method accepts an object and returns its own enumerable property’s values as an array. See the following example:

const person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 25
};

const profile = Object.values(person);

console.log(profile);
Code language: JavaScript (javascript)

Output:

[ 'John', 'Doe', 25 ]
Code language: JSON / JSON with Comments (json)

Object.values() vs. for…in

The Object.values() returns own enumerable properties while the for...in loop iterates properties in the prototype chain.

Technically, if you use the for...in loop with the Object.hasOwnProperty() method, you will get the same set of values as the Object.values().

Was this tutorial helpful ?