JavaScript while Loop

You are Here:

JavaScript while Loop

The while loop executes a block of code as long as the test condition evaluates to true.

Note: Unlike do while loop, In while loop, the condition is evaluated before executing the statement.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>This while loop will execute 6 times.</p> <p id="point"></p> <script> var x = document.getElementById("point"); var i = 0; var txt = ""; while(i <= 5){ txt += i; i++; } x.innerText = txt; </script> </body> </html>

Syntax

while(condition){ // code block }

More Examples

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>This while loop will execute 4 times.</p> <p id="point"></p> <script> var x = document.getElementById("point"); var bike = ["KTM", "Honda", "Yamaha", "BMW"]; var i = 0; var txt = ""; while(i < bike.length){ txt += bike[i] +"<br>"; i++; } x.innerHTML = txt; </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author