DEV Community

Max
Max

Posted on

2 1

JavaScript for loop

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
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

2
4
6
8
10
Enter fullscreen mode Exit fullscreen mode

Learn Javascript Tutorial

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more