What's the difference between iterating with for...of and for...in over an array?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Arrays #JS Basics

Brief Answer

Main difference between [for…of] and [for…in] when working with arrays:

  1. for…of — iterates array element values 📦
  2. for…in — iterates array indices (keys) as strings 🔑
const arr = ['a', 'b', 'c'];
 
// for...of — values
for (const value of arr) {
  console.log(value); // 'a', 'b', 'c'
}
 
// for...in — indices
for (const index in arr) {
  console.log(index); // '0', '1', '2' (strings!)
}

Full Answer

Imagine you have a shelf with books. [for…of] is like going through the books themselves, while [for…in] is like going through the shelf numbers! 📚🔢

for…of — iterating values

Works with the array elements themselves:

const fruits = ['apple', 'banana', 'orange'];
 
// Get values directly
for (const fruit of fruits) {
  console.log(fruit); 
}
// 'apple'
// 'banana' 
// 'orange'
 
// Works with numbers too
const numbers = [1, 2, 3];
for (const num of numbers) {
  console.log(num); // 1, 2, 3
}

for…in — iterating indices

Works with “slot names” (indices):

const fruits = ['apple', 'banana', 'orange'];
 
// Get indices (as strings!)
for (const index in fruits) {
  console.log(index);        // '0', '1', '2' (strings!)
  console.log(fruits[index]); // 'apple', 'banana', 'orange'
}
 
// Indices are strings!
console.log(typeof index); // 'string'

Practical Difference

With numbers

const numbers = [10, 20, 30];
 
// for...of — numbers
for (const num of numbers) {
  console.log(num + 1); // 11, 21, 31
}
 
// for...in — strings!
for (const index in numbers) {
  console.log(index + 1); // '01', '11', '21' (strings!)
}

With sparse arrays

const sparse = ['a', , 'c']; // Missing element
 
// for...of — iterates everything, including gaps
for (const value of sparse) {
  console.log(value); // 'a', undefined, 'c'
}
 
// for...in — only existing indices
for (const index in sparse) {
  console.log(index); // '0', '2' (no '1'!)
}

When to Use What

for…of — for values

// ✅ When you need elements themselves
const items = ['pen', 'pencil', 'eraser'];
for (const item of items) {
  console.log(`I have: ${item}`);
}

for…in — for indices

// ✅ When you need indices
const items = ['pen', 'pencil', 'eraser'];
for (const index in items) {
  console.log(`At position ${index}: ${items[index]}`);
}

Common Mistakes

Confusing what’s iterated

const arr = ['a', 'b', 'c'];
 
// ❌ Mistake — think for...in gives values
for (const item in arr) {
  console.log(item); // '0', '1', '2' — indices, not values!
}
 
// ✅ Correct — use for...of for values
for (const item of arr) {
  console.log(item); // 'a', 'b', 'c' — values
}

Thinking indices are numbers

const arr = [10, 20, 30];
 
// ❌ Mistake — indices are strings!
for (const index in arr) {
  console.log(arr[index] + 5); // Works
  console.log(index + 5);      // '05', '15', '25' — strings!
}
 
// ✅ Correct — convert to number
for (const index in arr) {
  console.log(+index + 5); // 5, 6, 7 — numbers
}

Simple Rules

  1. for…of — iterates element values 📦
  2. for…in — iterates indices (as strings) 🔑
  3. for…of — for working with content 🎯
  4. for…in — for working with positions 📍
  5. Indices are strings — don’t forget the type! ⚠️
  6. for…in with gaps — skips missing indices 🕳️

Understanding the difference between [for…of] and [for…in] helps iterate arrays correctly! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪