From the course: Computer Science Principles Lab: JavaScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use while loops

Use while loops - JavaScript Tutorial

From the course: Computer Science Principles Lab: JavaScript

Start my 1-month free trial

Use while loops

- [Instructor] In JavaScript, we can create a while loop using the while statement. In a while loop, we need to create a condition that needs to remain true for the loop to execute. First, let's create a value to test within the loop. Our loop will go through each integer from zero to 99 and find all numbers that are evenly divisible by nine. Let's create our new variable. Var myValue is equal to zero. We first start with the while statement. Then, we create our condition test that will need to be true for the loop to continue. Since we're testing for all values below a hundred, we can enter that in as myValue is less than 100. Then we create our code block for the loop. Inside of this, we are going to use a new mathematical operator called the modulo operator. Modulo takes a value and divides it by another number. If there is a remainder, it returns that number. So for instance, if we tried to divide nine by nine, it divides evenly, so the result is zero. If we tried to divide 10 by…

Contents