The JavaScript for…of statement iterates through the values of iterable objects, allowing you to loop over data structures like arrays, strings, maps, node lists, and more.
for (variable of iterable) { // code block to be executed } |
Variable: In each iteration, the value of the next property is assigned to the variable, which can be declared using const
, let
, or var
.
Iterable: An object that possesses iterable properties.
The for…of loop was introduced to JavaScript in 2015 with ES6, and Safari 7 was the first browser to support it.
The for...of
loop is not supported in Internet Explorer.
const cars = [“BMW”, “Volvo”, “Mini”]; let text = “”; for (let x of cars) { text += x; } |
let language = “JavaScript”; let text = “”; for (let x of language) { text += x; } |
The while loop and the do/while loop will be discussed in the following chapter.