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:
<!DOCTYPE html><html><head><title>Object.keys() VS Object.entries()</title></head><bodystyle="text-align:center"><h1style="color: green">GeeksforGeeks</h1><b>Object.keys() VS Object.entries()</b><p>Click on the button to get allproperties values.</p><buttononclick="getValues()">Click</button><scripttype="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><bodystyle="text-align:center"><h1style="color: green">GeeksforGeeks</h1><b>Object.keys() VS Object.entries()</b><p>Click on the button to getall properties values.</p><buttononclick="getValues()">Click</button><scripttype="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:




.png)