0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Star Pattern

Use nested for loops in JavaScript to print a star pattern, for example: for(let i=1; i<=5; i++){ let stars = ''; for(let j=1; j<=i; j++){ stars += '*'; } console.log(stars); } prints a triangle of stars.
📋

Examples

Input5
Output* ** *** **** *****
Input3
Output* ** ***
Input1
Output*
🧠

How to Think About It

To print a star pattern, think of rows and columns. For each row, print stars equal to the row number. Use one loop to go through rows and a nested loop to print stars in each row.
📐

Algorithm

1
Get the number of rows as input.
2
Start a loop from 1 to the number of rows.
3
For each row, create an empty string for stars.
4
Use a nested loop to add stars equal to the current row number.
5
Print the star string for the current row.
6
Repeat until all rows are printed.
💻

Code

javascript
const rows = 5;
for (let i = 1; i <= rows; i++) {
  let stars = '';
  for (let j = 1; j <= i; j++) {
    stars += '*';
  }
  console.log(stars);
}
Output
* ** *** **** *****
🔍

Dry Run

Let's trace printing 3 rows of stars through the code

1

Start outer loop with i=1

stars = ''

2

Inner loop runs j=1 to 1

stars = '*'

3

Print stars for row 1

*

4

Outer loop i=2, reset stars = ''

5

Inner loop j=1 to 2

stars = '**'

6

Print stars for row 2

**

7

Outer loop i=3, reset stars = ''

8

Inner loop j=1 to 3

stars = '***'

9

Print stars for row 3

***

Row (i)Stars Printed
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to the number of rows, deciding how many lines to print.

Step 2: Inner loop builds stars

The inner for loop runs from 1 to the current row number, adding stars to the string.

Step 3: Print stars line by line

After building the star string for a row, console.log prints it, creating the pattern line by line.

🔄

Alternative Approaches

Using repeat() method
javascript
const rows = 5;
for (let i = 1; i <= rows; i++) {
  console.log('*'.repeat(i));
}
This method is shorter and uses built-in string repeat, but may be less clear for beginners.
Using while loops
javascript
let i = 1;
const rows = 5;
while (i <= rows) {
  let stars = '';
  let j = 1;
  while (j <= i) {
    stars += '*';
    j++;
  }
  console.log(stars);
  i++;
}
Uses while loops instead of for loops, which some may find easier to understand.

Complexity: O(n^2) time, O(n) space

Time Complexity

The outer loop runs n times and the inner loop runs up to n times per outer iteration, resulting in O(n^2) time.

Space Complexity

The space used is O(n) for the star string in the worst case, which is recreated each row.

Which Approach is Fastest?

Using the repeat() method is faster and cleaner, but nested loops give more control and clarity.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(n)Learning loops and control
String repeat()O(n^2)O(n)Concise and modern code
While loopsO(n^2)O(n)Alternative loop style
💡
Use nested loops where the outer loop controls rows and the inner loop controls stars per row.
⚠️
Beginners often forget to reset the star string inside the outer loop, causing all stars to print on one line.