Skip to content

JavaScript Object values() | Method

  • by

JavaScript Object values() method returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Object.values(obj)

Note: that Object.values() only returns values for the object’s own properties, not for any properties it inherits from its prototype chain. Also, it only returns values for enumerable properties, which are those that have their enumerable attribute set to true.

Example JavaScript Object values()

A simple example code accepts an object and returns its own enumerable property values as an array.

<!DOCTYPE html>
<html>
<body>

<script>
    const person = {
      firstName: 'John',
      lastName: 'Steve',
      age: 25
    };

    const profile = Object.values(person);

    console.log(profile);

  </script>

</body>
</html> 

Output:

JavaScript Object values() Method

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().

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

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

    }
}

Output:

John
Doe
25

Returning enumerable property values of an array-like object.

<script>
 

    var object = { 70: 'x', 21: 'y', 35: 'z' };
    console.log(Object.values(object));
 
</script>

Output: [“y”, “z”, “x”]

How to Get Object Values?

Answer: Use Object.values() to access the values of enumerable properties of the object.

   let student = { name: 'KRUNAL', education: 'BE IT' };
   console.log(Object.values(student));

Output: [ “KRUNAL”, “BE IT” ]

Do comment if you have any doubts or suggestions on this JS object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *