DEV Community

Cover image for What are "for loops" in JavaScript?
Jahkamso
Jahkamso

Posted on

What are "for loops" in JavaScript?

Imagine you're in a hall (a ball room), all the chairs are disorganised, and your task is to arrange them. Where would you start? Obviously, you'll either go row by row or column by column, but it's going to be sequential/orderly.

The same is true when it comes to "for loops". It's basically a way to go through a list of data and present them one after the other till you get to the end.

An example of "for loops":

const fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];

for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}
Enter fullscreen mode Exit fullscreen mode

In the example above, there's an array of data containing fruit names. The "for loop" goes through all of them and prints them out in the console, one after the other.

This explanation is short, but I hope you understand "for loops" better.

Top comments (4)

Collapse
 
nifemi_olumide_014ceace29 profile image
Nifemi Olumide

I love the concept you used to explain it, well done boss

Collapse
 
jahkamso profile image
Jahkamso

thank you so much! glad you love it😊

Collapse
 
collins_kayode_661f9bf283 profile image
Collins Kayode

Well said πŸ‘

Collapse
 
jahkamso profile image
Jahkamso

I'm glad you find it usefulπŸ™πŸ»