Write a For Loop in Javascript
To write a for loop in Javascript:
for(j=0;j<50;j++) {
document.write(j + " ");
}
Which Prints:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
-We created a variable and named it
j because j is easy to type.
-We started
j at zero. j=0
-We said as long as
j is less than 50, keep going. j<50
-We incremented, or added one, each time through the loop. j++
-We used document.write to print
j, plus a space, to the browser each time through the loop.
Free Copy and Paste Javascript Code:
<script type="text/javascript">
<!--
for(j=0;j<50;j++) {
document.write(j + " ");
}
-->
</script>
Resources:
Exit a For Loop in Javascript with Break
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group