SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Javascript Tutorials and Javascript Frameworks jQuery resources, tools, articles, code samples and downloads for Web Programmers


Javascript For Loop Sample Code

Javascript developers can built two different javascript loops in their codes.
Javascript For Loop and Javascript While Loop.

Javascript Loop statements are used in order to execute a code block several times where the condition of the execution is determined dynamically by for loop arguments.
Using For Loop minimizes js code that is required by calling a javascript code block several times with changing variables just like calling a javascript function with variables.

Here is the Javascript For Loop syntax for js developers where var is the name of the control variable within for loop.
Start value and end value define the range of numbers for control variable var.
Increment is the incremental amount which is added or substracted from the control variable on each loop.

for ( var = start_value; var <= end_value; var = var + increment)
{
 // javascript code inside for loop
}
Code

Javascript For Loop Sample Code

Here are some sample javascripts using for loop.
Let's write numbers beginning from 1 to 10 using javascript for loop.

<script language="javascript" type="text/javascript">
  for (i = 1; i <= 10; i += 1) {
    document.write(i + '<br />');
  }
</script>
Code

The output of this sample Javascript For Loop is as follows.
The code will display series 1-10 in javascript using for loop.
Note that we can put HTML tags within Document.Write command as text argument as here "<br />" tag in our sample.


Another Javascript For Loop Example

Let's continue to mathematical samples with writing the powers of a given variable.

<script language="javascript" type="text/javascript">
function ShowPowersOf(i, j) {

  for (p = 0; p <= j; p += 1) {
    document.write('The ' + p + ' power of ' + i + ' is ' + Math.pow(i, p) + '<br />');
  }

}
ShowPowersOf(2, 8);
</script>
Code

And the output of this example javascript for loop statement is as follows :


Javascript Array using For Loop

Here there are a few code example of For Loop using Array using Javascript.
The below JavaScript For Loop example script code create javascript array variable, populates array items and then reads them back.

<script language="javascript" type="text/javascript">
  var jsArray = new Array();
  for (j = 0; j < 10; j = j + 1) {
    jsArray[j] = j;
  }
  for (j = 0; j < 10; j = j + 2) {
    document.write(jsArray[j] + '<br />');
  }
</script>
Code

Here is the output of the above sample javascript array code where for loop is used.
By the way while using Arrays, developers frequently use Javascript For Loop. It is also possible to create multi-dimensional array in Javascript using For Loop.


List All Links in A Web Page using Javascript For Loop and getElementsByTagName

In this javascript example script, we will list all links on a web page using JavaScript For Loop structure and using document.getElementsByTagName method.

<script language="javascript" type="text/javascript">
  var hyperlinks = document.getElementsByTagName('A');
  for (j = 0; j < hyperlinks.length; j = j + 1) {
    document.write(hyperlinks[j] + '<br />');
  }
</script>
Code

And using the above js code, the list of hyperlinks in this page is listed as follows.

I hope these for loop sample codes are useful for web developers as a javascript tutorial.



Javascript


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.