What are Object.keys() and Object.entries() in JavaScript?

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a dog, for example. A dog is an object, with properties. A dog has a color, eye color, weight, a type of skin it is made of, etc. In the same way, JavaScript objects can have properties that define their characteristics.

Objects are used in building software and implementing various features such as authentication, and sending and receiving data over a server.

Objects have two important methods: Object.entries() and Object.keys().

Object.keys()

This returns a key pair of an object in array. This method always returns an array.

let user = {
name: "John",
age: 30
};
console.log(Object.keys(user))

From our code, we have an object that has keys of name and age. We parse in the name of the object to the Object.keys() function so we get just the keys of the array.

Object.entries()

This method returns an array containing the (key, value) pairs of an object.

let user = {
name: "John",
age: 30
};
console.log(Object.entries(user))

Object.values()

This method is similar to Object.keys(), but instead of returning an array of keys, the method returns an array of values in the object.

Note: Object.keys(), Object.values(),Object.entries() return an array.

let user = {
name: "John",
age: 30
};
console.log(Object.values(user))

Free Resources