Before we proceed, make sure you've watched the for, for…in, while, do…while loop videos in the channel and subscribe that means a lot. Let's dive into the for...of loop with an example:
In this example, we have an array of
carBrands. The for...of loop allows us to iterate over each element in the array and store it in the
brand variable. We can then access and process each element inside the loop. For example, we print each car brand to the console, which displays all the car brands stored in the array.
Let's see another example to further understand the for...of loop:
In this new example, we have an array of
fruits. the for...of loop allows us to loop through each fruit and access it using the
fruit variable. We can perform any desired operations on each element inside the loop such as printing the fruit element.