JavaScript While Loop
☰Fullscreen
Table of Content:
JavaScript While Loop
JavaScript While Loop is used to execute a block of code for a number of times based on a condition.
In this tutorial, we will learn how to define/write a JavaScript While loop, and its usage using example programs.
Syntax
The syntax of JavaScript While Loop is
while (condition) { //statements }
where condition is the expression which is checked before executing the statements inside while loop.
Examples
In the following example, we execute a block of code that appends ‘hello world’ to a pre block HTML element ten times using a While Loop.
Output:
We can also use a while to iterate over the elements of an array, using array length in condition, as shown in the following example.
Output: