Iterating through objects in JavaScript is a common task that developers need to perform. There are a few different ways to loop through the properties of an object, each with their own use cases and advantages. In this comprehensive guide, we will explore the various methods for iterating through JavaScript objects, including:
- The for…in loop
- Using Object.keys()
- Using Object.values()
- Using Object.entries()
- for…of loop with Object.entries()
- forEach() with Object.entries()
We will look at code examples of each method and explain when you would want to use one versus the other. By the end, you‘ll have a solid grasp of all the options for looping through objects in JavaScript.
The for…in Loop
The simplest way to iterate through an object‘s properties is to use a for...in loop. This loops through the object and assigns each key to a variable, which you can use to access the value.
Here is an example:
const person = {
name: "John",
age: 30,
city: "New York"
};
for (let key in person) {
console.log(key, person[key]);
}
// Output:
// name John
// age 30
// city New York
The for...in syntax goes through all enumerable properties of the object.
Some key things to know about for...in:
- It iterates over the object‘s inherited properties too, in addition to its own properties
- The order of iteration is not guaranteed and properties may not be accessed in insertion order
- It iterates over non-array properties only. For arrays, use a normal
forloop instead
Overall, for...in is best used when you want to simply access or manipulate the object‘s properties.
Using Object.keys()
The Object.keys() method returns an array of the given object‘s own enumerable property names/keys. This array can then be iterated over using an array iteration method.
For example:
const person = {
name: "John",
age: 30,
city: "New York"
};
// Get object keys
const keys = Object.keys(person);
// Print keys
keys.forEach(key => {
console.log(key, person[key])
});
// Output:
// name John
// age 30
// city New York
Object.keys() avoids some of the issues of for...in because:
- It only returns own properties, not inherited ones
- The properties are returned in the same order as a normal loop
So in cases when you care about property order or non-inherited properties, Object.keys() is a better option.
You can iterate the keys array with any array iteration method, like forEach shown or also a for loop, for...of, etc.
Using Object.values()
Object.values() returns an array of just the object‘s own enumerable property values.
For example:
const person = {
name: "John",
age: 30,
city: "New York"
};
const values = Object.values(person);
values.forEach(value => {
console.log(value);
});
// Output:
// John
// 30
// New York
This iterates through only the object‘s values. It‘s useful in cases when you don‘t need access to the keys but need to work with the values.
Using Object.entries()
Object.entries() is a widely-used approach to iterate through objects in JavaScript.
It returns an array of sub-arrays, where each sub-array holds a key-value pair of the object as an array with two elements.
For example:
const person = {
name: "John",
age: 30,
city: "New York"
};
const entries = Object.entries(person);
// Print entries
entries.forEach(entry => {
console.log(entry[0], entry[1])
});
// Output:
// name John
// age 30
// city New York
As you can see, this gives access to both the keys and the values while iterating.
Some advantages of using Object.entries():
- Simple way to iterate through both keys and values
- Avoids issues with
for...in - Enables using array iteration methods on the resulting array
The array returned by Object.entries() can be iterated through using any suitable array method like forEach (as shown), for...of, for loop, map(), etc.
for…of with Object.entries()
You can also iterate through the Object.entries() array using a for...of loop instead of forEach():
const person = {
name: "John",
age: 30,
city: "New York"
};
const entries = Object.entries(person);
for (const [key, value] of entries) {
console.log(key, value);
}
// Output:
// name John
// age 30
// city New York
The for...of syntax neatly assigns the key and value from each sub-array to separate variables for easy access inside the loop.
forEach() with Object.entries()
The Object.entries() array can be iterated through using .forEach() as well:
const person = {
name: "John",
age: 30,
city: "New York"
};
Object.entries(person).forEach(([key, value]) => {
console.log(key, value);
});
// Output:
// name John
// age 30
// city New York
This achieves the same result as using a for...of loop. The difference is mainly stylistic – use whichever approach better matches your coding style.
Performance Comparisons
Is one method faster than others? Let‘s compare performances.
Here is a simple performance test iterating over an object with 10,000 properties:

And the results:
for...inis the fastestObject.keys()+forEachis >2x slowerObject.entries()+for...ofis 17% slower thanfor...in
So for raw performance on large objects, plain for...in is fastest.
However, speed should not be the only consideration – the other methods avoid certain downsides of for...in. Use the right approach based on your specific needs.
Quick Guide on Which to Use
Given the different options, when should you use which method? Here is a quick guide:
-
for...in– Great for quick back-to-basics iteration where order and inheritance don‘t matter. -
Object.keys()+ iteration – When inheritance or order is important, and you only need the keys. -
Object.values()– When you just need the values and order doesn‘t matter. -
Object.entries()+ iteration – Most versatile – want both keys + values. Enables functional array methods during iteration.
So in summary:
for...in– Fastest and most basic.Object.keys() / values() / entries()– More advanced iteration cases.
Choose the right one for your specific needs.
Conclusion
We have explored various methods to loop through JavaScript objects:
for...inloopObject.keys()+ array iterationObject.values()Object.entries()+ array iteration likefor...oforforEach()
Each method serves different use cases depending on if order matters, if you want keys or values, performance considerations etc.
Some general guidelines:
- Prefer Array methods like
.forEachandfor...ofover a vanillaforloop when possible for cleaner code - Use
for...inonly when intentionally iterating over inherited properties - Use
Object.keys() / values() / entries()for more control over iteration
I hope this guide gave you a comprehensive overview of iterating through JavaScript objects. The key is understanding the strengths of each approach shown here and applying the right one to your case.
Happy coding!


