Learn JavaScript

JavaScript while Loop

JavaScript while Loop is very useful in JavaScript programming. Whenever you want to execute a particular set of code snippets multiple times, with different set of input values, in that case you can take the help of while loop. It is used for the compact form of looping.

Standard Syntax of a while Loop

while (test condition)
{
Statement 1;
Statement 2;
Statement 3;
.
.
.
Statement n;
// set of statements execute, if the test condition is true
}

Explanation

While Loop Test Condition: The first statement that is executed under JavaScript while loop is test condition. Initially the test condition is checked and, if the truth value of the test condition is found to be true then control passes inside the while block and the set of statements within the block will be executed. If the truth value of the test condition is false, then the control exit from the while loop for further execution.

JavaScript while Loop Compact Form

In order to understand the compact form of while looping, consider the following two code snippets. Where you can easily observe that, the first code snippet is more compact than the second one.


Example 1

<html>
<body>
<h2>Program to understand the working of while loop</h2>
<h3>Winner of FIFA 2022 is Chosen from the Following Teams</h3>
<script>
var team = ["Brazil", "Netherland", "Spain", "Argentina", "France"];
var winner = "";
var i = 0;
while (i < 5)
{
winner += team[i] + "<br>";
i++;
}
document.write(winner);
</script>
</body>
</html>

Output

Program to understand the working of while loop

Winner of FIFA 2022 is Chosen from the Following Teams

Brazil
Netherland
Spain
Argentina
France


Example 2

<html>
<body>
<h2>Program to understand the compactness of while loop</h2>
<h3>Winner of FIFA 2022 is Chosen from the Following Teams</h3>
<script>
var team = ["Brazil", "Netherland", "Spain", "Argentina", "France"];
var winner = "";
var i;
winner += team[0] + "<br>";
winner += team[1] + "<br>";
winner += team[2] + "<br>";
winner += team[3] + "<br>";
winner += team[4] + "<br>";
document.write(winner);
</script>
</body>
</html>

Output

Program to understand the compactness of while loop

Winner of FIFA 2022 is Chosen from the Following Teams

Brazil
Netherland
Spain
Argentina
France


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.