Open In App

What is the difference between Object.keys() and Object.entries() methods in JavaScript ?

Last Updated : 15 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The Object.keys() method in JavaScript returns an array whose elements are strings corresponding to the enumerable properties

The Object.entries() method in JavaScript returns an array consisting of enumerable property [key, value] pairs of the object.

The only difference is that the Object.keys() method returns only the own property names and it only works for ES5 while Object.entries() method returns an array of arrays with key and value and it works from ES6.

Example 1: This example implements the Object.keys() method.

  • Program:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Object.keys() VS Object.entries()
        </title>
    </head>
      
    <body style="text-align:center">
          
        <h1 style="color: green"
            GeeksforGeeks 
        </h1>
          
        <b
            Object.keys() VS Object.entries()
        </b>
          
        <p>
            Click on the button to get all 
            properties values.
        </p>
          
        <button onclick="getValues()">
            Click
        </button>
          
        <script type="text/javascript">
            function getValues() {
                var object = {
                    2: 'Geeks1',
                    23: 'Geeks2',
                    52: 'Geeks3'
                };
      
                let valuesArray = Object.keys(object);
      
                for (let value of valuesArray) {
                    document.write(value + "<br>");
                }
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:

Example 2: This example implements the Object.entries() method.

  • Program:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Object.keys() VS Object.entries()
        </title>
    </head>
      
    <body style="text-align:center">
      
        <h1 style="color: green"
            GeeksforGeeks 
        </h1>
          
        <b
            Object.keys() VS Object.entries()
        </b>
          
        <p>
            Click on the button to get 
            all properties values.
        </p>
          
        <button onclick="getValues()">
            Click
        </button>
          
        <script type="text/javascript">
            function getValues() {
                var object = {
                    2: 'Geeks1',
                    23: 'Geeks2',
                    52: 'Geeks3'
                };
      
                let valuesArray = Object.entries(object);
      
                for (let value of valuesArray) {
                    document.write(value + "<br>");
                }
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:


Similar Reads

Difference between Object.values and Object.entries Methods
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 the Object class. JavaScript Object.values() Method In the same order as a for...in the loop, the Object.values() method returns an array of the enumerable proper
2 min read
JavaScript Object entries() Method
JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually. Syntax:Object.entries(obj);Parameters:obj: It is the object whose enu
2 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
TypeScript Array Object.entries() Method
The Object.entries() is a method in TypeScript, that is very useful for creating an array of key-value pairs from the keys and values of the object passed to it as a parameter. Syntax:Object.entries(obj);Parameter:obj: It takes the object as a parameter whose key-value pair you want to store in the array.Return Value:It will return an array of arra
2 min read
JavaScript typedArray.entries() with Examples
The Javascript typedArray.entries() is an inbuilt function in JavaScript that gives a new array iterator object containing the key and value pairs of the given typedArray object. Syntax: typedArray.entries() Parameter: It does not accept any parameters. Return value It returns a new array iterator object containing the key and value pairs of the gi
2 min read
JavaScript Set entries() Method
The Set.entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in the order of their insertion. Although a Set does not contain key-value pairs, to keep similarities with the map.entries() method, it will return key-value pairs where both key and value will have the same value. Sy
2 min read
JavaScript Map entries() Method
JavaScript Map.entries() method is used for returning an iterator object which contains all the [key, value] pairs of each element of the map. It returns the [key, value] pairs of all the elements of a map in the order of their insertion. The Map.entries() method does not require any argument to be passed and returns an iterator object of the map.
3 min read
JavaScript Array entries() Method
The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array. It allows iterating over arrays and accessing both the index and value of each element sequentially. Array entries() Syntaxarray.entries()Array entries() Parameters This method does not accept any parameters. Array entries() Re
2 min read
How to get all property values of a JavaScript Object (without knowing the keys) ?
Method 1: Using Object.values() Method: The Object.values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object. Therefore, the keys are not required to be known to get all the property values. Syntax: let valuesArray = Object.values(exampleOb
2 min read
How to replace the names of multiple object keys with the values provided using JavaScript ?
The following approach covers how to replace the names of multiple object keys with the values provided by the user using JavaScript. Problem Statement: You are given an object which contains different key-value pairs in which key symbolizes the property and value itself is known as the property value, and you need to change one or more key's origi
4 min read