The syntax of the values()
method is:
Object.values(obj)
The values()
method, being a static method, is called using the Object
class name.
values() Parameters
The values()
method takes in:
- obj - The object whose enumerable properties are to be returned.
Return value from values()
- Returns an array of strings that represent all the enumerable property values of the given object.
Note: The ordering of the properties is the same as that when looping over them manually.
Example: Using Object.values()
// Array objects
const arr = ["JavaScript", "Python", "C"];
console.log(Object.values(arr)); // [ 'JavaScript', 'Python', 'C' ]
// Array-like objects
const obj = { 65: "A", 66: "B", 67: "C" };
console.log(Object.values(obj)); // [ 'A', 'B', 'C' ]
// random key ordering
const obj1 = { 42: "a", 22: "b", 71: "c" };
console.log(Object.values(obj1)); // ['b', 'a', 'c'] -> Arranged in key's numerical order
// string -> from ES2015+, non objects are coerced to object
const string = "code";
console.log(Object.values(string)); // [ 'c', 'o', 'd', 'e' ]
Output
[ 'JavaScript', 'Python', 'C' ] [ 'A', 'B', 'C' ] [ 'b', 'a', 'c' ] [ 'c', 'o', 'd', 'e' ]
Recommended Reading: Javascript Object.keys()