JavaScript for...of Loop
Examples
Iterate (loop) over the values of an array:
let text = "";
const cars = ['BMW', 'Volvo', 'Mini'];
for (let x of cars) {
text += x + " ";
}
Try it Yourself »
Example
Iterate (loop) over the values of a string:
let text = "JavaScript";
for (let x of text) {
text += x + " ";
}
Try it Yourself »
Description
The for...of
statements combo iterates (loops) over the values of any iterable.
The code block inside the loop is executed once for each value.
See Also:
JavaScript Tutorial: The JavaScript for...of Tutorial
Syntax
for (x of
iterable) {
code block to be executed
}
Parameters
Parameter | Description |
x | Required. For every iteration the value of the next property is assigned to x. |
iterable | Required. Anything that has iterable properties. |
JavaScript Loop Statements
Statement | Description | |
break | Breaks out of a loop | |
continue | Skips a value in a loop | |
while | Loops a code block while a condition is true | |
do...while | Loops a code block once, and then while a condition is true | |
for | Loops a code block while a condition is true | |
for...of | Loops the values of any iterable | |
for...in | Loops the properties of an object |
Browser Support
for..of
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
for..of
is not supported in Internet Explorer.