Javascript while loop examples

In this tutorial, I will give you many Examples of while loop in javascript. You only need to learn about the if-else statement and while loop in javascript.

Example 1: Javascript Program to Print the number from 1 to 10 using a while loop

var number = 10;
var i = 1;
while(i<=10)
  {
    console.log(i);
    i++
  }

The output of the program is

1
2
3
4
5
6
7
8
9
10

Example 2: Javascript program to print even numbers using a while loop

var number = 10;
var i = 1;
console.log("The Even numbers from 1 to 10 are: ");
while(i<=10)
  {
    if(i%2==0)
      {
        console.log(i);
      }
    i++
  }

The output of the program is

The Even numbers from 1 to 10 are: 
2
4
6
8
10

Example 3: Javascript program to print the sum of numbers from 1 to 10

var number = 10;
var i = 1;
var sum = 0;
while(i<=number)
  {
    sum = sum + i;
    i++
  }
console.log("The sum of numbers from 1 to 10 is: "+sum);

The output of the program is

The sum of numbers from 1 to 10 is: 55

Example 4: Javascript Program to display the multiplication table for a given integer using while loop.

var number = 3;
var i = 1;
var mul = 1;
console.log("The multiplication table of "+number+" is: ");
while(i<=10)
  {
    mul = number*i;
    console.log(number+"*"+i+"="+mul);
    i++
  }

The output of the program is

The multiplication table of 3 is: 
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30

Example 5: Javascript program to print the factorial of a number using while loop.

var number = 4;
var i = 1;
var fact = 1;
while(i<=number)
  {
    fact = fact*i;
    i++
  }
console.log("The factorial of"+number+" is "+fact);

The output of the program is

The factorial of4 is 24

Example 6: Javascript program to find the sum of all even numbers from 1 to n using a while loop

var number = 10;
var i = 1;
var sum = 0;
while(i<=number)
  {
    if(i%2==0)
      {
        sum = sum + i;
      }
    i++
  }
console.log("The sum of all even numbers from 1 to 10 is "+sum);

The output of the program is

The sum of all even numbers from 1 to 10 is 30

Example 7: Javascript program to find the perfect number using a while loop.

var number = 6;
var i = 1;
var sum = 0;
while(i<number)
  {
    if(number%i==0)
      {
        sum = sum + i;
      }
    i++
    
  }

if(sum==number)
  {
    console.log(number+" is perfect number");
  }
else{
  console.log(number+" is not a perfect number");
}

The output of the program is

6 is perfect number

Example 8: Javascript program to print the character from ‘a’ to ‘z’ and ‘A’ to ‘Z’ using while loop.

var number = 6;
var i = 65;
var sum = 0;

console.log("Chatacter from A to Z are:")
while(i<=90)
  {
    console.log(String.fromCharCode(i));
    i++;
  }

i = 97;
console.log("Chatacter from a to z are:")
while(i<=122)
  {
    console.log(String.fromCharCode(i));
    i++;
  }

The output of the program is

Chatacter from A to Z are:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Chatacter from a to z are:
a b c d e f g h i j k l m n o p q r s t u v w x y z

Example 9: Javascript program to print pyramid of a *

var str = "";
var i = 1;

while(i<=5)
  {
    var j = 1;
    while(j<=i)
      {
        str= str + "*";
        ++j;
      }
    str += "\n";
    ++i;
  }

console.log(str);

The output of the program is

*
**
***
****
*****

Example 10: Javascript program to print a pyramid of numbers using while loop.

var str = "";
var i = 1;

while(i<=5)
  {
    var j = 1;
    while(j<=i)
      {
        str= str + j;
        ++j;
      }
    str += "\n";
    ++i;
  }

console.log(str);

The output of the program is

1
12
123
1234
12345
Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME !!
Johan William

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

RECENT POSTS

TEST