
Looping Statements in JS
Looping Statements in JavaScript Looping statements in JavaScript are used to repeat a block of code multiple times until a condition becomes false. Instead of writing the same code again and again, loops help make the code shorter and easier to manage. JavaScript mainly provides the following loops: 1) for Loop The for loop is used when we know how many times the loop should run. for ( let i = 1 ; i <= 5 ; i ++ ) { console . log ( i ); } Output: 1 2 3 4 5 Here, the loop starts from 1 and runs until 5. 2) while Loop The while loop runs as long as the given condition is true. let i = 1 ; while ( i <= 5 ) { console . log ( i ); i ++ ; } This also prints numbers from 1 to 5. Example Programs: Looping Programs in JavaScript 1) 1 1 1 1 1 Print the number 1 five times. for ( let i = 1 ; i <= 5 ; i ++ ) { console . log ( 1 ); } Output: 1 1 1 1 1 2) 1 2 3 4 5 Print numbers from 1 to 5. for ( let i = 1 ; i <= 5 ; i ++ ) { console . log ( i ); } Output: 1 2 3 4 5 3) 1 3 5 7 9 Print the first fiv
Continue reading on Dev.to
Opens in a new tab


