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

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • Output:

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

  • Program:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.