javaScript for loops

javascript for loops are a way of repeating the same block of code over and over.

the javascript for loop can executes a statement or series of statements until a specified condition is false.


         for ( initialization  ;  condition  ;  increment)

                             the for loop code block

initialization expression.this expression is executed only once, before the loop is executed.


optional.a boolean expression.if condition is return true, loop body is executed .if condition is false, the loop is terminated.

increment/decrement expression.the increment expression is executed at the end of every pass through the loop.


    var   a = [10,20,30,40,50,60,70,80,90,100] ;
       for ( var i=0 ; i < a.length ; i++){
            document.write("<br/>"+"the array index value a["+i+"]"+"     "+ a[i] );
  • first, the initial value of the variable i is evaluated.
  • then, as long as the value of i is less than to array length, the document.write statements are executed and i is reevaluated.
  • when i is greater than array length, the condition becomes false and control is transferred outside the loop.

the initialization,  condition,  increment statements are we can write for loop in number of ways.

first form

    var   a = [100,200,300,400,500,600,700,800,900,1000] ;
       var  j = 0 ;     // initialization statement           
       for (   ; j < a.length ; j++){
  document.write("<br/>"+"the array-----index-----value----a["+j+"]"+"  "+ a[j] );

second form

    var   aa = [1,2,3,4,5,6,7,8,9,10] ;
       var  a = 0 ;     //we can put initialization statement output for loop parentheses          
       for (   ; a < aa.length ;  ){
            document.write("<p>"+"the array________index_____ value aa["+a+"]"+"     "+ aa[a] );
            a++;  //you can put increment statement inside loop body                      

javaScript nested for loop

Nesting of loops, that is, one for statement within another for statement.


       for( initialization  ;  condition  ;  increment ){  outer for loop block        
                              for( initialization  ;  condition  ;  increment ){
                                                           inner for loop block
                                                        } }
   <head>  <script>
 for(var i=1; i<10; i++){  document.write("<br/>outer for loop  i value--"+i);
    for(var j=1; j<10; j++){  if(i==j){
                    document.write("<br/>inner for loop  i  == j value"+j); }

javaScript infinite For loop

an infinite loop in javascript occurs if a condition always evaluates to true.

in the below examples some pseudocode for an infinite loop for a web server.

third form

    var   i = [1000,2000,3000,4000,5000,6000,7000,8000,9000,10000] ;
       var  j = 0 ;     //we can put initialization statement output for loop parentheses          
       for (   ;       ; //omit loop condition  ){
            document.write("<br/>"+"java script infinite loop  i["+j+"]"+"     "+ i[j] );
            j++;  //you can put increment statement inside loop body                      

note:if you omit the loop condition, condition always true, in this situation loop never its called infinite loop and this will crash your browser.

to solve this situation, you must provide a break statement inside the loop body.

         for ( ; ; ) {    document.write("this is infinite loop") ; break; }