Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

My Personal Notes arrow_drop_up
Last Updated : 15 Apr, 2020
Like Article
Save Article
Similar Reads
Related Tutorials