Fireship · @fireship

JavaScript Loops - Code This, Not That

Uploaded January 16, 2019 · Length 8:36

JavaScript Loops - Code This, Not That

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.

  1. 0:00

    [Music]

  2. 0:06

    welcome to another episode of code this

  3. 0:09

    not that where we try to write the best

  4. 0:10

    code possible and avoid the bad stuff

  5. 0:12

    today we'll look at loops or iteration

  6. 0:14

    and JavaScript which is a lot more

  7. 0:16

    interesting than you might think we'll

  8. 0:17

    look at this from both a readability and

  9. 0:19

    performance perspective to figure out

  10. 0:21

    the best loop to write in your code if

  11. 0:22

    you're new here like and subscribe and I

  12. 0:24

    recommend following along with the

  13. 0:25

    source code on fire ship IO and if you

  14. 0:27

    want to see more videos like this one

  15. 0:29

    let me know in the comments when we talk

  16. 0:31

    about loops we're actually talking about

  17. 0:32

    iteration which happens when we tell a

  18. 0:34

    computer to do something over and over

  19. 0:36

    again until we tell it to stop the best

  20. 0:38

    way to understand this is by looking at

  21. 0:40

    the traditional for loop first we need

  22. 0:42

    to keep track of the number of

  23. 0:43

    iterations that we've made which usually

  24. 0:44

    starts at zero then we need a condition

  25. 0:46

    to stop the loop and then we need to

  26. 0:48

    increment the counter each time an

  27. 0:50

    iteration is completed it's kind of like

  28. 0:51

    saying start here stop here and do this

  29. 0:54

    after each completed loop this loop is

  30. 0:56

    very explicit because we can look at it

  31. 0:58

    and understand exactly what it's doing

  32. 1:00

    without making any assumptions but this

  33. 1:02

    code is kind of hard to digest and

  34. 1:04

    usually as programmers we want to write

  35. 1:06

    as little code as possible and make that

  36. 1:07

    code understandable to other human

  37. 1:09

    beings

  38. 1:10

    luckily javascript provides two other

  39. 1:12

    ways to loop over things that are much

  40. 1:13

    more sugary and easier to understand for

  41. 1:15

    the average person the first one is the

  42. 1:17

    for of loop and it's very similar to a

  43. 1:20

    for loop except we use the of keyword

  44. 1:22

    and pass and the thing that we're

  45. 1:23

    iterating over certain things in

  46. 1:25

    JavaScript are iterable for example

  47. 1:27

    arrays maps and strings so in this case

  48. 1:30

    javascript will assume that we're

  49. 1:31

    iterating to the end of this array and

  50. 1:33

    will give us access to the elements in

  51. 1:34

    the array inside of the brackets so when

  52. 1:37

    just looping over an array this is a lot

  53. 1:38

    less mental overhead than a traditional

  54. 1:40

    for loop but in order to really

  55. 1:42

    understand what's going on here let's

  56. 1:43

    look at things at a lower level the

  57. 1:45

    array itself is iterable and it

  58. 1:47

    implements an iterator function that

  59. 1:49

    allows it to be looped over we can

  60. 1:51

    actually override the default behavior

  61. 1:52

    of the for of loop by implementing our

  62. 1:55

    own iterator function so when we use the

  63. 1:57

    object in a for loop it's going to call

  64. 1:59

    this iterator function over and over