Repeated Number Triangle in JavaScript

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a repeated number triangle in JavaScript where each row repeats the same number multiple times.

This is a great nested-loop exercise for understanding the difference between printing i and printing j.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
22
333
4444
55555
1

Complete JavaScript Program

Outer loop chooses the row number. Inner loop repeats that row number exactly i times.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Choose row count

const rows = 5; sets the number of lines.

Setup
2

Outer loop (row value)

for (let i = 1; i <= rows; i++) picks which number to print on that row.

Row control
3

Inner loop (repeat i times)

for (let j = 1; j <= i; j++) runs i times, and line += i; repeats the row value.

Number printing
4

Print the row

console.log(line); outputs one repeated-number row per iteration.

Line break
=

Repeated number triangle

Total printed digits follow 1+2+…+n = n(n+1)/2, so time complexity is O(n²).

2

Variation — Browser (document.write) Version

Print the same pattern directly in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
const rows = 5;

for (let i = 1; i <= rows; i++) {
  for (let j = 1; j <= i; j++) {
    document.write(i);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate rows input before generating output
  • Add spaces between repeated numbers for readability
  • Print repeated characters instead of numbers for variants
  • Use user input to choose triangle size dynamically
  • Render output in a <pre> element via DOM APIs

Avoid

  • Appending j when you want repeated i
  • Skipping newline output between rows
  • Using non-numeric inputs without validation
  • Using document.write in production interfaces

Key Takeaways

1

The outer loop decides the value to be repeated on each row.

2

The inner loop controls how many times that value repeats.

3

Rows grow in size from 1 up to rows.

4

Changing one print statement (i vs j) creates a different pattern.

❓ Frequently Asked Questions

Because outer loop value is i = 4 and inner loop repeats output 4 times.
Set rows = 7. The last row will print 7 repeated seven times.
Yes. Example 1 already uses console.log(line) and is recommended for coding practice.
O(n²), since output count grows as n(n+1)/2.

Explore More JavaScript Number Patterns!

Practice more row/column combinations to quickly master nested-loop pattern problems.

All Number Patterns →
Did you know?

Printing i vs j inside the inner loop is one of the most common pattern-programming switches. One character change can create a totally different output.

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.

12 people found this page helpful