Reverse-Start Ascending Number Triangle in JavaScript

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

What You’ll Learn

How to print a reverse-start ascending number triangle in JavaScript using nested loops. Each next row starts with a smaller number and extends to rows.

This pattern demonstrates how changing the inner loop start creates a left-growing sequence.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5
45
345
2345
12345
1

Complete JavaScript Program

Outer loop decreases from rows to 1. Inner loop prints from the current row value up to rows.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set rows value

const rows = 5; sets the maximum number and number of rows.

Setup
2

Outer loop (rows down)

for (let i = rows; i >= 1; i--) lowers starting number each row.

Row control
3

Inner loop (print i..rows)

for (let j = i; j <= rows; j++) prints values from current start to max value.

Number printing
4

Print line output

console.log(line); outputs each generated row.

Line break
=

Reverse-start ascending triangle

Total printed numbers still sum to n(n+1)/2, so runtime 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 = rows; i >= 1; i--) {
  for (let j = i; j <= rows; j++) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate row input before printing pattern output
  • Add spaces between numbers for cleaner display
  • Try rows = 7 to observe the same growth behavior
  • Convert logic into a reusable function
  • Render output in a <pre> element using DOM APIs

Avoid

  • Using incorrect inner loop direction for this pattern style
  • Forgetting newline output after each row
  • Mixing loop boundaries (i vs rows) accidentally
  • Depending on document.write for production pages

Key Takeaways

1

The outer loop decreases the start value from rows to 1.

2

The inner loop prints from i up to rows.

3

Rows grow in length while their first value decreases.

4

Changing loop starts/ends is the key to building new number patterns.

❓ Frequently Asked Questions

Initially i = rows, so inner loop runs once from j = 5 to 5.
When i = 2, inner loop runs from 2 to 5, producing 2345.
Yes, Example 1 already uses console.log(line), which is cleaner for coding practice.
O(n²), because total printed values are n(n+1)/2.

Explore More JavaScript Number Patterns!

Experiment with loop boundaries and directions to build faster intuition for pattern programming.

All Number Patterns →
Did you know?

Pattern programs often differ by just one loop boundary. Tiny loop edits can transform output completely, which makes them excellent for mastering nested loops.

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