Object.values( ) In JavaScript

27 Sep, 2021
Object and Object Constructors in JavaScript? In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.  
Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.  
For instance, consider the following code: 
function Automobile(color) {
  this.color=color;
}

var vehicle1 = new Automobile ("red");
The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”. When new Automobile() is called, JavaScript does two things:  
It creates a fresh new object(instance) Automobile() and assigns it to a variable.It sets the constructor property i.e “color” of the object to Automobile.
It creates a fresh new object(instance) Automobile() and assigns it to a variable.
It sets the constructor property i.e “color” of the object to Automobile.
Object.values() Method 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 is a loop is applied to the properties. Object.values() takes the object as an argument of which the enumerable own property values are to be returned and returns an array containing all the enumerable property values of the given object.Applications:  
Object.values() is used for returning enumerable property values of a simple array.
Object.values() is used for returning enumerable property values of an array like object.
Object.values() is used for returning enumerable property values of an array like object with random key ordering.
Syntax:  
Object.values(obj)
Parameters Used:  
obj : It is the object whose enumerable property values are to be returned.
obj : It is the object whose enumerable property values are to be returned.
Return Value: Object.values() returns an array containing all the enumerable property values of the given object.  
Examples of the above function are provided below.Examples:  
Input : var check = ['x', 'y', 'z'];
        console.log(Object.values(check));
Output : Array ["x", "y", "z"]
Explanation: 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.  
Input : var object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
        console.log(Object.values(object));
Output : Array ["23", "geeksforgeeks", "true"]
Explanation: 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.  
Input : var object = { 70: 'x', 21: 'y', 35: 'z' };
        console.log(Object.values(object));
Output : Array ["y", "z", "x"]
Explanation: 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.Codes for the above function are provided below.Code 1: 
javascript
<script> // Returning enumerable property values of a simple arrayvar check = ['x', 'y', 'z'];console.log(Object.values(check)); </script>
OUTPUT :  
Array ["x", "y", "z"]
Code 2: 
javascript
<script> // Returning enumerable property values// of an array like object.var object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };console.log(Object.values(object)); </script>
OUTPUT :  
Array ["23", "geeksforgeeks", "true"]
Code 3: 
javascript
<script> // Returning enumerable property values// of an array like object.var object = { 70: 'x', 21: 'y', 35: 'z' };console.log(Object.values(object)); </script>
OUTPUT :  
 Array ["y", "z", "x"]
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 Browser:
Chrome 54 and above
Edge 14 and above
Firefox 47 and above
Opera 41 and above
safari 10.1 and above
Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values 
ysachin2314
javascript-functions
javascript-object
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.