JavaScript while loop starts by evaluating the condition. If the condition expression is true the statement is executed. If the condition is false, the statement(s) is not executed, and while loop ends.
while (condition)
{
statement(s);
}
statement(s): A statement that is executed as long as the condition evaluates to true.
Example while loop start in JavaScript
Simple HTML example code.
<!DOCTYPE html>
<html>
<head>
<script>
var i = 1;
while (i < 5)
{
console.log(i);
i++; // i=i+1 same thing
}
</script>
</head>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS loop topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version