Right-Angled Triangle Star Pattern in JavaScript

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:
*
**
***
****
*****Complete JavaScript Program
Fixed rows = 5 version (prints using nested loops):
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
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.
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).
Emit the row
console.log(line) prints the finished row to the console (with a newline). Repeat for every i.
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
Row i prints exactly i stars.
Total stars for n rows is \(n(n+1)/2\).
repeat() is a clean way to build each row in JavaScript.
Time complexity is O(n²) because total printed characters grow quadratically.
This is a great starter pattern to learn loops.
❓ Frequently Asked Questions
"*".repeat(i) is concise and readable."* ".repeat(i).trimEnd() for each row.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.
In JavaScript, repeat() is implemented in native code and is often faster than manual concatenation for moderate sizes.
10 people found this page helpful
