JavaScript Basic

JavaScript Array Iteration Methods (for loop, for...in, for...of, forEach, map, filter, every, some)

AI Practice

JavaScript Array Iteration Methods

1. for loop

The most basic — supports break / continue:

const arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

2. for...in

Iterates over enumerable property keys — not recommended for arrays (may include prototype properties):

for (const index in arr) {
  console.log(index); // '0', '1', '2' (strings)
}

3. for...of

Iterates over iterable values — great for arrays, supports break:

for (const value of arr) {
  console.log(value); // 1, 2, 3
}

4. forEach

Executes a function for each element — no return value, cannot break:

arr.forEach((value, index) => {
  console.log(index, value);
});

5. map

Returns a new array with each element transformed:

const doubled = arr.map(x => x * 2); // [2, 4, 6]

6. filter

Returns a new filtered array:

const evens = arr.filter(x => x % 2 === 0); // [2]

7. every

Returns true if all elements pass the test:

arr.every(x => x > 0); // true
arr.every(x => x > 1); // false

8. some

Returns true if at least one element passes the test:

arr.some(x => x > 2); // true
arr.some(x => x > 5); // false

Comparison

Method Returns Can break Use case
for — ✅ General iteration
for...of — ✅ Value iteration
forEach undefined ❌ Side effects
map New array ❌ Transform elements
filter New array ❌ Filter elements
every boolean ❌ All pass?
some boolean ❌ Any pass?

✦ AI Mock Interview

Type your answer and get instant AI feedback

Sign in to use AI scoring

Copyright © 2026 Wood All Rights Reserved · FE Interview Hub