JavaScript Loop For JAVASCRIPT

JavaScript Loop For  

JavaScript Loop For

The For Loop

Loops are handy, if you want to run the same code over and over again, each time with a different value.

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example

for (i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}

The For/Of Loop

The JavaScript for/of statement loops through the values of iterable objects

for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.

The for/of loop has the following syntax:

for (variable of iterable) {
  // code block to be executed
}

variable - For every iteration, the value of the next property is assigned to the variable. Variable can be declared with constlet, or var.

iterable - An object that has iterable properties.

Example

var cars = ['BMW', 'Volvo', 'Mini'];
var x;

for (x of cars) {
  document.write(x + "<br >");
}

Download free E-book of JAVASCRIPT


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries