Right-Aligned Descending Number Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Right alignment with spaces

What You’ll Learn

How to print a right-aligned triangle where each row shows descending digits ending at 1: 1, 21, 321, 4321, 54321.

You’ll practice using a condition inside a loop to print either spacing or digits.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
    1
   21
  321
 4321
54321
1

Complete JavaScript Program

The inner loop always counts from size down to 1. For early columns where j > i, we print spaces; otherwise we print the digit.

JavaScript
const size = 5;

for (let i = 1; i <= size; i++) {
  let line = "";
  for (let j = size; j >= 1; j--) {
    line += (j > i) ? "  " : String(j);
  }
  console.log(line.trimEnd());
}

🧠 How It Works

1

Choose a size

const size = 5; sets both the height and the width of the triangle.

Setup
2

Outer loop builds rows

for (let i = 1; i <= size; i++) increases the number of printed digits each row.

Rows
3

Inner loop scans from size..1

for (let j = size; j >= 1; j--) ensures digits appear in descending order on each row.

Descending
4

Condition decides spaces vs digit

If j > i, we print spacing. Otherwise we print j.

Alignment
=

Right-aligned row

Spaces come first, then the descending digits appear on the right.

2

Variation — Browser (document.write) Version

Print the pattern directly into an HTML page using document.write and &nbsp; spacing:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j;
for (i = 1; i <= 5; i++) {
  for (j = 5; j >= 1; j--) {
    if (j > i)
      document.write("&nbsp;&nbsp;");
    else
      document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change size to create a bigger triangle
  • Print spaces between digits for larger sizes to improve readability
  • Flip the loop direction to get an ascending right-aligned triangle
  • Replace digits with symbols to create a right-aligned symbol triangle

Avoid

  • Using normal spaces in HTML (use &nbsp; for stable alignment)
  • Forgetting to trim trailing spaces in console output
  • Mixing row/column roles (keep i for rows)

Key Takeaways

1

Leading spaces are produced by a simple condition (j > i).

2

Counting j downward prints digits in descending order.

3

The number of printed digits grows by one each row.

4

This is a common foundation for right-aligned pyramids and diamonds.

❓ Frequently Asked Questions

On row 3, only digits where j <= 3 print, and the loop runs from 5 down to 1, so the printed digits are 3, 2, 1.
If you don’t want alignment, remove the j > i branch and just print j when j <= i.
Yes. Print the digits first, then spaces (or just omit the spacing logic), to align to the left.
Add spacing or padding for each printed number (e.g., padStart) so multi-digit values don’t break alignment.

Explore More JavaScript Number Patterns!

Right-aligned patterns teach spacing logic that you’ll reuse in pyramids and diamonds.

All Number Patterns →
Did you know?

Most alignment problems in pattern printing come down to deciding when to print “padding” vs “content”. The j > i check here is a classic example.

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