Hello there guys, today I'll show some JavaScript tutorial that (might) be useful in the future. Thanks to W3Schools and MDN for being a good reference.
Loop was some useful function, it allows you to run the same code several times but with the different value. JavaScript supports many kind of loop with the different usage, here's the list :
For Loop
For loop is the loop that loops through a block of code a number of times.
Syntax :
So, let's get to the example.
It will declare the variable i and set the value to zero (0), before checks if the value of variable i is less than variablename and performs the succeeding statements before increasing the value of variable i by 1 after each pass through the loop.
Yep, I'll explain just this, I don't understand about loop and such that much rofl, hope you understand more. Thanks to Mozilla Developer Network and W3Schools for the help.
Loop was some useful function, it allows you to run the same code several times but with the different value. JavaScript supports many kind of loop with the different usage, here's the list :
- for - loops through a block of code a number of times
- for/in - loops through the properties of an object
- while - loops through a block of code while a specified condition is true
- do/while - also loops through a block of code while a specified condition is true
For Loop
For loop is the loop that loops through a block of code a number of times.
Syntax :
- Code:
for ([initialization]; [condition]; [final-expression])
statement
- Explanation of syntax:
- initialization
An expression(including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded.
condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.
final-expression
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.
statement
A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement (.
So, let's get to the example.
- Code:
var somevariable = variablename;
for (var i = 0, i < variablename; i++) {
document.body.innerHTML = 'Hello world. This is my first loop, you can call this a test loop';
It will declare the variable i and set the value to zero (0), before checks if the value of variable i is less than variablename and performs the succeeding statements before increasing the value of variable i by 1 after each pass through the loop.
Yep, I'll explain just this, I don't understand about loop and such that much rofl, hope you understand more. Thanks to Mozilla Developer Network and W3Schools for the help.