JavaScript for/of statement

Definition and usage

The for/of statement loops through the values of an iterable object.

JavaScript supports different types of loops:

  • for - Loop the code block multiple times
  • for/in - Loop through the properties of an object
  • for/of - Loop through the values of an iterable object
  • while - Loop the code block if the specified condition is true
  • do/while - Execute the code block once, then repeat the loop if the specified condition is true

Example

Example 1

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 2

Loop through the values of a string:

var txt = 'JavaScript';
var x;
for (x of txt) {
document.write(x + "<br >");
}

Try it yourself

syntax

for (variable of iterable) {
  code block to be executed
}

Parameter value

Parameter Description
variable Required. For each iteration, the value of the next property is assigned to a variable. The variable can be declared with const, let, or var.
iterable Required. An object with iterable properties.

Technical details

JavaScript version: ECMAScript 2015

browser support

statement Chrome IE Firefox Safari Opera
for/of 38.0 12.0 51.0 8.0 25.0

Related Pages

JavaScript Tutorial:JavaScript For Loop

JavaScript Reference Manual:JavaScript for Statement