while-loops

Learn how while-loops differ from for-loops. Learn when to use each loop and the advantages of each.

While-loops

While-loops are another tool in our toolbelt. They’re used less frequently than for-loops.

Let’s see how to do the same thing we did in the previous lesson with a while-loop. We want to log the numbers 0 - 9 to the console.

Structure

While-loops have a different structure.

while(/*condition*/) {
    // body
}

They take in only one piece of information, the condition. We have to do the other parts ourselves. To make this while loop equivalent to our for-loop from the previous lesson:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy