JavaScript Loops Function


 

JavaScript Loops Function

JavaScript Loops: In this tutorial you will come to know about different kind of loops functions supported by JavaScript e.g. for, while, do...while, for..in etc. Examples will illustrate each of these loop.

JavaScript Loops: In this tutorial you will come to know about different kind of loops functions supported by JavaScript e.g. for, while, do...while, for..in etc. Examples will illustrate each of these loop.

JavaScript Loops Types:

In Java programming language a loop is a language construct which allows the statements  to execute again and again. Loop can be achieved both in iterative(for, while, do-while) and recursive method(when a function or method calls itself).

JavaScript provides for, while, do-while, and foreach loop. Following examples will help you to learn loops:

JavaScript Loop Functions Example 1:

<html>
<head>
<title>Loop in Javascript </title>
<script type="text/javascript" >
for (x=1;x<=10;x++ )
{
	document.write(x+"<br/>");
}
</script>
</head>
<body>
</body>
</html>

Output:

1
2
3
4
5
6
7
8
9
10

JavaScript for Loop Example 2:

<html>
<head>
<title>My Firt JavaScript File </title>
<script type="text/javascript" >
x=1;
while(x<=10)
{
	document.write(x+"<br/>");
	x+=2;
}
</script>
</head>
<body>
</body>
</html>

Output:

1
3
5
7
9

Java While Loop Example 3:

<html>
<head>
<title>My Firt JavaScript File </title>
<script type="text/javascript" >
x=1;
do
{
	document.write(x+"<br/>");
	x++;
}while(x<=10);
</script>
</head>
<body>
</body>
</html>

Output:

1
2
3
4
5
6
7
8
9
10

Example 4:

<html>
<head>
<title>Write your title here </title>
<script type="text/javascript" >
var x;
var str=new Array();
str[0]="This";
str[1]="is";
str[2]="new";
str[3]="string";

for(x in str)
{
	document.write(str[x]+"<br/>");
}
</script>
</head>
</html>

Output:

This
is
new
string

Ads