Fireship · @fireship
JavaScript Loops - Code This, Not That
Uploaded January 16, 2019 · Length 8:36
Summary
JavaScript Loops - Code This, Not That: A tutorial on loops in JavaScript, covering traditional for loops, for of loops, and for each loops, with a focus on readability and performance.
- javascript loops
- for loop
- for of loop
- for each loop
- javascript performance
- javascript iteration
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
welcome to another episode of code this
- 0:09
not that where we try to write the best
- 0:10
code possible and avoid the bad stuff
- 0:12
today we'll look at loops or iteration
- 0:14
and JavaScript which is a lot more
- 0:16
interesting than you might think we'll
- 0:17
look at this from both a readability and
- 0:19
performance perspective to figure out
- 0:21
the best loop to write in your code if
- 0:22
you're new here like and subscribe and I
- 0:24
recommend following along with the
- 0:25
source code on fire ship IO and if you
- 0:27
want to see more videos like this one
- 0:29
let me know in the comments when we talk
- 0:31
about loops we're actually talking about
- 0:32
iteration which happens when we tell a
- 0:34
computer to do something over and over
- 0:36
again until we tell it to stop the best
- 0:38
way to understand this is by looking at
- 0:40
the traditional for loop first we need
- 0:42
to keep track of the number of
- 0:43
iterations that we've made which usually
- 0:44
starts at zero then we need a condition
- 0:46
to stop the loop and then we need to
- 0:48
increment the counter each time an
- 0:50
iteration is completed it's kind of like
- 0:51
saying start here stop here and do this
- 0:54
after each completed loop this loop is
- 0:56
very explicit because we can look at it
- 0:58
and understand exactly what it's doing
- 1:00
without making any assumptions but this
- 1:02
code is kind of hard to digest and
- 1:04
usually as programmers we want to write
- 1:06
as little code as possible and make that
- 1:07
code understandable to other human
- 1:09
beings
- 1:10
luckily javascript provides two other
- 1:12
ways to loop over things that are much
- 1:13
more sugary and easier to understand for
- 1:15
the average person the first one is the
- 1:17
for of loop and it's very similar to a
- 1:20
for loop except we use the of keyword
- 1:22
and pass and the thing that we're
- 1:23
iterating over certain things in
- 1:25
JavaScript are iterable for example
- 1:27
arrays maps and strings so in this case
- 1:30
javascript will assume that we're
- 1:31
iterating to the end of this array and
- 1:33
will give us access to the elements in
- 1:34
the array inside of the brackets so when
- 1:37
just looping over an array this is a lot
- 1:38
less mental overhead than a traditional
- 1:40
for loop but in order to really
- 1:42
understand what's going on here let's
- 1:43
look at things at a lower level the
- 1:45
array itself is iterable and it
- 1:47
implements an iterator function that
- 1:49
allows it to be looped over we can
- 1:51
actually override the default behavior
- 1:52
of the for of loop by implementing our
- 1:55
own iterator function so when we use the
- 1:57
object in a for loop it's going to call
- 1:59
this iterator function over and over


