fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← React

React / 3 MIN READ

For Loop

Basic iteration in React

From the original Fervor library. Examples may use older package versions.

Using the For…of Loop in JavaScript and React

The for…of loop is a powerful construct in JavaScript used to iterate over iterable objects like arrays, strings, maps, and sets. It provides a more concise and readable way to loop through the values of these objects, making code simpler and easier to understand. In this tutorial, we’ll explore how to use the for…of loop, along with some practical examples.

Introduction to the For…of Loop

The for…of loop is especially useful when you need to iterate over the values of iterable objects. It simplifies the process by automatically assigning each value to a variable on each iteration.

Basic Syntax of the For…of Loop

The basic syntax of the for…of loop is as follows:

for (variable of iterable) {
    // Code to execute on each iteration
}

In this syntax, variable represents the variable that will hold the current value from the iterable, and iterable is the object you want to iterate over.

Using the For…of Loop with Arrays

Let’s start with an example of how to use the for…of loop to iterate over an array of numbers:

const numbers = [1, 2, 3, 4, 5];

for (const num of numbers) {
    console.log(num);
}

In this example, the loop iterates over each value in the numbers array, and the num variable is assigned the current value on each iteration. The output will be:

1
2
3
4
5

Using the For…of Loop with Strings

The for…of loop is particularly useful for iterating over characters in a string, as strings are iterable objects in JavaScript:

const sentence = "This is a sentence.";

for (const char of sentence) {
    console.log(char);
}

In this case, the loop iterates over each character in the sentence string, and the char variable holds the current character. The output will be each character of the string on separate lines.

Using the For…of Loop with Maps

The for…of loop can also iterate over iterable objects like maps. Here’s an example of using the loop to iterate over key-value pairs in a map:

const myMap = new Map();
myMap.set("one", 1);
myMap.set("two", 2);
myMap.set("three", 3);

for (const [key, value] of myMap) {
    console.log(`${key} = ${value}`);
}

In this example, the loop iterates over the key-value pairs in the myMap map. The destructuring assignment syntax is used to assign the key and value variables to the respective values of each key-value pair.

Practical Uses of the For…of Loop

  • Array Manipulation: You can use the for…of loop to process and manipulate the elements of an array.
  • String Processing: When working with strings, the loop allows you to perform character-level operations.
  • Iterating Over Maps: The loop simplifies the process of iterating over the key-value pairs of a map.

Conclusion

The for…of loop is a versatile tool for iterating over iterable objects in JavaScript and React. It provides a cleaner and more concise syntax compared to traditional loops and can be particularly useful when working with arrays, strings, maps, and sets.

Home


By mastering the for…of loop, you can efficiently iterate over different types of iterable objects in JavaScript and React applications. This technique enhances code readability and simplifies your development process.

Keep your curiosity going.Explore more React →
287 TUTORIALS · 22 TOPICSREADY