πŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
πŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///
⚑ Total XP: 0|πŸ’» javascript XP: 0

JavaScript Loops: for, for...of, forEach β€” Iterating Arrays the Right Way

Master every way to loop through arrays in JavaScript: the classic for loop for full control, for...of for clean value extraction, and .forEach() for functional callbacks. Learn why for...in is dangerous on arrays.

LOADING ENGINE...

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Iteration Engine

The processing core for automating repetitive logic across data collections. Three approaches β€” each with distinct strengths.


Data storage is only half the battle. To build useful apps, you must iterate through that data to display, filter, and transform it. JavaScript offers three main approaches β€” each with distinct strengths.

1The Classic for Loop β€” Total Control

The classic for loop is the most verbose but most flexible iteration tool. It has three parts separated by semicolons: the initializer (let i = 0), the condition (i < arr.length), and the increment (i++). You control exactly where to start, when to stop, and how to advance.

2for...of β€” Modern Value Extraction

The for...of loop is the recommended way to iterate arrays in modern JavaScript. It extracts the value of each element directly β€” no counter variable, no .length check, no bracket notation. This eliminates off-by-one errors and makes your intent clear.

3for...in β€” Why You Should NEVER Use It on Arrays

for...in is designed for enumerating the properties of objects, not arrays. When used on an array, it returns the indices as strings ('0', '1', '2'), not numbers. This means key + 1 produces '01' instead of 1 β€” a subtle but dangerous bug.

4.forEach() β€” Functional Iteration with Callbacks

.forEach() is an array method that embraces the functional paradigm. Instead of managing a counter, you pass a callback function that receives the current value, index, and the full array. It executes that callback once for each element.

5Decision Framework: Which Loop to Use

for...of: default choice for 90% of iterations β€” clean, safe, works on all iterables. Classic for: when you need backwards iteration, index skipping, or early exit with break. .forEach(): when you prefer a functional style for side effects (logging, DOM updates) and don't need break. for...in: only for objects β€” never arrays.

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Lesson Glossary

[01]Iteration

The process of repeating a block of code once for every item in a collection. Loops are the primary mechanism for iteration in JavaScript.

Code Preview
for (const item of arr) { ... }

[02]for Loop

The classic three-part loop structure: initializer (let i = 0), condition (i < n), and increment (i++). Gives full control over index, direction, and early exit.

Code Preview
for (let i = 0; i < arr.length; i++)

[03]for...of

A modern loop that iterates directly over the values of any iterable object (arrays, strings, Maps, Sets). Recommended for 90% of array iteration.

Code Preview
for (const value of arr) { ... }

[04]for...in

A loop that iterates over the enumerable string keys of an object. Should NEVER be used on arrays because it returns indices as strings, causing type-coercion bugs.

Code Preview
for (const key in obj) { ... } // objects only

[05].forEach()

An array method that executes a callback function once for each element. Receives (value, index, array). Returns undefined β€” cannot use break to exit early.

Code Preview
arr.forEach((val, idx) => { ... })

[06]Callback

A function passed as an argument to another function, to be executed as part of that function's operation. .forEach(), .map(), and .filter() all accept callbacks.

Code Preview
(item, index) => { ... }

[07]break

A statement that immediately exits the current loop. Works in for, for...of, while, and switch β€” but NOT inside .forEach() callbacks.

Code Preview
if (condition) break;

[08]continue

A statement that skips the rest of the current iteration and jumps to the next one. Works in for and for...of, but in .forEach() you use return instead.

Code Preview
if (condition) continue;

[09]Iterable

Any object that implements the Symbol.iterator protocol, making it compatible with for...of. Built-in iterables: Array, String, Map, Set, NodeList.

Code Preview
for (const x of iterable) { ... }

[10]Off-by-One Error

A common bug where a loop runs one time too many or too few, usually caused by using <= instead of < with .length, or starting at 1 instead of 0.

Code Preview
i <= arr.length // ← bug: accesses undefined

Continue Learning