In this example, you will learn how to print numbers from 1 to 100 using various methods in JavaScript.
The first approach is to use a for loop:
for(let i=1; i<=100; i++){ // Print each number console.log(i); }
Output:
1 2 3 4 5 6 7 8 9 10 ... ...97 98 99 100
Example 2: Using While Loop
Create a variable num
and set its value to 1 initially.
Inside the while loop, increase the value of num
by 1 for each iteration and run the while loop until the value of num
becomes 100.
let num = 1; while(num<=100){ // Print each number console.log(num); // Increase num by 1 num = num + 1; }
Output:
1 2 3 4 5 6 7 8 9 10 ... ...97 98 99 100