JavaScript: For Loop

A for loop is a looping block where the block inside a for conditioned is likely to be looped according to the given statement.

Syntax:

for(statement1 ; statement2 ; statement3){
//code goes here
}

Example:

for(let number = 1; number <= 7; number++) {
console.log(" The Rising Number " +number)
}

Output:

 The Rising Number 1
 The Rising Number 2
 The Rising Number 3
 The Rising Number 4
 The Rising Number 5
 The Rising Number 6
 The Rising Number 7
  •  Here inside the for condition place, I have said let a variable number by 1.
  •  Then let that variable number be less than or equal to 7.
  •  That means I want numbers from 1 to 7.
  •  And if we want that we need to increase/increment it.
  •  That's why we give variable name number++ to increment after every iteration the loop goes inside the body.
  •  It prints the number and comes back to the increment part and prints the corresponding/following numbers up to what I mentioned.
  •  It will stop and come out of the block once the condition is satisfied.
  •  Here when it reaches up to the point of 7 it executes the block.
  •  When it checks for 8 after 7. 8 is not less or equal to 7, so the loop ends and the code comes out of the block.
Fri, 10/01/2021 - 14:56

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags