In this lesson, we will begin the study of loops. Loops are used to execute a piece of learn.itnots.rultiple times in a row.
Why do we need them - imagine that you need
to square 100
array elements. If you
access each element separately by its key,
it will take 100
lines of code, and
in order to write this code, you will need
to spend quite a lot of time.
But this is not necessary - we have the ability to make JavaScript perform some operation for us the required number of times. For example, it squared all the elements of an array. This is done with loops.
Loops can repeat some code a given number of times. Each such step of the loop is called a loop iteration.
Loops often use special variables that
increment their value by one each iteration.
Such variables are called loop counters.
Counters are used to count how many times
a loop has been executed. It is customary
to use the letters i
, j
and
k
for counters.
In the following lessons, we'll start learning about loops in JavaScript.