JavaScript loops
making things happen again and again
Loops allow us to perform the same functions over and over again - in JavaScript (and in programming in general) we have two main types of loops:
for loops - do something for a certain amount of times
while loops - do something until something else happens
Why loops?
We use loops primarily because we want to iterate, or go through, something, such as a list or an array of items, e.g.:
If we wanted to print each item in myArray
then we could do this:
However, that defeats the purpose of programming! We would rather just type the names inside each console.log()
! What if we had 100 items in that list?
So, instead, we use loops! The general idea, or pseudocode:
For
loop
For
loopWe use this loop to iterate (go) through a finite array of items:
General structure
Specific example
Breaking it down:
let i = 0
initializes the counter (index) variableall arrays come with a
length
propertyJavaScript arrays have a quirk where the first item has an index of
0
thus
Miami
would bemyPlaces[0]
followed byRoatán
formyPlaces[1]
i < myPlaces.length
thus means stop the loop afteri
is one less than the # of items inmyPlaces
i++
incrementsi
by 1 after each turn of the loopconsole.log(myPlaces[i])
thus runsmyPlaces.length
times (in this case, 5 times)
We would also use the for
loop if we know the number of times that the loop will run!
As we know by now, JavaScript is quite flexible and you can experiment with what operators and numbers (including negative ones):
Here we will print the list in reverse by:
initializing the counter variable as one less than the array's length (i.e. 4)
letting the loop increment by -1
letting the loop run until the index becomes 0
It will thus print the array's items backwards:
Nassau
Cozumel
Placencia
Roatán
Miami
for ... in ...
loop
for ... in ...
loopWe use the "for-in" loop to iterate through an object's keys:
General structure
Specific example
Breaking it down:
we let
key
(the first variable) represent each property name ofmyObject
then we use
in
to referencemyObject
within the loop we access each key's value with
myObject[key]
key
could be any legal JavaScript variable name
for ... of ...
loop
for ... of ...
loopWe use the "for-of" loop to iterate through an iterable's values:
General structure
Specific example
Breaking it down:
the variable
item
could exist as aconst
or alet
or even avar
in the last example above we used a
let
because we wished to change each item before doing aconsole.log
on it
the iterable can contain either an array, string or some list-like collection
each data type will have its own way through which the loop will iterate
while
loop
while
loopWe use the while
loop if we want to stop a loop when a certain condition is no longer true:
General structure
Specific example
do ... while
loop
do ... while
loopWe use the while
loop if we wish to run a loop at least once:
General structure
Specific example
break
and continue
break
and continue
The break
and continue
statements are not loops but special operators that allow us to manoeuvre through loops:
break
lets us exit the loop entirelycontinue
lets us skip the current iteration of a loop
Last updated