Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between Object.values and Object.entries Methods

  • Last Updated : 01 Dec, 2021

The object is the parent class from which all the javascript objects are inherited and these two methods are the static methods of the Object class as they are called by the class name of Object class.

Object.values() method: In the same order as a for…in the loop, the Object.values() method returns an array of the enumerable property values of an object. This is the only difference: a for…in the loop also enumerates properties in the prototype chain.

Syntax:

Object.values(object)
  • Parameter: This object is enumerable with its own properties whose values should be returned.
  • Return Type: Array of values

Example: Users can open the console into the chrome web browser by pressing ctrl + shift + I.

HTML




<script>
    let fullname = {
        firstname: "geeks",
        middlename: "for",
        lastname: "geeks",
    };
    let name = Object.values(fullname);
    console.log(name);
</script>

Output:

["geeks", "for", "geeks"]

Object.entries() method:

This method returns an array of the keys and values of the objects’ enumerable string-keyed properties. It works similarly to iterating with a for…in the loop, with the exception that a for…in the loop also enumerates properties in the prototype chain.

Syntax:

Object.entries(object)
  • Parameter: Returns the object’s own enumerable string-keyed property [key, value] pairs.
  • Return: This is an array of the given object’s string-keyed property [key, value] pairs.

Example:

HTML




<script>
    let fullname = {
        firstname: "geeks",
        middlename: "for",
        lastname: "geeks",
    };
    let name = Object.entries(fullname);
    console.log(name);
</script>

Output:

[["firstname", "geeks"], 
 ["middlename", "for"], 
 ["lastname", "geeks"]]

Difference between object.value and object.entries methods:

object.valueobject.entries
It returns the array of values of a particular objectIt returns an array of arrays of key-value pair
It returns only values of all keys present in an objectIt returns both keys as well as their values present in an object

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!