while Loop

while Loop

  • While loops run its statements as long as a specified condition evaluates to true
while (condition) {
  statement
}

// run some other code
  • If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop

Example

let n = 0

while (n < 5) {
  console.log(n)
  n++
}

JS Bin on jsbin.com

Exercise

Write a program that uses a while loop to print (using console.log()) a countdown from 10 to 1

JS Bin on jsbin.com