while and do while statements in JS

Loops that run with one expression  

An alternative to loops with for statements in JS, are loops with while or do while statements. Unlike for statements which typically use up to three expressions to define a loop's logic, loops with while and do while statements only require one expression to define when a loop ends. This makes while and do while statements much simpler to declare, but at the same time it can make them less flexible since any control variable and/or step size adjustment must be made directly in a loop's body.Therefore, loops with while or do while statements are best suited when there's no need for a control variable or step size adjustment.

The syntax of the while and do while statements consists of an expression to end a loop when it evaluates to false, as well as a block statement wrapped in curly brackets {} that contains the logic to execute on every iteration the loop's expression evaluates to true. The only difference between a for loop with a while and do while statement, is the while statement evaluates its expression before it performs an iteration and the do while statement evaluates its expression after an iteration is done. This guarantees a do while for loop runs its block statement at least once and always before a determination is made to finish the loop, while a while for loop iteration only runs its block statement after it's determined if a loop's work is complete.

Loops with the while and do while statements can also use break and continue statements to exit loops or iterations prematurely, as well as label statements to alter the workflow of a loop and send it to a predetermined line of code. In addition, it's also possible to create a cleaner endless loop with a while or do while statement using the syntax while(true) { } or do { } while(true) syntax and relying on a break statement inside the loop to terminate it.

Listing 6-4 illustrates JavaScript how to create loops with the while and do while statements.

Listing 6-4. Loops with while and do while statements
let primeNumbers = [2,3,5,7,11];

while (primeNumbers.length) { 
  console.log(primeNumbers.pop());
}

let vowels = ["a","e","i","o","u"];

do { 
  console.log(vowels.shift());
} while (vowels.length > 2);

The while loop in listing 6-4 iterates over the primeNumbers array. The expression of the while loop checks primeNumbers.length, therefore it always does an iteration so long as the primeNumbers array is not empty. Inside the block statement of the while loop, the Array data type's pop() method is used to remove the last element from primeNumbers and output said element in a log statement, in this manner, the array eventually empties causing the while loop expression to be evaluated to false and finish the loop.

The do while loop in listing 6-4 iterates over the vowels array. The expression of the while loop checks for vowels.length > 2, therefore an iteration is made so long as the vowels array has more than elements. Inside the block statement of the do while loop, the Array data type's shift() method is used to remove the first element from vowels and output said element in a log statement, in this manner, the array eventually reaches a size of 2 causing the do while loop expression to be evaluated to false and finish the loop.