The Wayback Machine - https://web.archive.org/web/20240801002852/https://www.geeksforgeeks.org/difference-between-object-keys-and-object-entries-methods-in-javascript/
Open In App

Difference between Object.keys() and Object.entries() methods in JavaScript

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties:

Object.keys() returns an array of a given object’s own enumerable property names, while Object.entries() returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.

Object.keys()

Object.keys() iterates over an object’s own enumerable properties, returning an array containing the keys of those properties.

Example: To demonstrate the implementation of the Object.Keys() method in JavaScript.

JavaScript
const obj = {
    name: 'John',
    age: 30,
    city: 'New York'
};

const keys = Object.keys(obj);
console.log(keys);

Output
[ 'name', 'age', 'city' ]

Object.entries()

Object.entries() iterates over an object’s own enumerable string-keyed property [key, value] pairs, returning an array of arrays.

Example: To demonstrate the implementation of the Object.entries() method in JavaScript.

JavaScript
const obj = {
    name: 'John',
    age: 30,
    city: 'New York'
};

const entries = Object.entries(obj);
console.log(entries);

Output
[ [ 'name', 'John' ], [ 'age', 30 ], [ 'city', 'New York' ] ]

Difference between Object.keys() and Object.entries()

Features

Object.keys()

Object.entries()

Return Value

Array of object’s keys

Array of [key, value] pairs

Iteration Output

Returns keys as strings

Returns [key, value] pairs as arrays

Use Case

Useful for iterating over keys

Useful for iterating over [key, value] pairs

Example Output

`[“name”, “age”, “city”]`

`[[“name”, “John”], [“age”, 30], [“city”, “New York”]]`


Similar Reads

Difference between Object.values and Object.entries Methods
The object is the parent class from which all the javascript objects are inherited and these two methods are the static methods of the Object class as they are called by the class name of the Object class. JavaScript Object.values() Method In the same order as a for...in the loop, the Object.values() method returns an array of the enumerable proper
2 min read
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
In JavaScript, Object.keys() and Object.getOwnPropertyNames() both retrieve properties of an object but differ in scope. Object.keys() returns an array of an object's own enumerable property names. In contrast, Object.getOwnPropertyNames() returns an array of all own property names, including non-enumerable properties. These methods are useful for
3 min read
JavaScript Object entries() Method
JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually. Syntax:Object.entries(obj);Parameters:obj: It is the object whose enu
3 min read
Difference Between for...in and Object.keys() in JavaScript
The for...in and Object.keys() in JavaScript are used to iterate over the properties of an object. While they might seem similar at first glance they have distinct usage, behavior, and characteristics. This article will explore these differences in detail. These are the following topics that we are going to discuss: Table of Content What is for...i
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
TypeScript Array Object.entries() Method
The Object.entries() method in TypeScript is used for creating an array of key-value pairs from an object's keys and values. This method simplifies the process of iterating over an object's properties and enables easier data manipulation. SyntaxObject.entries(obj);Parameterobj: It takes the object as a parameter whose key-value pair you want to sto
3 min read
JavaScript typedArray.entries() with Examples
The Javascript typedArray.entries() is an inbuilt function in JavaScript that gives a new array iterator object containing the key and value pairs of the given typedArray object. Syntax: typedArray.entries() Parameter: It does not accept any parameters. Return value It returns a new array iterator object containing the key and value pairs of the gi
2 min read
JavaScript Map entries() Method
JavaScript Map.entries() method is used for returning an iterator object which contains all the [key, value] pairs of each element of the map. It returns the [key, value] pairs of all the elements of a map in the order of their insertion. The Map.entries() method does not require any argument to be passed and returns an iterator object of the map.
4 min read
JavaScript Set entries() Method
The Set.entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in the order of their insertion. Although a Set does not contain key-value pairs, to keep similarities with the map.entries() method, it will return key-value pairs where both key and value will have the same value. Sy
3 min read
JavaScript Array entries() Method
The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array. It allows iterating over arrays and accessing both the index and value of each element sequentially. Syntax:array.entries()Parameters:This method does not accept any parameters.Return value:Returns an array of indexes and value
3 min read
three90RightbarBannerImg