What's the difference between Object.entries and Object.keys?

Object.keys returns only the own property names and works for ES5.

Object.entries returns an array of arrays with key and value and works from ES6.

If you need only keys or like to filter the keys, then take Object.keys, otherwise Object.entries.


Object.keys(obj) – returns an array of keys.

Object.entries(obj) – returns an array of [key, value] pairs.

Consider the below example.

 let user = {
 name: "John",
 age: 30
};

Object.keys(user) = ["name", "age"]

Object.entries(user) = [ ["name","John"], ["age",30] ]

When you want a key, value pair, you would use Object.entries. When you just want the key, you would use Object.keys.


Object.keys gives you an array of the keys in that object. where as Object.entries returns an array of arrays containing key value pair.

const object = { foo: 'bar', baz: 42 };
console.log(Object.keys(object));
console.log(Object.entries(object));

Tags:

Javascript