
)Ĭonsole.log('iterate values and indexes from a generator')įor (const of eachWithIndex(eachFromTo(10, 13))) console.log(val, i)Ĭonst anArray = eachToArray(eachFromTo(10, 13))Ĭonsole.log('iterate values and indexes from an array')įor (const of eachWithIndex(anArray)) console.log(val, i) yields every value and index of an iterable (array, generator. convers an iterable to an array (potential infinite loop)įor (const val of iterable) result.push(val) Or ES6’s, which now has support across current browser versions: for (const of myArray.entries()) For them, there’s ES5’s forEach method that passes both the value and the index to the function you give it: var myArray = You shouldn’t use it to iterate over arrays.


For…in iterates over property names, not values, and does so in an unspecified order (yes, even after ES6).
