DEV Community

Cover image for Understanding Object Iteration in JavaScript: `for...of` vs `for...in`
Dharmendra Kumar
Dharmendra Kumar

Posted on

Understanding Object Iteration in JavaScript: `for...of` vs `for...in`

Iterating over objects is a common task in JavaScript, but knowing the correct technique for each situation can make your code cleaner and more efficient. This article explains why you can't use for...of directly with objects, offers alternative approaches, and provides best practices for iterating over objects.

Table of Contents

  1. Introduction to Iteration in JavaScript
  2. Why for...of Doesn’t Work with Objects
  3. Techniques for Iterating Over Objects
    • Using for...in
    • Using Object.keys()
    • Using Object.values()
    • Using Object.entries()
  4. Comparison of Object Iteration Techniques
  5. Comparison Between for...in and for...of
  6. Advanced Example: Iterating Over Nested Objects
  7. Best Practices for Object Iteration in JavaScript

1. Introduction to Iteration in JavaScript

In JavaScript, iterating over data structures is an essential part of handling complex datasets. While arrays and strings are iterable objects, plain objects (key-value pairs) require different methods for iteration. When developers try to use for...of on objects, they often encounter issues.


2. Why for...of Doesn’t Work with Objects

The for...of loop is used to iterate over iterable objects like arrays, strings, Maps, and Sets. Plain JavaScript objects, however, are not iterable by default.

Example: Trying for...of with an Object

const user = { name: 'John', age: 30 };

for (const value of user) {
  console.log(value);
}
// TypeError: user is not iterable
Enter fullscreen mode Exit fullscreen mode

Attempting to use for...of on a plain object throws a TypeError. This happens because objects in JavaScript do not have a [Symbol.iterator] method, which is required for the for...of loop.


3. Techniques for Iterating Over Objects

To iterate over objects in JavaScript, several techniques are available:

3.1 Using for...in

The for...in loop iterates over an object’s enumerable properties. It loops through the keys of the object.

const user = { name: 'John', age: 30 };

for (const key in user) {
  console.log(key, user[key]);
}
// Output: 
// name John
// age 30
Enter fullscreen mode Exit fullscreen mode
  • Pros: Simple and direct.
  • Cons: Iterates over inherited properties if they are enumerable, which might cause unexpected behavior.

3.2 Using Object.keys()

Object.keys() returns an array of the object’s own property keys, allowing you to use for...of to iterate over them.

const user = { name: 'John', age: 30 };

for (const key of Object.keys(user)) {
  console.log(key, user[key]);
}
// Output: 
// name John
// age 30
Enter fullscreen mode Exit fullscreen mode
  • Pros: Only iterates over the object’s own properties.
  • Cons: Only retrieves the keys, not the values.

3.3 Using Object.values()

Object.values() returns an array of the object’s property values, which can then be iterated over with for...of.

const user = { name: 'John', age: 30 };

for (const value of Object.values(user)) {
  console.log(value);
}
// Output: 
// John
// 30
Enter fullscreen mode Exit fullscreen mode
  • Pros: Direct access to values without dealing with keys.
  • Cons: Cannot access the keys directly.

3.4 Using Object.entries()

Object.entries() returns an array of the object’s key-value pairs, which makes it perfect for iterating over both keys and values.

const user = { name: 'John', age: 30 };

for (const [key, value] of Object.entries(user)) {
  console.log(key, value);
}
// Output: 
// name John
// age 30
Enter fullscreen mode Exit fullscreen mode
  • Pros: Easy access to both keys and values in one iteration.
  • Cons: Slightly more complex syntax.

4. Comparison of Object Iteration Techniques

Technique Access to Keys Access to Values Inherited Properties Simplicity
for...in Yes Yes Yes (if enumerable) Simple
Object.keys() + for...of Yes No No Moderate
Object.values() + for...of No Yes No Moderate
Object.entries() + for...of Yes Yes No Slightly complex

5. Comparison Between for...in and for...of

5.1 for...in Loop

The for...in loop is used to iterate over the enumerable properties (keys) of an object, including properties that may be inherited through the prototype chain.

Example: for...in with an Object

const user = { name: 'John', age: 30 };

for (const key in user) {
  console.log(key, user[key]);
}
// Output:
// name John
// age 30
Enter fullscreen mode Exit fullscreen mode
  • Explanation: The for...in loop iterates over the keys (name and age) and allows you to access the corresponding values (John and 30).

Example: for...in with an Array (Not Recommended)

const colors = ['red', 'green', 'blue'];

for (const index in colors) {
  console.log(index, colors[index]);
}
// Output:
// 0 red
// 1 green
// 2 blue
Enter fullscreen mode Exit fullscreen mode
  • Explanation: The for...in loop iterates over the indices (0, 1, 2) of the array, not the values themselves. This behavior is usually less desirable when working with arrays.

5.2 for...of Loop

The for...of loop is used to iterate over iterable objects like arrays, strings, maps, sets, and other iterables. It loops over the values of the iterable.

Example: for...of with an Array

const colors = ['red', 'green', 'blue'];

for (const color of colors) {
  console.log(color);
}
// Output:
// red
// green
// blue
Enter fullscreen mode Exit fullscreen mode
  • Explanation: The for...of loop directly iterates over the values of the array (red, green, blue), which is ideal for array iteration.

Example: for...of with a String

const name = 'John';

for (const char of name) {
  console.log(char);
}
// Output:
// J
// o
// h
// n
Enter fullscreen mode Exit fullscreen mode
  • Explanation: The for...of loop iterates over each character of the string (J, o, h, n), making it useful for string manipulation.

Summary: Key Differences Between for...in and for...of

Feature for...in for...of
Purpose Iterates over object keys (including inherited properties) Iterates over iterable values (arrays, strings, etc.)
Works with Objects Yes No (objects are not iterable)
Works with Arrays Yes, but not ideal (returns indices) Yes, ideal (returns values)
Use Case Best for iterating over object properties Best for arrays, strings, maps, sets, etc.

6. Advanced Example: Iterating Over Nested Objects

Sometimes, objects are nested, and you need to iterate through all levels of the object. Here's an example that uses recursion to handle nested objects.

const user = {
  name: 'John',
  age: 30,
  address: {
    city: 'New York',
    zip: 10001
  }
};

// Recursively iterate through the object
function iterate(obj) {
  for (const [key, value] of Object.entries(obj)) {
    if (typeof value === 'object' && !Array.isArray(value)) {
      console.log(`Entering nested object: ${key}`);
      iterate(value); // Recursively call for nested objects
    } else {
      console.log(key, value); // Output key-value pair
    }
  }
}

iterate(user);
// Output: 
// name John
// age 30
// Entering nested object: address
// city New York
// zip 10001
Enter fullscreen mode Exit fullscreen mode
  • Explanation: The function checks if the value is an object, then recursively iterates through it.

7. Best Practices for Object Iteration in JavaScript

Use the Right Technique for the Right Task

  1. Use for...in cautiously: It may iterate over properties inherited from the prototype chain,

Top comments (0)