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.
for(i=0; i<5; i++) {
 document.write('<br> i = '+i);
- 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.

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.
var obj = {apple:100, bool:false, astring:''};
for(var prop in obj){
 document.write(prop + ' - '+ obj[prop]+ '<br>');

The for...of instruction

With 'for...of' you can parse the properties of an object or the elements of an Array.
The difference from "" is that the 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).

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.
var arr =['', '', 80];
for(var elm of arr){
 document.write('<br>'+ elm);


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

- 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.
var arr = [100, '', ''];

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


Daily Test with Code Example

Which type of <input> creates input fields that should contain a numeric value?
type="text" type="number" type="date"
<input type="number" name="points" min="5" max="80" />
Which CSS property allows to change the transparency of an element?
font-style opacity color
#id {
  filter:alpha(opacity=40);    /* for IE */
Click on the function that formats a number to specified number of characters.
toPrecision() exp() toFixed()
var num = 12.34567;
alert( num.toPrecision(3) );       // 12.3
Which function randomizes the order of the elements into an array?
natsort() shuffle() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"");
var_export($lang);     // array (0=>"", 1=>"PHP", 2=>"JavaScript")
For loops in JavaScript

Last accessed pages

  1. Sending data with GET and POST in the same request (5726)
  2. Countdown Timer with starting time added into a form (8694)
  3. jQuery UI draggable - Drag elements (10637)
  4. Animating in Flash - Frame-by-Frame Animation (2230)
  5. Selection Tools (6281)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (233)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (158)
  3. Read Excel file data in PHP - PhpExcelReader (147)
  4. querySelector and querySelectorAll (104)
  5. Wake Up! (101)