Javascript Course


for() is a repetitive instruction. It is used when you wish to perform a command several times.


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.
<script>
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.

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.

Syntax:
for(variable in object){
 // instructions
}
- 'variable' is an 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>
var obj = {apple:100, bool:false, astring:'coursesweb.net'};
for(var prop in obj){
 document.write(prop + ' - '+ obj[prop]+ '<br>');
}
</script>

The for...of instruction

With 'for...of' you can parse the properties of an object or the elements of an Array.
The difference from "for...in" is that the for...in keeps in 'variable' the property name (or index for array), but the for...of keeps in 'variable' the property value (or the item value, for array).

Syntaxa:
for(variable of object){
 //code to be executed
}
- "variable" is a variable that contains the value of the current property (or the item value, for array) from the traversed object.
Example:
<script>
var arr =['MarPlo.net', 'GamV.eu', 80];
for(var elm of arr){
 document.write('<br>'+ elm);
}
</script>

forEach()

forEach() is a JavaScript method used to traverse Array items. forEach executes a provided function once per array element.

Syntax:
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>
var arr = [100, 'marplo.net', 'https://coursesweb.net/'];

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

arr.forEach(parseArr);
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
For loops in JavaScript

Last accessed pages

  1. Get Lower, Higher, and Closest Number (5342)
  2. Simple Admin Login PHP Script (10894)
  3. jQuery parent, children and nth-child() (13826)
  4. Display UL bullets and OL numbers on the right side (8088)
  5. Convert BBCode to HTML and HTML to BBCode with JavaScript (9298)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide