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 whe…


This content originally appeared on DEV Community and was authored by Jahkamso

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]);
}

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.


This content originally appeared on DEV Community and was authored by Jahkamso


Print Share Comment Cite Upload Translate Updates
APA

Jahkamso | Sciencx (2024-08-23T18:45:58+00:00) What are “for loops” in JavaScript?. Retrieved from https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/

MLA
" » What are “for loops” in JavaScript?." Jahkamso | Sciencx - Friday August 23, 2024, https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/
HARVARD
Jahkamso | Sciencx Friday August 23, 2024 » What are “for loops” in JavaScript?., viewed ,<https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/>
VANCOUVER
Jahkamso | Sciencx - » What are “for loops” in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/
CHICAGO
" » What are “for loops” in JavaScript?." Jahkamso | Sciencx - Accessed . https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/
IEEE
" » What are “for loops” in JavaScript?." Jahkamso | Sciencx [Online]. Available: https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/. [Accessed: ]
rf:citation
» What are “for loops” in JavaScript? | Jahkamso | Sciencx | https://www.scien.cx/2024/08/23/what-are-for-loops-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.