Member-only story

Comprehensive guide to JavaScript iteration methods

habtesoft
4 min readOct 1, 2024

--

Everything you need to know about javascript iteration techniques is explained below, go through it.

Not a Medium member? Read this article here

1. Traditional for Loop

The classic for loop is versatile and can iterate over arrays, strings, and more.

Syntax:

for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}

Example:

const fruits = ['apple', 'banana', 'cherry'];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}

Use Case: Best for cases where you need full control over the iteration, including access to the index.

2. for...of Loop

The for...of loop provides a concise way to iterate over iterable objects like arrays, strings, and sets.

Syntax:

for (const element of iterable) {
// code
}

Example:

const colors = ['red', 'green', 'blue'];
for (const color of colors) {
console.log(color);
}

Use Case: Ideal for simple iteration without needing the index.

3. for...in Loop

--

--

habtesoft
habtesoft

Written by habtesoft

Passionate JavaScript developer with a focus on backend technologies. Always eager to connect and learn. Let’s talk, https://buymeacoffee.com/habtesoftat

No responses yet