Reverse Descending 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 descending number triangle in JavaScript using nested for loops. Each row starts from a decreasing value and counts down to 1.

This pattern is useful for learning how to control both a decreasing outer loop and a decreasing inner loop.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
4321
321
21
1
1

Complete JavaScript Program

The outer loop decreases the starting number each row. The inner loop prints from that value down to 1.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set the row count

const rows = 5; defines both the first row start and total rows.

Setup
2

Outer loop (rows down)

for (let i = rows; i >= 1; i--) reduces the row start value each iteration.

Row control
3

Inner loop (print i..1)

for (let j = i; j >= 1; j--) appends descending values from i down to 1.

Number printing
4

Print one row per line

console.log(line); outputs each completed row on a new line.

Line break
=

Reverse descending triangle

Total printed digits are still triangular: 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 = rows; i >= 1; i--) {
  for (let j = i; j >= 1; j--) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Add spaces between digits to improve readability
  • Reverse the inner loop direction to get increasing rows
  • Use user input to generate dynamic pattern sizes
  • Replace digits with letters to build reverse alphabet patterns

Avoid

  • Forgetting to decrement loop variables in reverse patterns
  • Using wrong loop conditions that cause infinite loops
  • Skipping newline output after each row
  • Overusing document.write in modern production pages

Key Takeaways

1

The outer loop moves from rows down to 1 and sets each row’s start value.

2

The inner loop prints from i down to 1 in reverse order.

3

Total printed digits still follow the triangular count: n(n+1)/2.

4

This reverse loop technique is helpful for star patterns and other mirrored designs.

❓ Frequently Asked Questions

Because the outer loop starts from rows (5) and the inner loop prints down from i to 1, so the first row becomes 54321.
After the first iteration, i becomes 4. Then inner loop j runs from 4 down to 1, producing 4321.
Yes. Build each row in line and print with console.log(line). This is generally cleaner for algorithm practice.
O(n²) for n rows, since total printed values are n(n+1)/2.

Explore More JavaScript Number Patterns!

Try more number pattern variations to master loop direction and boundary control.

All Number Patterns →
Did you know?

Changing only loop direction can produce completely different visuals. Reverse loops are a core technique for mirrored and descending pattern problems.

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