Advertisement
Google Ad Slot: content-top
JS For Of
The for...of loop is used to iterate over iterable objects, such as arrays, strings, maps, sets, and more. It directly provides the values of the iterable.
Syntax:
for (element of iterable) {
// Code to execute for each element
}
element: A variable that stores the value of the current item in the iteration.iterable: An object that can be iterated (e.g., an array, string, or other iterable).
Iterating Over an Array
let numbers = [10, 20, 30, 40];
for (let number of numbers) {
console.log(number);
}
Iterating Over a String
let str = "hello";
for (let char of str) {
console.log(char);
}