Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top