JavaScript For Of

For Of Loop

JavaScript for of The statement iterates over the values of an iterable object.

It allows you to iterate over iterable data structures such as arrays, strings, maps, node lists, and so on:

Syntax

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

variable - 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 - An object with iterable properties.

浏览器支持

For/of Browser Support

Added to JavaScript (ES6) in 2015

Safari 7 is the first browser to support for of: IE Firefox Safari Opera
Chrome 38 Edge 12 Firefox 51 Safari 7 Opera 25
October 2014 July 2015 October 2016 October 2013 October 2014

Internet Explorer does not support For/of

Traverse Array

Example

const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
  text += x;
}

Try It Yourself

Traverse String

Example

let language = "JavaScript";
let text = "";
for (let x of language) {
text += x;
}

Try It Yourself

While Loop

We will explain in the next chapter while Loop and do/while Loop.