JavaScript ES6 for Beginners #5: Iterables, Looping and Array improvements

4 minute read


ES6 introduced a new type of loop, the for of loop.


The for of loop

Iterating over an array

Usually we would iterate using something like this:

var fruits = ['apple','banana','orange'];
for (var i = 0; i < fruits.length; i++){
// apple
// banana
// orange

Look at how we can achieve the same with a for of loop:

const fruits = ['apple','banana','orange'];
for(const fruit of fruits){
// apple
// banana
// orange


Iterating over an object

Objects are non iterable so how do we iterate over them? We have to first grab all the values of the object using something like Object.keys() or the new ES6 Object.entries().

const car = {
  maker: "BMW",
  color: "red",
  year : "2010",

for (const prop of Object.keys(car)){
  const value = car[prop];
// BMW maker
// red color
// 2010 year


The for in loop

Even though it is not a new ES6 loop, let’s look at the for in loop to understand what differentiate it compared to the `for of.

The for in loop is a bit different because it will iterate over all the enumerable properties of an object in no particular order.

It is therefore suggested not to add, modify or delete properties of the object during the iteration as there is no guarantee that they will be visited, or if they will be visited before or after being modified.

const car = {
  maker: "BMW",
  color: "red",
  year : "2010",
for (const prop in car){
  console.log(prop, car[prop]);
// maker BMW
// color red
// year 2010


Difference between for of and for in

The first difference we can see is by looking at this example:

let list = [4, 5, 6];

// returns a list of keys
for (let i in list) {
   console.log(i); // "0", "1", "2",

// for ...of returns the values 
for (let i of list) {
   console.log(i); // "4", "5", "6"

for in will return a list of keys whereas the for of will return a list of values of the numeric properties of the object being iterated.

Another differences is that we can stop a for of loop but we can’t do the same with a for in loop.

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (const digit of digits) {
  if (digit % 2 === 0) {
// 1 3 5 7 9

The last important difference I want to talk is that the for in loop will iterate over new properties added to the object.

const fruit = ["apple","banana", "orange"]; = "gnam gnam";

for (const prop of fruit){
// apple
// banana
// orange

for (const prop in fruit){

// apple
// banana
// orange
// gnam gnam


Array improvements


Array.from() is the first of the many new array methods that ES6 introduced.

It will take something arrayish, meaning something that looks like an array but it isn’t, and transform it into a real array.

// our html
<div class="fruits">
  <p> Apple </p>
  <p> Banana </p>
  <p> Orange </p>

const fruits = document.querySelectorAll(".fruits p");
const fruitArray = Array.from(fruits);

//it will return us an array containing 3 p tags [p,p,p];

//since now we are dealing with an array we can use map
const fruitNames = fruit => fruit.textContent);

// ["Apple", "Banana", "Orange"]

We can also simplify like this:

const fruits = Array.from(document.querySelectorAll(".fruits p"));
const fruitNames = => fruit.textContent);

// ["Apple", "Banana", "Orange"]

Now we transformed fruits into a real array, meaning that we can use any sort of method such as map on it.

Array.from() also takes a second argument, a map function so we can write:

const fruits = document.querySelectorAll(".fruits p");
const fruitArray = Array.from(fruits, fruit => {
  // <p> Apple </p>
  // <p> Banana </p>
  // <p> Orange </p>
  return fruit.textContent;
  // we only want to grab the content not the whole tag
// ["Apple", "Banana", "Orange"]



Array.of() will create an array with all the arguments we pass into it.

const digits = Array.of(1,2,3,4,5);

// Array [ 1, 2, 3, 4, 5];



Array.find() returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

It can be useful in instances where we have a json file, maybe coming from an API from instagram or something similar and we want to grab a specific post with a specific code that identifies it.

Let’s looks at a simple example to see how Array.find() works.

const array = [1,2,3,4,5];

let found = array.find( e => e > 3 );
// 4

As we mentioned, it will return the first element that matches our condition, that’s why we only got 4 and not 5.



Array.findIndex() will return the index of the element that matches our condition.

const greetings = ["hello","hi","byebye","goodbye","hi"];

let foundIndex = greetings.findIndex(e => e === "hi");
// 1

Again, only the index of the first element that matches our condition is returned.


Array.some() & Array.every()

I’m grouping these two together because their use is self-explanatory: .some() will search if there are some items matching the condition and stop once it finds the first one, .every() will check that all items match the given condition.

const array = [1,2,3,4,5,6,1,2,3,1];

let arraySome = array.some( e => e > 2);
// true

let arrayEvery = array.every(e => e > 2);
// false

Simply put, the first condition is true, because there are some elements greater than 2, but the second is false because not every element is greater than 2.


This was the fifth part of my ES6 for beginners course, check out the rest of them here.

You can also read this articles on medium, on my profile.

Thank you for reading.

Leave a Comment