Javascript Each Loop: Myth or Reality?
Natale Anfuso

Great introduction to the typical for-loop, but JavaScript actually has a number of looping mechanisms.

A better for-loop for “iterables” like arrays is the for…of statement. To implement your Ruby loop, you would do something similar to this:

const numbers = [1, 2, 3, 4, 5]
for (let num of numbers) {

The array also has a few looping methods that you could use. For example the array’s humble #forEach method is almost identically to Ruby’s each:

const numbers = [1, 2, 3, 4, 5]
numbers.forEach(n => console.log(n))

One of the challenges when learning JavaScript is that there’s never one-true-way to do a thing. You’ll sometimes want to do things imperatively (like the original for loop), more declaratively (like the #forEach method), or something in between (like the for…of loop).

Happy JavaScripting!