Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Javascript Loops


Javascript Loops

By rockbll4 avatarrockbll4 | 4452 Reads |
0     0

Loops in Javascript by RockBll4

First of all this is my second first article, so dont be too harsh.

Well I know some Javascript and I thought I'd do a little article on Loops.

So the loops in javascript are: the for loop and the while loop.

THE FOR LOOP:

The for loop is used when you know in advance how many times the script should run.

the syntax for the for loops is as follow:

for ( var = value; var <= endvalue; var(increment) ) { code to be executed within the code block }

Example:

<html> <head>Javascript for Loop Example</head> <body>

<script type="text/javascript" language="Javascript"> <!–

var i = 1;

for ( i = 1; i <= 5; i++ ) { docu<i></i>ment.write("Hi " + i + " times"); docu<i></i>ment.write("<br/>"); }

//–> </script>

</body> </html>

Result:

Hi 1 times Hi 2 times Hi 3 times Hi 4 times Hi 5 times

THE WHILE LOOP:

The while loop is used when you want the loop to execute and continue executing while the specified condition is true.

the syntax for the while loop is:

while ( var <= endvalue) { Code to be executed within the code block var(increment); }

Example:

<html> <head>Javascript while Loop Example</head> <body>

<script type="text/javascript" language="Javascript"> <!–

var i = 1;

while ( i <= 5 ) { docu<i></i>ment.write("Hello " + i + " times."); docu<i></i>ment.write("<br/>");

i++

}

//–> </script>

</body> </html>

Result:

Hello 1 times. Hello 2 times. Hello 3 times. Hello 4 times. Hello 5 times.

There is your crash course on the loops in Javascript.

If you would like to further your understanding on loops or Javascript go to google and do some research ;-).

Comments
ghost's avatar
ghost 18 years ago

nice.. this couldd be usefull to change js14 … using some cookis :D :ninja: :happy:

n30's avatar
n30 18 years ago

js14 is down you know H-oLm35…. anyway, nice article:angry: gives u 2 thumbs up. Next time, be aware that it doesn't accept HTML tags:ninja: 4/5

rockbll4's avatar
rockbll4 18 years ago

lol ik that it doenst accept html tags, it still emphysizes the main parts tho (if you know html ;))

ghost's avatar
ghost 17 years ago

it does acccept html…just not after you click on the comments.:ninja: