You are here: Home / Topics / While Loop example in JavaScript

While Loop example in JavaScript

Filed under: JavaScript on 2023-08-06 22:03:24

In this post we will see while loop in JavaScript. While loop keep working until the condition is true. As soon as the condition is false it stops.

For example we have taken one array here and we will push the values in this array with the help of while loop.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>While Loop in JavaScript</title>
</head>
<body>
   <script>
       var myArray = [];
       var i = 0;
       while(i < 5){
           myArray.push(i);
           i++;
       }

       console.log(myArray);
   </script>
</body>
</html>

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.