JavaScript for loop
The for loop is used when we know in advance how many times the script should run.
‘for’ loop is a type of loop in JavaScript that is used to repeat a block of code. The syntax of the for loop is as follows:
for (initialization; condition; iteration) {
// code to be executed in each iteration
}
1) Initialization: It is the initialization of the counter. It is executed once before the execution of the code block.
2) Condition: This part is evaluated before each iteration. If the condition is true, the loop continues to execute. If it’s false, the loop terminates.
3) Iteration: It is the increment or decrement of the counter & executed (every time) after the code block has been executed.
Example -: Program in JavaScript to print name 5 times using for loop.
Output:
My name is Vishal
My name is Vishal
My name is Vishal
My name is Vishal
My name is Vishal