Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Loop For Of

The For Of Loop

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.

Syntax

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.

Browser Support

The for…of loop was introduced to JavaScript in 2015 with ES6, and Safari 7 was the first browser to support it.

js

The for...of loop is not supported in Internet Explorer.

Looping over an Array

Example

const cars = [“BMW”“Volvo”“Mini”];

let text = “”;
for (let x of cars) {
  text += x;
}

Looping over a String

Example

let language = “JavaScript”;

let text = “”;
for (let x of language) {
text += x;
}

The While Loop

The while loop and the do/while loop will be discussed in the following chapter.