Looping In Java Script

In this article you learn the basics of JavaScript and create your first JavaScript program.

Looping In Java Script

Looping In Java Script

In this article you learn the basics of JavaScript and create your first JavaScript program.

What is JavaScript loop?
 The JavaScript loops used to execute the same block or code a specified number of times and while a specified condition. JavaScript loops Very often write code, you want the same block of code to run over and over again in a row. The Instead of adding several almost equal lines in a script we can use loops to perform a task.

JavaScript supported Two different type looping :-
 1.The For Loop
 2.The while Loop

 For Loop:
The JavaScript for loop is used the know in advance how many times of the script should run.  JavaScript loop Use a For loop to run time same block of code a specified the number.

Example:

<html>
<body>
<script language="javascript">
var i=0
for (i=0;i<=6;i++)
{
alert("The number is " + i)
}
</script>
</body>
</html>


The while Loop:
The while loop is used when the loop to execute and continue executing while the specified condition

<html>
<body>
<script language="Javascript">
var i=0
while (i<=10)
{
document.write("serial number" + i)
document.write("<br />")
i=i+1
}
</script>
</table>
</body>
</html>


The do while loop
The JavaScript do...while loop is a variant of the while loop. The loop will be always execute a block of code once and then it will repeat the loop as long as the specified condition is true. This loop will be always executed once, even if the condition is false, because the loop code are executed before the condition is tested.

 Example:

<html>
<body>
<script language="Javascript">
var i=0
do 
{
alert("number" + i)
i=i+1
}
while (i<3)
</script>
</table>
</body>
</html>

 

The for in:
This is not same as the for loop . The javascript for...in loop is used to provide to the enumerate properties of a JavaScript object . This loop only found in JavaScript . This statement in the loop are executed for each property of an object until every property has been accessed.

Example:

<html>
<head> 
<Script language="Javascript">
var i;
var num= new Array(8,4,5,7,10);
for(i in num)
{
document.write(" " ,num[i]);
document.write("<br />")
}
</script>
</head>
</body>
</html>

Javascript looping Break and continue
There are two statements supported in javascript used in loop:- Break and continue

Break:
The JavaScript break command will break the loop and continue executing the code the follows after the loop.

Example:

<html>
<head>
<Script language="Javascript">
var i=0
for (i=0;i<=6;i++)
{
if (i==3){break}
alert("number" + i)
}
</script>
</head>
</body>
</html>

Continue:
The JavaScript continue command will be break the current loop and continue with the next value. 

Example

 

<html>
<head>
<script language="JavaScript">
var i=0;
for (i=0;i<=8;i++)
{
if (i==4){continue}
document.write("value" + i)
document.write("</br>")
}
</script>
</head>
</body>
</html>