Js Loop For in JavaScript
? JavaScript for Loop
The for loop is one of the most commonly used loops in JavaScript. It allows you to run a block of code repeatedly for a fixed number of iterations.
? Syntax
for (initialization; condition; increment) { // code to be executed repeatedly}initialization: executed once before the loop starts (usually sets a counter)
condition: checked before each iteration; if
true, loop continuesincrement: executed after each iteration (usually updates the counter)
? Example: Basic for Loop
for (let i = 0; i < 5; i++) { console.log("Iteration number: " + i);}Output:
Iteration number: 0Iteration number: 1Iteration number: 2Iteration number: 3Iteration number: 4? Explanation
let i = 0: Start withiat 0i < 5: Loop runs whileiis less than 5i++: Increaseiby 1 after each iteration
? Example: Looping Through an Array
const fruits = ["apple", "banana", "cherry"];for (let i = 0; i < fruits.length; i++) { console.log(fruits[i]);}Output:
applebananacherryWould you like me to explain other loops like for...of or while next?