Loops
Loops are one key control flow structure of any programming language. JavaScript provides quite a few different ways to loop.
Loops are one of the fundamental concepts of any programming language.
Using a loop we can execute a piece of code multiple times to perform all kind of tasks.
From the basic loop that prints a number from 1 to 10, up to looping an array and printing its values in a JSX template, loops are invaluable and you use them all the time.
In this unit I’ll introduce how we can loop in JavaScript.
The for
loop
for
loopThe for
loop is one of the most classic loops in programming.
Here’s the syntax.
We start by defining the 3 properties of the loop with semicolon, and then we add a block that’s executed for every iteration:
By <initialization>
I mean we typically set up an index variable to 0
, like let i = 0
:
In the <condition>
block we say how many times we want the loop to iterate, for example until i
is less than the length of an array:
Finally, in the third spot, we increment the index variable:
Here’s a practical example, where we have a list
array and we loop over it to print each value:
This <initialization>; <condition>; <increment>
setup is very powerful, because we can do things like looping in the opposite direction, or skip items, or only iterate a portion of an array, but it’s also quite tricky to learn at first.
Here’s a little demo that also shows how to work with the keywords break
and continue
to do some useful stuff:
The do-while
loop
do-while
loopThe do..while
loop might be less powerful than for
but it’s also simpler to setup.
With this loop we do something, which we define in a block, and then we decide if we want to do it again.
We have to define an index variable outside of it before we can use it, so it’s more verbose than for
.
Here’s the example we saw in the for
lesson, transformed for do..while
:
Notice that we also have to increment i
manually. If you forget to do so, you will create what’s called an infinite loop.
You can interrupt a while
loop using break
:
and you can jump to the next iteration using continue
:
The while
loop
while
loopThe while
loop is similar to do..while
, but there’s a key difference: with while
we first check the condition and then (maybe) we do something.
With do..while
, if you remember, first we did something and then we checked if we wanted to do it again.
So it’s a similar loop, but for a different use case.
Here’s an example:
Same as do..while
we have to define and increment i
manually to avoid an infinite loop.
You can interrupt a while
loop using break
:
and you can jump to the next iteration using continue
:
The for-of
loop
for-of
loopThe for...of
loop is a very simple kind of loop.
With this loop you don’t have to worry about the index.
Here’s how use it with the example we’ve been using with the other loops:
It’s much simpler, right?
One downside is that we don’t have a way to get the index value, which is often useful.
In this case we use a “trick” by combining the array destructuring syntax and calling the entries()
method on the array, which we’ll talk more about later on when we’ll get to iterators:
Notice the use of const
when we define item
. This loop creates a new scope in every iteration, so we can safely use that instead of let
. This is not something we can do for the for
loop, where we have to use let
.
The for-in
loop
for-in
loopfor..in
, which should not be confused with for..of
, can be used to iterate over the enumerable properties of an object:
Since arrays are a special kind of object, we can iterate over the items in an array too:
Other kinds of loops
We have two other kinds of loops in JavaScript.
The first is Array.forEach()
, and then we have Array.map()
.
Notice I prefixed Array.
to them, because they are methods of an array.
We’ll see what this means when we’ll get to methods, and functions.
In the meantime, keep in mind those are other kinds of loops.
Last updated
Was this helpful?