A for...of Loop
is a control flow statement that creates a loop iterating over iterable objects, including built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.
Syntax of a for…of Loop
The syntax of a for...of Loop
in JavaScript is as follows:
for (variable of iterable) {
// code to be executed
}
- Variable: On each iteration, a different property value is assigned to the variable.
- Iterable: This is an object whose enumerable properties are iterated.
Example of a for…of Loop
Here’s a simple example of a for...of Loop
that prints the elements of an array:
let fruits = ["apple", "banana", "mango"];
for (let fruit of fruits) {
console.log(fruit);
}
In this example, the for...of Loop
iterates over the elements of the fruits
array and print each fruit.
The for...of Loop
is a unique and powerful tool in JavaScript, allowing you to easily iterate over the elements of iterable objects. It’s a fundamental concept that’s widely used in programming, so understanding how it works will greatly enhance your JavaScript coding skills.