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 - JavaScript Tutorial
From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext
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
-
-
-
-
-
(Locked)
Learning objectives1m 16s
-
Learn about equality in JavaScript5m 31s
-
(Locked)
Learn about if-statements in JavaScript4m 8s
-
(Locked)
Learn about for-loops in JavaScript4m 53s
-
(Locked)
Learn about while-loops and do-while loops in JavaScript1m 29s
-
(Locked)
Handle and throw errors in JavaScript2m 34s
-
(Locked)
Learn about switch-case blocks in JavaScript1m 53s
-
(Locked)
Learn about ternary operators in JavaScript1m 34s
-
(Locked)
Learn the difference between block-scoped and function-scoped variables4m 29s
-
(Locked)
-
-
-
-
-
-
-
-
-