for of loop ecmascript 6
The for
loop is a fundamental looping structure available in a wide range of programming languages, including ECMAScript 6. The for
loop enables you to iterate over a set of values, running a block of code a certain number of times. The structure of the for
loop is as follows:
for ([initialExpression]; [condition]; [incrementExpression]) {
// code block to be executed
}
In the "initialExpression" section, you can declare and initialize variables for use within the loop. The "condition" section is where the loop's logic is defined - the loop continues to run until the condition evaluates to false. The "incrementExpression" section is where you can increase or decrease the value of your loop variables, allowing for more complex looping behaviors. The code block within the for
loop can contain any JavaScript statements, such as assignment statements, arrays, objects, or conditionals. The loop will execute the code block once per iteration, and then move on to the next iteration. Here is an example of a for
loop that prints out the numbers 1 to 10:
for (let i = 1; i <= 10; i++) {
console.log(i);
}
In this example, the loop variable i
is declared and initialized to 1
in the initialExpression section. The condition section checks to see if i
is less than or equal to 10
, and if it is, the code block will be executed. After the code block is executed, the incrementExpression section is run, and i
is incremented by 1. This process is repeated until i
is greater than 10
, at which point the loop is terminated.