This article will describe the “for” loop’s increment statement by 2.
The “for” statement constructs a loop with three optional arguments “initializer”, “conditional-statement” and the “final-expression” enclosed in parentheses and spaced by semicolons. The final expression is utilized to change the counter’s value.
For the increment by 2 in “for” loop, use the below-provided syntax:
In the above syntax:
- i=0 is called initializer, use “var” or “let” instead of “const” because it will throw an Uncaught TypeError: Assignment to constant variable.
- i<10 is the conditional statement that must be executed before every loop iteration.
- i+=2 is the final expression that increments by 2.
Let, print the even numbers between 0 and 10 using “for” loop, incrementing by 2:
The above output indicates that the loop increments by 2 and prints an even number between 0 and 10.
Create an array of number 1 to 10:
Here, iterate the loop over an array until its length, incrementing by 2:
The output displays an odd numbers between 1 and 10 from an array:
A “for” loop is utilized to repeatedly run a given block of code a certain number of times. It has three optional arguments, the initializer, the conditional statement, and the incremental or decremental expression. To increment by 2 in a for loop, use the “i += 2” as a final or incremental expression statement. This article described the for loop’s increment statement by 2.