JavaScript for/of Statement
❮ JavaScript Statements Reference
Example
Loop through the values of an array:
var cars = ['BMW', 'Volvo', 'Mini'];
var x;
for (x of cars) {
document.write(x + "<br >");
}
Try it Yourself »
Example
Loop through the values of a string:
var txt = 'JavaScript';
var x;
for (x of txt) {
document.write(x
+ "<br >");
}
Try it Yourself »
Definition and Usage
The for/of statement loops through the values of an iterable object.
JavaScript supports different kinds of loops:
- for - loops through a block of code a number of times
- for/in - loops through the properties of an object
- for/of - loops through the values of an iterable object
- while - loops through a block of code while a specified condition is true
- do/while - loops through a block of code once, and then repeats the loop while a specified condition is true
Browser Support
| Statement | |||||
|---|---|---|---|---|---|
| for/of | 38.0 | 12.0 | 51.0 | 8.0 | 25.0 |
Syntax
for (variable of
iterable) {
code block to be executed
}
Parameter Values
| Parameter | Description |
|---|---|
| variable | Required. For every iteration the value of the next property is assigned to the variable. Variable can be declared with const, let, or var. |
| iterable | Required. An object that has iterable properties |
Technical Details
| JavaScript Version: | ECMAScript 2015 |
|---|
Related Pages
JavaScript Tutorial: JavaScript For Loop
JavaScript Reference: JavaScript for Statement
❮ JavaScript Statements Reference

