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 Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.

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

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 />');

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);

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 />');

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 />');

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 Articles

Javascript Forums

Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums

Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems