What is Object.values in JavaScript?

By using Object.values, we can get the values of the object. This method will return an array that contains the object’s own enumerable property values. The order of the returned values is the same as the order that it occurs in the for…in loop.

Syntax

Object.values(obj)

Example

let employee = {companyName : "Educative"};
let manager = Object.create(employee);
manager.name = "Ram";
manager.designation = "Manager";
manager.getName = function(){
return this.name;
}
manager[Symbol("symbol")] = "symbol";
Object.defineProperty(manager, "non-enumerable", {
enumerable: false
})
console.log("The manager object is ", manager);
console.log("\nValues of the manager object is ", Object.values(manager))
// the values of non-enumerable properties,symbols and inherited properties are not included in the keys

In the above code, we have:

  1. Created the manager object from the employee object.
  2. Added symbol as a property key to the manager object.
  3. Added a non-enumerable property to the manager object.

When we call the Object.values method by passing the manager object as an argument, we will get an array of its own object property values. The returned array doesn’t include the values of:

  1. Symbols
  2. Inherited propertiesIn our case, employee object properties are inherited properties, so those property values are not included.
  3. Non-enumerable propertiesThe properties that don’t occur in the for…in loop; in our case, the non-enumerable property of manager object.