From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Learn about for-loops in JavaScript

Learn about for-loops in JavaScript

- Let's move on to the next control flow structure in JavaScript, which is for loops. Now, as you probably already know, loops allow us to execute a block of code multiple times. Sometimes this number of times is known beforehand and sometimes it isn't. In JavaScript for loops are primarily used only for the first case. When we know ahead of time how many times we want our code to execute. For example, if we have an array of items and we want to loop through all those items one by one and do something to them. Now JavaScript actually provides us with a few different ways of doing for loops. The first way, which is by far my least favorite way because it's the messiest and, well, least readable, is very similar to what you might see in C++ or Java. And it looks something like this. We have for and then parentheses that contain three statements separated by semicolons. The first statement is where we define any new variables we want to use, such as an index variable. The second…

Contents