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.
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.
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.
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.
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.
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.