I noticed for...of loops were added to the ECMAScript-6 proposal, but have never heard of them until now. What's the typical use case for them?
Asked
Active
Viewed 111 times
1 Answers
2
The use case is when you want to do something with each element of an array. for...in
in ECMAScript will sort of let you go over the array indices (and even that incorrectly).
The Python equivalent of ES6 for...of
is for...in
, like so:
myList = [ "a", "b", 5]
for el in myList:
# el takes on the values "a", "b", 5 in that order
pass

Boris Zbarsky
- 34,758
- 5
- 52
- 55
-
1Some languages call this `foreach`... in fact ES5 calls it `forEach` when used as a method on an array, but the `for...of` syntax cuts out a lot of the extra weight and allows it to easily be used on non-arrays. – Nathan Wall Nov 26 '12 at 13:45