DEV Community

florent giraud
florent giraud

Posted on

[REMINDER-4] For-of loop

For-of loop

The nice to know about this is that for-of get the conscience of forEach. And give you the possibility to break it.

for (const v of ['a', 'b', 'c']) {
  console.log(v);
}
Enter fullscreen mode Exit fullscreen mode

Look at the const here. As the for of will create a new scope (Go to REMINDER 1 if you want to know more about scope).
Effectively it will be transpiled by

for (var _i = 0, _a = ["a", "b", "c"]; _i < _a.length; _i++) {
    var v = _a[_i];
    console.log(v);
}
Enter fullscreen mode Exit fullscreen mode

With transpilation v will not be accessible even if real life here v will the last value of the array "c"

Top comments (0)