For is a repetitive instruction. It is used when you wish to perform a command several times.
In javaScript we can use the following repetitive instruction:

1. The for loop

The for loop is used when you know how many times the script should run. It has the following syntax:

for (nr_start; condition_nr; nr_increment) {
    code to be executed
}
- "nr_start" specifies a variable and assigns an initial value to it. It sets up the initial state for the loop.
"condition_nr" This second part tests for a condition. The loop will keep running as long as this condition is True.
"nr_increment increments or decrements the "nr_start", then its value is checked again with the second parameter, "condition_nr", until the result is FALSE.
Inside the for instruction you can introduce other "for" or any conditional statements.
Here's a simple example using the "for":
<script type="text/javascript"><!--
for (i=0; i<5; i++) {
  document.write("<br /> i = "+i);
}
--></script>
- These statements define a loop that uses the variable "i", initializes it with the value of zero, and loops as long as the value of "i" is less than 5.
- The increment expression, "i++', adds one unit to the value of "i" with each iteration of the loop.
- Because this happens at the end of the loop, the output will list the numbers zero through five.
This script will display the following result:
i = 0
i = 1
i = 2
i = 3
i = 4

2. The for ... in loop

The for…in loop is less flexible than an ordinary for() loop. This instruction goes through the elements of an object (or an Array) and gets the name (or key) of each property of that object.

The "for ... in" has the fallowing sintax:
for (variable in object) {
    instructions
}
- "variable" is a index variable that takes the name of the property. For each iteration of the loop, the variable is set to the next property of the object.
Example:
<script type="text/javascript"><!--
var obj = {apple:100, bool:false, astring:"coursesweb.net"};
for (var prop in obj) {
  document.write(prop + ' - '+ obj[prop]+ '<br />');
}
--></script>
- Results:
apple - 100
bool - false
astring - coursesweb.net

3. forEach

Another loop structure built on the "for" keyword, is the forEach property. Executes a provided function once per array element.

The general syntax is:
array.forEach(callbackF)
- callbackF = the function to execute for each element. This function can have three parameters: callbackF(val, index, array).
      - val = the element value.
      - index = the element index.
      - array = the array being traversed.

Example, printing the contents of an array.
<script type="text/javascript"><!--
var arr = [100, 'marplo.net', "http://coursesweb.net/"];

// callback function
function parseArr(val, index) {
  document.write('['+ index + '] = ' + val +'<br/>');
}

arr.forEach(parseArr);
--></script>
- This will produce the following output:
[0] = 100
[1] = marplo.net
[2] = http://coursesweb.net/

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute can be used to add CSS styles directly in the HTML tag?
type style class
<div style="width: 80%; border: 3px solid #888888;">Content</div>
Which CSS transform method distorts the HTML element in a given angle (including its content)?
translate() scale() skew()
#some_id {
  transform: skew(20deg, 25deg);
  -ms-transform: skew(20deg, 25deg);   /* IE 9 */
  -webkit-transform: skew(20deg, 25deg);   /* Safari and Chrome */
}
Click on the function which converts a Date object to a string.
indexOf() toString() getDate()
var rightnow = new Date();
alert( rightnow.toString() );
Which function applies a callback function to the elements of the given array?
array_merge() array_search() array_map()
$arr = arra("abc", "<p>xyz</p>", "<em>PHP</em>");
// apply the strip_tags() function to delete HTML tags from each array item
$arr = array_map("strip_tags", $arr);
}
For loops in JavaScript

Last accessed pages

  1. Bubbles3 (47598)
  2. SHA512 Encrypt hash in JavaScript (7776)
  3. Mahjong2 (855)
  4. Mahjong Games (17769)
  5. Get Lower, Higher, and Closest Number (1574)

Popular pages this month

  1. Qwop (3030)
  2. Drag Racer V3 (1516)
  3. Bubbles3 (728)
  4. Flash Games - Free online Games (677)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (665)