0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Numbers 1 to n

Use a for loop in JavaScript like for(let i = 1; i <= n; i++) { console.log(i); } to print numbers from 1 to n.
📋

Examples

Input1
Output1
Input5
Output1 2 3 4 5
Input0
Output
🧠

How to Think About It

To print numbers from 1 to n, start counting from 1 and keep adding 1 until you reach n. For each number, show it on the screen. Stop when you reach n.
📐

Algorithm

1
Get the input number n.
2
Start a counter i at 1.
3
While i is less than or equal to n, do the following:
4
Print the current value of i.
5
Increase i by 1.
6
Stop when i becomes greater than n.
💻

Code

javascript
const n = 5;
for (let i = 1; i <= n; i++) {
  console.log(i);
}
Output
1 2 3 4 5
🔍

Dry Run

Let's trace the program with n = 3 to see how it prints numbers.

1

Initialize n and i

n = 3, i = 1

2

Check if i <= n

1 <= 3 is true, so print 1

3

Increment i

i becomes 2

4

Check if i <= n

2 <= 3 is true, so print 2

5

Increment i

i becomes 3

6

Check if i <= n

3 <= 3 is true, so print 3

7

Increment i

i becomes 4

8

Check if i <= n

4 <= 3 is false, stop loop

iCondition i <= nAction
1truePrint 1
2truePrint 2
3truePrint 3
4falseStop
💡

Why This Works

Step 1: Start from 1

We begin counting at 1 because the task is to print numbers starting from 1.

Step 2: Use a loop to repeat

The for loop repeats the printing action until the number reaches n.

Step 3: Stop after n

The loop stops when the number goes beyond n, ensuring only numbers 1 to n are printed.

🔄

Alternative Approaches

while loop
javascript
const n = 5;
let i = 1;
while (i <= n) {
  console.log(i);
  i++;
}
Uses a while loop instead of for; slightly longer but works the same.
recursive function
javascript
function printNumbers(i, n) {
  if (i > n) return;
  console.log(i);
  printNumbers(i + 1, n);
}
printNumbers(1, 5);
Uses recursion to print numbers; elegant but can cause stack issues for very large n.
Array and forEach
javascript
const n = 5;
Array.from({ length: n }, (_, i) => i + 1).forEach(num => console.log(num));
Creates an array and prints each number; less direct but shows array usage.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so it executes n times, making the time complexity O(n).

Space Complexity

The program uses a fixed amount of extra space regardless of n, so space complexity is O(1).

Which Approach is Fastest?

The simple for loop is fastest and most readable; recursion adds overhead and risk of stack overflow; array methods use extra memory.

ApproachTimeSpaceBest For
For loopO(n)O(1)Simple and efficient counting
While loopO(n)O(1)Similar to for loop, slightly less concise
Recursive functionO(n)O(n)Elegant but risky for large n due to stack
Array with forEachO(n)O(n)When working with arrays or functional style
💡
Use a for loop for simple counting tasks like printing numbers from 1 to n.
⚠️
Starting the loop from 0 instead of 1 when the task requires numbers from 1 to n.