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:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free. Share your stuff here so that others can get benefitted.