Press "Enter" to skip to content

Learn how to write a JavaScript for loop

In this article you will learn how write a simple JavaScript for loop function.
Following is a sample of “For loop” function:

<script type="text/javascript">
function writeforloop(){
 var i=0;
 for (i=0;i<=10;i++)
 {
 document.write("The i’s  is value is " + i);
 document.write("<br />");
 }
 }
 </script>

Put the following code inside the body tag:

<script type="text/javascript">
 writeforloop();
 </script>

Below is a sample HTML page:

<html>
 <head>
 <script type="text/javascript">
 function writeforloop(){
 var i=0;
 for (i=0;i<=10;i++)
 {
 document.write("The i’s  is value is " + i);
 document.write("<br />");
 }
 }
 </script>
</head>
 <body>
 <script type="text/javascript">
 writeforloop();
 </script>
</body>
 </html>