From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Learn about while-loops and do-while loops in JavaScript

Learn about while-loops and do-while loops in JavaScript - JavaScript Tutorial

From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Learn about while-loops and do-while loops in JavaScript

- The next control structures we're going to look at in JavaScript are the while and do while loops. Unlike the for loops we looked at previously, these loops are usually used when we don't know exactly how many iterations we need for our loop to complete. So let's look at the basic syntax of while and do while loops. The while loop is pretty simple. It's just while, followed by parentheses, with some condition under which the code inside the while loop should be executed. And JavaScript will continue to run the code inside the loop body over and over again until the condition inside the parentheses is false. And something to note too is that if the condition is false to begin with, the code inside the loop will never be executed. And the do while loop is pretty similar to the while loop, except the condition for repeating another iteration comes after the body of the loop. And that's the main difference between while and do while loops. The while loop checks the condition before…

Contents