Open In App

JavaScript Object values() Method

JavaScript object.values() method is used to return 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 the object manually if a loop is applied to the properties. Object.values() takes the object as an argument of which the enumerable property values are to be returned and returns an array containing all the enumerable property values of the given object.

Syntax:  

Object.values(obj);

Parameters:  

Return Value: 

Returns an array containing all the enumerable property values of the given object. 



Example 1: In this example, an array “check” has three property values [‘x’, ‘y’, ‘z’] and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 




// Returning enumerable property values of a simple array
let check = ['x', 'y', 'z'];
console.log(Object.values(check));

Output: 



Array ["x", "y", "z"]

Example 2: In this example, an array-like object “check” has three property values { 0: ’23’, 1: ‘geeksforgeeks’, 2: ‘true’ } and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 




// Returning enumerable property values
// of an array like object.
let object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
console.log(Object.values(object))

Output:  

Array ["23", "geeksforgeeks", "true"]

Example 3: In this example, an array-like object “check” has three property values { 70: ‘x’, 21: ‘y’, 35: ‘z’ } in random ordering and the object.values() method returns the enumerable property values of this array in the ascending order of the value of indices.




// Returning enumerable property values
// of an array like object.
let object = { 70: 'x', 21: 'y', 35: 'z' };
console.log(Object.values(object));

Output:  

 Array ["y", "z", "x"]

Applications:  

Exceptions:  

Supported Browsers:

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.


Article Tags :