ES6 – for…of

Hi, ES6 introduce the for …of loop that is for iterate over the values of an iterable object.

Iterable objects is a generalization of arrays. That’s a concept that allows us to make any object useable in a for..of loop.

Strings are iterables

const myName = "Cristina"

for (let character of myName) {
  console.log("character --->", character);
}

Result:

Arrays are iterables

const myName = ['c', 1, 'r', 2, 'i', 3, 's', 4, 't', 5, 'i', 6, 'n', 7, 'a', {}, null]

for (let character of myName) {
  console.log("character --->", character);
}

Result:

By Cristina Rojas