for loop in JavaScript

The loop for allows you to repeat some code a specified number of times. Here is its syntax:

for ( initial commands; termination condition; commands after iteration ) { loop body }

The initial commands are what will be executed before the start of the loop. They will be executed only once. Usually, the initial values ​​of the counters are placed there, for example: i = 0.

The condition of the loop termination is a condition under which the loop will spin, while it is true , for example: i <= 10.

The commands after the loop iteration are the commands that will be executed each time the loop iteration ends. Usually counters are increased there, for example: i++.

Let's use the loop for to print successively the numbers from 1 to 9:

for (let i = 1; i <= 9; i++) { console.log(i); // shows 1, 2... 9 }

And now we will increase the counter not by 1, but by 2:

for (let i = 1; i <= 9; i += 2) { console.log(i); // shows 1, 3, 5... }

You can do a countdown:

for (let i = 10; i > 0; i--) { console.log(i); // shows 10, 9, 8... }

Use the loop for to print the numbers from 1 to 100 to the console.

Use the for loop to print the numbers from 11 to 33 to the console.

Use the for loop to output even numbers between 0 and 100 to the console.

Use the for loop to output odd numbers in the range from 1 to 99 to the console.

Use the loop for to print the numbers from 100 to 0 to the console.

enru