Javascript for loop is used execute a block of code repeatedly, based on a condition, until the condition is no longer true. In this article you will learn how for loop works.
Syntax of the Javascript For Loop
for (initialization; condition; increment/decrement) {
// code block to be executed
}
for loop require three element to make it works,
initialization
used to declare or set a value before starting the loop.
condition
block will be executed for each iteration in the loop, this will evaluate from the start. If the condition fails then the loop will be terminated.
increment / decrement
block will execute at the end of each iteration. It usually increments or decrements the counter variable.
Javascript Program to Check if a Number is Odd or Even
for (let i = 1; i <= 10; i++) {
if(i % 2 == 0) {
console.log(i);
}
}
Output
2
4
6
8
10
Top comments (0)