Open In App

JavaScript Object values() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  • obj: It is the object whose enumerable property values are to be returned.

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. 

javascript




// 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. 

javascript




// 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.

javascript




// 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:  

  • Object.values() is used for returning enumerable property values of a simple array, array-like object, and array-like object with random key ordering.

Exceptions:  

  • It causes a TypeError if the argument passed is not an object.
  • If an object is not passed as an argument to the method, then it persuades it and treats it as an object.

Supported Browsers:

  • Chrome 54 and above
  • Edge 14 and above
  • Firefox 47 and above
  • Opera 41 and above
  • safari 10.1 and above

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



Last Updated : 13 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads