for...of Statement (JavaScript)

Executes one or more statements for each value of an iterator obtained from an iterable object.

Syntax

for (variable of object) {  
    statements   
}  

Parameters

variable
Required. A variable that can be any property value of object.

object
Required. An iterable object such as an Array, Map, Set, or an object that implements the iterator interfaces.

statements
Optional. One or more statements to be executed for each value of an object. Can be a compound statement.

Remarks

At the beginning of each iteration of a loop, the value of variable is the next property value of object.

Example

The following example illustrates the use of the for...of statement on an array.

let arr = [ "fred", "tom", "bob" ];  

for (let i of arr) {  
    console.log(i);  
}  

// Output:  
// fred  
// tom  
// bob  

Example

The following example illustrates the use of the for...of statement on a Map object.

var m = new Map();  
m.set(1, "black");  
m.set(2, "red");  

for (var n of m) {  
  console.log(n);  
}  

// Output:  
// 1,black  
// 2,red  

Requirements

Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.

See Also

for...in Statement
for Statement
while Statement