Insomnia & the Power of Loops in JavaScript

Darrin Weyers
3 min readApr 11, 2021

Not long ago, when I would wake in the middle of the night my mind wandered to existential concerns like paying my mortgage, being a good father and surviving a global pandemic. But recently my mind has a new concern… JavaScript.

I just finished week 4 of a 12 week Full Stack Coding Bootcamp. It’s called a bootcamp because the pace is intense and there are seemingly no breaks. Dinner with family? Forget about it. Weekends? What am I, soft? At lunch I do push ups and run around the block.

By week 2 we were coding JavaScript and building mini apps. JavaScript is seemingly all I think about now.

And now, in liminal moments of consciousness my mind turns over things like variables, arguments, operators, and expressions. Functions, methods and objects. Math.random, math.max, math.floor, math.ceiling, and even math.PI! And loops! For loops, while loops, do while loops and breaking the loop.

What are loops? And why am I thinking about them in the middle of the night?

In JavaScript loops do some heavy lifting. Loops check conditions in a block of code and if the condition returns true the block of code will run. Then the condition will be checked again and if it still returns true, the code block will run again. And again. And again. This repeats until the condition returns false.

So let’s say we wanted a block of code to count sheep to help us get back to sleep. And we know that we usually fall back to sleep after counting about 100 sheep. We could use a while loop for that.

Here’s what our loop would look like:

Let’s break this down. First we declare some variables using “let”:

👉 let text = “”;

This means that text is an empty string. Think of it as an empty box that we are going to insert actual text into later.

👉 let sheep = 1;

This is where we start counting sheep. We start from 1!

👉 while (sheep < 100) {
text += sheep + “ 🐑 “;
sheep++;
}

And here is the fun part. Our while loop says that while the value of sheep is less than 100

👉 while (sheep < 100)

keep counting sheep in our head,

👉 text += sheep + “ 🐑 “;

and then repeat the step by adding 1 to the value of sheep

👉 sheep++;

and sheep is now at 2 . And the next time we loop over

👉 sheep++;

sheep will be at 3. And so on.

We keep this up, looping through the code block over and over, repeating the same steps until the following condition is no longer true:

👉 while (sheep < 100)

So when we hit 99 sheep… glorious sleep…

1 🐑 2 🐑 3 🐑 4 🐑 5 🐑 6 🐑 7 🐑 8 🐑 9 🐑 10 🐑 11 🐑 12 🐑 13 🐑 14 🐑 15 🐑 16 🐑 17 🐑 18 🐑 19 🐑 20 🐑 21 🐑 22 🐑 23 🐑 24 🐑 25 🐑 26 🐑 27 🐑 28 🐑 29 🐑 30 🐑 31 🐑 32 🐑 33 🐑 34 🐑 35 🐑 36 🐑 37 🐑 38 🐑 39 🐑 40 🐑 41 🐑 42 🐑 43 🐑 44 🐑 45 🐑 46 🐑 47 🐑 48 🐑 49 🐑 50 🐑 51 🐑 52 🐑 53 🐑 54 🐑 55 🐑 56 🐑 57 🐑 58 🐑 59 🐑 60 🐑 61 🐑 62 🐑 63 🐑 64 🐑 65 🐑 66 🐑 67 🐑 68 🐑 69 🐑 70 🐑 71 🐑 72 🐑 73 🐑 74 🐑 75 🐑 76 🐑 77 🐑 78 🐑 79 🐑 80 🐑 81 🐑 82 🐑 83 🐑 84 🐑 85 🐑 86 🐑 87 🐑 88 🐑 89 🐑 90 🐑 91 🐑 92 🐑 93 🐑 94 🐑 95 🐑 96 🐑 97 🐑 98 🐑 99 🐑 Zzzzzzzzzzzzzzzzzzzzzz

And that’s the power of loops in JavaScript, helping us get a good night of sleep for the coming day of code drills and push ups.

Want to stay up late? Check out the code.

--

--