Code Editor
Learn
Loops

while Loops

Awesome job! for loops are great, but they have a limitation: you have to know how many times you want the loop to run. What if you want a loop to run an unknown or variable number of times instead?

For example, if we have a deck of cards and we want to flip cards (loop a card flipping function) until we get a 'Spade', how could we write that in JavaScript?

That's the purpose of the while loop. It looks like similar to a for loop. Check it out:

while (condition) { // code block that loops until condition is false }
  1. The loop begins with the keyword while

  2. Inside the parentheses, we can insert a condition. As long as the variable evaluates to true the block of code will loop.

  3. Inside the code block we can write any code we'd like to loop.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.