Right-Angled Triangle Star Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 1 Code Example
n(n+1)/2 stars total

What You'll Learn

This is the first star pattern in the series. For each row, print one more * than the previous row. In JavaScript, you can do this with nested loops or with repeat().

Total stars printed for rows = n is \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\).

⭐ Pattern Output

When you run the program with rows = 5:

Output
*
**
***
****
*****
1

Complete JavaScript Program

Fixed rows = 5 version (prints using nested loops):

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) line += "*";
  console.log(line);
}

🧠 How It Works

1

Setup and outer row loop

const rows = 5; fixes the height. for (let i = 1; i <= rows; i++) walks each row index; let line = ""; starts an empty string for that row.

Setup
2

Inner loop: append stars

for (let j = 1; j <= i; j++) line += "*"; runs i times, growing line without a line break. You can swap this for "*".repeat(i).

Stars
3

Emit the row

console.log(line) prints the finished row to the console (with a newline). Repeat for every i.

Output
=

Right-angled triangle

Star counts 1+2+…+n = n(n+1)/2. O(n²) output for n = rows, O(1) extra memory besides the current row string. Long rows scroll inside the green preview on phones.

💡 Tips for Enhancement

Try These

  • Use "*".repeat(i) to build each row without an inner loop
  • Print the triangle with spaces between stars (e.g., "* ".repeat(i))
  • Store rows in an array and join with "\n" for testing
  • Try the inverted triangle next (Program 2)

Avoid

  • Building huge strings with + in tight loops for very large inputs
  • Forgetting to print a newline after each row
  • Mixing tabs and spaces (alignment breaks in other patterns)
  • Printing extra spaces at the end of each line (harder to compare outputs)

Key Takeaways

1

Row i prints exactly i stars.

2

Total stars for n rows is \(n(n+1)/2\).

3

repeat() is a clean way to build each row in JavaScript.

4

Time complexity is O(n²) because total printed characters grow quadratically.

5

This is a great starter pattern to learn loops.

❓ Frequently Asked Questions

For learning, nested loops are perfect. For clean JavaScript, "*".repeat(i) is concise and readable.
Print "* ".repeat(i).trimEnd() for each row.
It’s O(n²) for n rows, because total printed characters are proportional to \(n(n+1)/2\).

Next: Inverted Right-Angled Triangle

Continue to Program 2 to print the inverted version of this triangle.

Program 2 →
Did you know?

In JavaScript, repeat() is implemented in native code and is often faster than manual concatenation for moderate sizes.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful