Difference Between for...in and Object.keys() in JavaScript
Last Updated :
08 Jul, 2024
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:
What is for...in?
The for...in statement is a looping construct that iterates over the enumerable properties of an object. It goes through each property name (key) in the object and allows to perform the actions with these properties.
Characteristics:
- Iterates over all enumerable properties including the inherited properties.
- Can iterate over properties added to the object via the prototype chain.
- Useful for general property enumeration when dealing with both own and inherited properties.
Applications:
- Iterating over all properties of an object.
- Useful when we want to include the inherited properties in the iteration.
Example: In this example, the for...in loop iterates over the properties name and age of the person object as well as the gender property inherited from the Object.prototype.
JavaScript
let person = {
name: 'kumar',
age: 25
};
Object.prototype.gender = 'Male';
for (let key in person) {
console.log(key + ': ' + person[key]);
}
Outputname: kumar
age: 25
gender: Male
What is Object.keys()?
The Object.keys() method returns an array of the given object's own enumerable property names in the same order as provided by the for...in loop but without including the properties from the prototype chain.
Characteristics:
- Returns an array of the object's own enumerable properties.
- Does not include the inherited properties.
- Useful for obtaining a list of the property names for an object's own properties.
Applications:
- Extracting a list of the object's own property names.
- Avoiding the enumeration of the properties from the prototype chain.
Example: In this example Object.keys(person) returns an array containing only the name and age properties excluding the inherited gender property.
JavaScript
let person = {
name: 'kumar',
age: 30
};
Object.prototype.gender = 'Male';
let keys = Object.keys(person);
console.log(keys);
keys.forEach(key => {
console.log(key + ': ' + person[key]);
});
Output[ 'name', 'age' ]
name: kumar
age: 30
Difference Between for...in and Object.keys()
Characteristics | for...in | Object.keys() |
---|
Iterates over | All enumerable properties | Own enumerable properties only |
---|
Returns | The Property names as strings | The Array of property names |
---|
Includes inherited properties | Yes | No |
---|
Use case | When you need to iterate over all properties | When you need a list of the own property names |
---|
Conclusion
Understanding the differences between the for...in and Object.keys() is crucial for effectively working with the objects in JavaScript. While for...in is useful for the iterating over all enumerable properties Object.keys() provides a cleaner approach for retrieving only an object's own properties. Choosing the right method depends on the specific use case and the properties we need to the work with.
Similar Reads
Difference between Object.keys() and Object.entries() methods in JavaScript 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 g
2 min read
Differences Between for-in and for-of Statement in JavaScript The for..in loop is designed for iterating over an object's keys or property names, making it useful when accessing each property in an object. Although it can also be used to iterate over the indices of an array. Conversely, the for..of loop is intended to iterate directly over values in iterable c
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-en
2 min read
Difference between forEach and for loop in Javascript In JavaScript, both forEach and for loops are used to iterate over arrays or collections, but they differ in usage and behavior. forEach is a higher-order function that simplifies iteration with built-in array methods, while for loops offer more control, flexibility, and broader application.For Loop
4 min read
Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It
2 min read