Home >>Javascript Tutorial >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:
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>
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>
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>
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>
5 10 15 20 25 30 35 40 45 50
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>
20 21 22 23
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>
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>
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!