LOLWesta, a social media forum


You are not connected. Please login or register

View previous topic View next topic Go down Message [Page 1 of 1]

1
[#1] Javascript Lesson on Wed Dec 24, 2014 3:44 am

LOLWesta


LOLWesta King
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. Wink




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 expres​sion(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 (Wink.

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. Smile Thanks to Mozilla Developer Network and W3Schools for the help.

View user profile http://lolwesta.forummotions.com

2
Re: [#1] Javascript Lesson on Wed Dec 24, 2014 6:36 am

Tsu


Members
Members
Like this?

Code:
var loop = for (var i = 0, i < 13; i++) {
document.write('This is a test');
}

console.log( loop )

View user profile

3
Re: [#1] Javascript Lesson on Wed Mar 04, 2015 7:16 am

Left


Members
Members
Why did you use comma? You have to use this symbol : ;. If you use commas, then the code will never work forever. Then, don't use W3S as a reference forking place.

View user profile

Sponsored content


View previous topic View next topic Back to top Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum