5 steps to creating a while loop

Zach Cusimano
loriscode
Published in
3 min readNov 26, 2016

What is a while loop in javascript? How does it work?

A while loop executes a block of code as long as the expression is true. Seriously, it will run over & over & OVER as long as it’s true.

First, some vocabulary you should already be familiar with..

Code Block — code placed in-between the curly brackets {}. All code that is run each time the loop is run.

Code Block

Conditional statements — runs a block of code if a particular statement is true.

Conditional Statement

Function — set of instructions that tells the computer to do something, usually repetitive. Like memory blocks used to save a mini program.

function: returns a random number from 1 to a number that’s given

Building a simple while loop…

1.Basic while loop structure

while loop bones

2.Create a variable to keep track of the times the loop runs

var counter = 0;

3.Each time it runs we’ll add +1 to the counter

counter += 1;

4.Create condition, it doesn’t have to be a number but at some point it needs to evaluate to false, so it can end the madness and see it’s family for once.

( counter < 50) //If counter is <50, the loop will repeat.

5.Print the outcome to the page.

console.log(counter)

In this example the counter will continue to +1 to our variable until the condition evaluates to false.

Hopefully this helps make a little more sense out of while loops for you. Thanks for stoping by and come back… after a while, crocodile (sorry just wanted to work that in.).

//Newbie so if you have additional insights, comment and I’ll update.

--

--