title: Const

The for-of-loop

The for-of is a new loop in ES6 that replaces both for-in and forEach() and supports the new iteration protocol. Use it to loop over iterable objects (Arrays, strings, Maps, Sets, etc

Table Of Contents

Description

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.

It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

Syntax

/**
 * for-of-loop
 * 
 * {variable} -  On each iteration a value of a different property is assigned to variable. variable may be declared with const, let, or var.
 * 
 * {iterable} Object whose iterable properties are iterated.
 */

for (variable of iterable) {
  statement
}
1
2
3
4
5
6
7
8
9
10

Code

Examples

Basic for of loop

let testResults = [1.23, 1.10, 4.1];

for (let restResult of testResults) {
   console.log(restResult)
}
1
2
3
4

Result of looping through different iterable objects

// Iterating over an array

let iterable = [10, 20, 30];

for (let value of iterable) {
  value += 1;
  console.log(value);
}
// 11
// 21
// 31

// Tip :  You can use const instead of let too, if you don't reassign the variable inside the block.

let iterable = [10, 20, 30];

for (const value of iterable) {
  console.log(value);
}
// 10
// 20
// 30

// Iterating over a String

let iterable = 'boo';

for (let value of iterable) {
  console.log(value);
}
// "b"
// "o"
// "o"

// Iterating over a TypedArray

let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {
  console.log(value);
}
// 0
// 255

// Iterating over a Map

let iterable = new Map([['a', 1], ['b', 2], ['c', 3]]);

for (let entry of iterable) {
  console.log(entry);
}
// ['a', 1]
// ['b', 2]
// ['c', 3]

for (let [key, value] of iterable) {
  console.log(value);
}
// 1
// 2
// 3

// Iterating over a Set

let iterable = new Set([1, 1, 2, 2, 3, 3]);

for (let value of iterable) {
  console.log(value);
}
// 1
// 2
// 3

// Iterating over the arguments object
// You can iterate over the arguments object to examine all of the parameters passed into a JavaScript function:

(function() {
  for (let argument of arguments) {
    console.log(argument);
  }
})(1, 2, 3);

// 1
// 2
// 3

// Iterating over a DOM collection
// Iterating over DOM collections like NodeList: the following example adds a read class to paragraphs that are direct descendants of an article:

// Note: This will only work in platforms that have
// implemented NodeList.prototype[Symbol.iterator]
let articleParagraphs = document.querySelectorAll('article > p');

for (let paragraph of articleParagraphs) {
  paragraph.classList.add('read');
}

// Closing iterators
// In for...of loops, abrupt iteration termination can be caused by break, throw or return. In these cases, the iterator is closed.

function* foo(){ 
  yield 1; 
  yield 2; 
  yield 3; 
}; 

for (let o of foo()) { 
  console.log(o); 
  break; // closes iterator, execution continues outside of the loop
}
console.log('done');

// Iterating over generators
// You can also iterate over generators, i.e. functions generating an iterable object:

function* fibonacci() { // a generator function
  let [prev, curr] = [0, 1];
  while (true) {
    [prev, curr] = [curr, prev + curr];
    yield curr;
  }
}

for (let n of fibonacci()) {
  console.log(n);
  // truncate the sequence at 1000
  if (n >= 1000) {
    break;
  }
}

// Iterating over other iterable objects
// You can also iterate over an object that explicitly implements the iterable protocol:

let iterable = {
  [Symbol.iterator]() {
    return {
      i: 0,
      next() {
        if (this.i < 3) {
          return { value: this.i++, done: false };
        }
        return { value: undefined, done: true };
      }
    };
  }
};

for (let value of iterable) {
  console.log(value);
}
// 0
// 1
// 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

Playground Examples


Resources

MDM

Last Updated: 8/11/2019, 3:11:10 AM