Reverse 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 ascending number triangle in JavaScript. Row length increases, but each row is printed in descending order.

This pattern is a useful variant for practicing opposite loop directions between outer and inner loops.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
21
321
4321
54321
1

Complete JavaScript Program

Outer loop grows row size. Inner loop prints values from current row size down to 1.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set total rows

const rows = 5; controls triangle height.

Setup
2

Outer loop (row growth)

for (let i = 1; i <= rows; i++) increases row length from 1 to rows.

Row control
3

Inner loop (print i..1)

for (let j = i; j >= 1; j--) appends descending values for that row.

Number printing
4

Print row output

console.log(line); outputs one completed row each iteration.

Line break
=

Reverse ascending triangle

Total printed digits are triangular in count: n(n+1)/2, with overall complexity 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 = i; j >= 1; j--) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate rows input before running nested loops
  • Add spaces between digits for readability
  • Use rows = 8 to see extended output
  • Swap inner loop direction to compare pattern differences
  • Display output in the DOM instead of using document.write

Avoid

  • Using j++ in this reverse inner loop pattern
  • Forgetting newline output after each row
  • Mixing row and column loop boundaries unintentionally
  • Using document.write in modern production interfaces

Key Takeaways

1

The outer loop controls row size growth.

2

The inner loop runs backward from i to 1.

3

Each row length increases by one while values descend.

4

Loop direction changes can generate entirely new pattern families.

❓ Frequently Asked Questions

Because the inner loop decrements from j = i to 1, so values are printed in reverse order within each row.
Increase rows to at least 6. Then the last row will include 654321.
Yes. Build each row in a string and print with console.log(line), as shown in Example 1.
O(n²), since total printed numbers are n(n+1)/2.

Explore More JavaScript Number Patterns!

Try more loop combinations to quickly master row/column logic and output control.

All Number Patterns →
Did you know?

Many interview pattern questions are solved by changing just one loop boundary or direction. This pattern is a perfect example of that principle.

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