11

How can I build a loop in JavaScript?

MrValdez
  • 8,515
  • 10
  • 56
  • 79
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229

4 Answers4

31

For loops

for (i = startValue; i <= endValue; i++) {
    // Before the loop: i is set to startValue
    // After each iteration of the loop: i++ is executed
    // The loop continues as long as i <= endValue is true
}

For...in loops

for (i in things) {
    // If things is an array, i will usually contain the array keys *not advised*
    // If things is an object, i will contain the member names
    // Either way, access values using: things[i]
}

It is bad practice to use for...in loops to itterate over arrays. It goes against the ECMA 262 standard and can cause problems when non-standard attributes or methods are added to the Array object, e.g. by Prototype. (Thanks to Chase Seibert for pointing this out in the comments)

While loops

while (myCondition) {
    // The loop will continue until myCondition is false
}
Community
  • 1
  • 1
georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • 2
    You should not use for...in to loop over arrays. This will cause problems with Prototype. See http://www.prototypejs.org/api/array – Chase Seibert Oct 09 '08 at 02:37
  • 2
    The problem with the for-in loops can be avoided if you check with the hasOwnProperty: if(!things.hasOwnProperty(i)) { continue; } – Andreas Grech Mar 19 '09 at 18:36
3

Here is an example of a for loop:

We have an array of items nodes.

for(var i = 0; i< nodes.length; i++){
    var node = nodes[i];
    alert(node);
}
minty
  • 22,235
  • 40
  • 89
  • 106
1

Aside form the build-in loops (while() ..., do ... while(), for() ...), there is a structure of self calling function, also known as recursion to create a loop without the three build-in loop structures.

Consider the following:

// set the initial value
var loopCounter = 3;

// the body of the loop
function loop() {

    // this is only to show something, done in the loop
    document.write(loopCounter + '<br>');

    // decrease the loopCounter, to prevent running forever
    loopCounter--;

    // test loopCounter and if truthy call loop() again 
    loopCounter && loop();
}

// invoke the loop
loop();

Needless to say that this structure is often used in combination with a return value, so this is a small example how to deal with value that is not in the first time available, but at the end of the recursion:

function f(n) {
    // return values for 3 to 1
    //       n   -n  ~-n   !~-n   +!~-n   return
    // conv int neg bitnot  not  number 
    //       3   -3   2    false    0    3 * f(2)
    //       2   -2   1    false    0    2 * f(1)
    //       1   -1   0     true    1        1
    // so it takes a positive integer and do some conversion like changed sign, apply
    // bitwise not, do logical not and cast it to number. if this value is then
    // truthy, then return the value. if not, then return the product of the given
    // value and the return value of the call with the decreased number
    return +!~-n || n * f(n - 1);
}

document.write(f(7));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

A loop in JavaScript looks like this:

for (var = startvalue; var <= endvalue; var = var + increment) {
    // code to be executed
}
Nhan
  • 3,595
  • 6
  • 30
  • 38
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229