How to use while statement to create loop in Javascript

Description

The while statement is a pretest loop. Here's the syntax:


while(expression) {
   statement
}

Example

The following code has a while loop which use an integer to control the condition.

The variable i starts out equal to 0 and is incremented by two each time through the loop. As long as the variable is less than 10, the loop will continue.


var i = 0; 
while (i < 10) { /*from  w w  w.java2 s .co m*/
   i += 2; 
   console.log(i);
} 

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions