Today I Learned - Rocky Kev

TIL the history of the for, forEach, and for...of loops

POSTED ON:

TAGS:

First, we had the for loop.

The problem with for loops are:

IT's explained here 4 Reasons Not to Use Programming Loops (and a Few Ways to Avoid Them)

Then, we had the forEach loop.
This new version was introduced in 2009 with Ecmascript 5.

It solved a bunch of problems. It's a fine solution.

Finally, we have the for ... of loop.
As new objects like set, map were created, they needed a solution.

forEach only works on arrays.

for ... of works on:

It supports all kinds of control flow in the loop body, like continue, break, return, yield and await.

Resources:
https://stackoverflow.com/questions/50844095/should-one-use-for-of-or-foreach-when-iterating-through-an-array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of


Related TILs

Tagged:

TIL mutations and how to sort without mutating

Mutation isn't bad. It CAN cause side effects and ruin your life. There's times when you want to have your objects mutate. For everything else, switch to functional programming and passing the data.

TIL a clean way to add classes based on the page in Vue

If your pages exist in a array, use the includes() to check.

TIL Finds the differences between arrays

Finds the differences between arrays