ES6++: 3- For Of

hassanzohdy

Hasan Zohdy

Posted on November 17, 2022

ES6++: 3- For Of

Introduction

In this article we will talk about the for of loop, we will talk about how to use it and what are the differences between it and the for in loop.

for of loop

The for of loop is used to loop over arrays, this can be used instead of the standard for loop, let's see an example

const names = ["Hasan", "Ahmed", "Ali"];

// standard for loop

for (let i = 0; i < names.length; i++) {
  const name = names[i];
  console.log(name);
}

// for of loop
for (const name of names) {
  console.log(name);
}
Enter fullscreen mode Exit fullscreen mode

See? much better and easier to read.

Loop over iterables

The for of loop can be used to loop over any iterable, let's see an example

// loop over string
const name = "Hasan";

for (const char of name) {
  console.log(char); // H a s a n
}
Enter fullscreen mode Exit fullscreen mode

Also it can loop over a custom iterable, let's see an example

const iterable = {
  [Symbol.iterator]() {
    let i = 0;
    return {
      next() {
        if (i < 3) {
          return { value: i++, done: false };
        }
        return { value: undefined, done: true };
      },
    };
  },
};

for (const num of iterable) {
  console.log(num); // 0 1 2
}
Enter fullscreen mode Exit fullscreen mode

We're not going to talk about iterables in this section, but the important thing here when you read in a package documentation that they have an iterable, you can use the for of loop to loop over it.

for in loop

You might think why i'm mentioning the for in loop here, well because not too many people knows about it or they forget about it, so i'm going to mention it here.

The for in loop is used to loop over objects, so we have for-of for arrays and for-in for objects, let's see an example


const person = {
  name: "Hasan",
  age: 20,
  country: "Egypt"
}

for (const key in person) {
  console.log(key); // name age country
}
Enter fullscreen mode Exit fullscreen mode

As you can see we can loop over the keys of the object, but we can't loop over the values, so we have to do something like this

for (const key in person) {
  console.log(person[key]); // Hasan 20 Egypt
}
Enter fullscreen mode Exit fullscreen mode

🎨 Conclusion

In this article we talked about the for of loop, we talked about how to use it and what are the differences between it and the for in loop.

I hope you enjoyed this article, if you have any questions or suggestions please leave a comment below.

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

📚 Bonus Content 📚

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

💖 💪 🙅 🚩
hassanzohdy
Hasan Zohdy

Posted on November 17, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

16-ES6++: Reflection In JavaScript
javascript 16-ES6++: Reflection In JavaScript

December 6, 2022

15-ES6++: Proxy In JavaScript
javascript 15-ES6++: Proxy In JavaScript

December 4, 2022

14-ES6++: Null Coalescing in Javascript
javascript 14-ES6++: Null Coalescing in Javascript

November 26, 2022

13-ES6++: Optional Chaining in Javascript
javascript 13-ES6++: Optional Chaining in Javascript

November 26, 2022