JavaScript for loop

max24816

Max

Posted on April 10, 2023

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

💖 💪 🙅 🚩
max24816
Max

Posted on April 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

JavaScript for of loop tutorial
javascript JavaScript for of loop tutorial

April 29, 2023

JavaScript for loop
javascript JavaScript for loop

April 10, 2023

JavaScript for in loop
javascript JavaScript for in loop

April 22, 2023