Home >>Javascript Tutorial >JavaScript Loop

JavaScript Loop

JavaScript Loop

In order to iterate the piece of code using the for, while, do-while, or for-in loops JavaScript is used. It is generally used in array and helps in making the code compact.

In JavaScript, there are generally four types of loops:

  1. for loop
  2. while loop
  3. do-while loop
  4. for-in loop

1.For Loop in JavaScript

The For Loop in JavaScript iterates the elements for a finite number of times and should be used when the number of iteration is known.

Here is the syntax mentioned below:

for (initialization; condition; increment/decrement)  
{  
    code that you wants to be executed  
}  

Let's take an example for For loop

 

<script>
	for (i=2; i<=6; i++)  
	{  
	document.write(i + "<br/>")  
	}  
</script>

 

Here is the output for the above mentioned example:
2
3
4
5
6

Example 2(Sum of 1 to 100)

<script>
	var sum=0;
	for (var i=1; i<=100; i++)  
	{
	sum=sum+i;	  
	} 
	document.write("Sum of 1 to 100="+sum);	
</script>
Here is the output for the above mentioned example: Sum of 1 to 100=5050

2.While Loop in JavaScript

While loop in JavaScript iterates the elements for the infinite times and it is generally used if the number of iteration is unknown.

Here is the syntax of while loop:

while (condition)  
{  
    code that you wants to be executed  
}  

Let's take an example for while loop

 

<script>
	var i=10;  
	while(i<=12)  
	{  
	document.write(i + "<br/>");  
	i++;  
	}  
</script>

 

The output of the above mentioned example will be as follows:
10
11
12

Example 2 (Print table of 5)

 

<script>
	var table=5;
	var i=1;
	var tab=0;	
	while(i<=10)  
	{ 
	var tab=table*i;	
	document.write(tab + " ");  
	i++;  
	}  
</script>

 

The output of the above mentioned example will be as follows:
5 10 15 20 25 30 35 40 45 50

3.Do-While Loop in JavaScript

Just like while loop, the do-while loop iterates the elements for an infinite number of times. But, the code is executed at least one time despite of the fact that condition is true or false.

Here is the syntax of do-while loop:

do
{  
code that you wants to be executed  
}
while (condition);  

Let's take an example for Do-while loop

<script>
	var i=20;  
	do
	{  
	document.write(i + "<br/>");  
	i++;  
	}
	while (i<=23); 
</script>
Here is the output for the above mentioned example:
20
21
22
23

4.For-in Loop in JavaScript

The for-in loop in JavaScript is generally used to iterate the properties of an object/Associative arrat.

Here is the syntax for for-in loop:

for (Tempvariablename in object/array) 
{
   statement or code to be executed
}

Let's take an example for For-in loop

<html>
   <body>     
      <script type = "text/javascript">
        var user = {fname:"sanjeev", lname:"kumar",country:"india"};
		var x;
		for (x in user) 
		{
		  document.write(x+": "+user[x]+"<br>");
		}
      </script>      
   </body>
</html>
Here is the output for the above mentioned example:
fname: sanjeev
lname: kumar
country: india

Example 2:

<html>
   <body>     
      <script type = "text/javascript">
            var aProperty;
            document.write("Navigator Object Properties:<br/>");        
            for (aProperty in navigator) {
               document.write(aProperty);
               document.write("<br/>");
            }
            document.write ("Exiting from the loop!");
      </script>      
      <p>Set the variable to different object and then try...<p>
   </body>
</html>
Here is the output for the above mentioned example:
Navigator Object Properties:
vendorSub
productSub
vendor
maxTouchPoints
hardwareConcurrency
cookieEnabled
appCodeName
appName
appVersion
platform
product
userAgent
language
languages
onLine
doNotTrack
geolocation
mediaCapabilities
connection
plugins
mimeTypes
webkitTemporaryStorage
webkitPersistentStorage
getBattery
sendBeacon
getGamepads
javaEnabled
vibrate
userActivation
mediaSession
permissions
registerProtocolHandler
unregisterProtocolHandler
deviceMemory
clipboard
credentials
keyboard
locks
mediaDevices
serviceWorker
storage
presentation
bluetooth
usb
requestMediaKeySystemAccess
getUserMedia
webkitGetUserMedia
requestMIDIAccess
Exiting from the loop!