JavaScript for loop
JavaScript For loop
1] for loop :-
javascript for loop iterates element for fixed number of time.
Syntax :-
for (initialization; condition; increment)
{
code executed
}
initialization :- is the count variable.
codition :- condition is called answer is true body than for loop executed.
condition :- condition is called answer is false than process stop
1] Example of for loop
Q. Iterate 1 to 25 print each number.
for (let i = 1; i < 25; i++) {
console.log(i);
}
for loop output :-
2] Example of for loop
Q. Iterate through array and print element.
fruits=['kiwi','pineapple','watermelon'];
for(let i=0; i<fruits.length; i++){
console.log(fruits[i]);
}
Output
3] Example of for loop
Q. Iterate backward form 5 to 0 print.
for (let i = 5; i >= 0; i--) {
console.log(i);
}
Output
4] Example of for loop
Q. for loop run 15 time
for(let i=0;i<15;i++)
{
console.log("iteration",i+1);
}
Explain:-
let i=0; initializes loop
counter i to 0
i<15 condition for loop
i is less than 15
i++ increments value i by 1
prints message display to the
console.log(iteration , i+1)
Output :-
Related Tutorial :-
Post a Comment