Right-Aligned 5-to-i Descending Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Indentation + descending range

What You’ll Learn

How to print a right-aligned triangle where each row starts at 5 and prints down to the row limit: 5, 5 4, 5 4 3, 5 4 3 2, 5 4 3 2 1.

You’ll practice indentation with spaces and a descending loop for the numbers.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1
1

Complete JavaScript Program

First, print indentation spaces. Then print numbers from size down to i. As i decreases, each row gets longer.

JavaScript
const size = 5;

for (let i = size; i >= 1; i--) {
  let line = "";

  for (let s = 1; s < i; s++) {
    line += "  ";
  }

  for (let j = size; j >= i; j--) {
    line += j + " ";
  }

  console.log(line.trimEnd());
}

🧠 How It Works

1

Set the size

const size = 5; sets the maximum number and the height.

Setup
2

Outer loop controls row limit

i counts down from 5 to 1. Smaller i means more numbers printed.

Rows
3

Indentation spaces

The loop s = 1..i-1 prints spaces so earlier rows shift to the right.

Indent
4

Print numbers size..i

for (j = size; j >= i; j--) prints 5 4 3 ... down to i on each row.

Digits
=

Right-aligned triangle

Spaces shrink while the descending sequence grows.

2

Variation — Browser (document.write) Version

Print the same output inside an HTML page using &nbsp; for indentation:

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

💡 Tips for Enhancement

Try These

  • Make size dynamic and print from size down to 1
  • Change the number loop direction to build other right-aligned triangles
  • Use fixed-width padding for multi-digit sizes (alignment stays clean)
  • Center-align by printing more spaces and using two halves

Avoid

  • Using normal spaces in HTML (use &nbsp; for stable indentation)
  • Forgetting to add a newline / <br> after each row
  • Mixing string concatenation and numbers without separators

Key Takeaways

1

Indentation is created by a loop that prints spaces before the digits.

2

The number loop prints a descending range from 5 down to i.

3

As i decreases, the triangle grows wider.

4

This is a common base for right-aligned and centered patterns.

❓ Frequently Asked Questions

On the first row, i = 5, so the number loop prints from 5 down to 5 (one value).
Start the outer loop from 1 and print from 5 down to i. That will make the first row include the full range.
Yes. In the console version we use trimEnd() to remove extra spacing.
Yes, but alignment may shift with multi-digit numbers. Use fixed-width padding for clean output.

Explore More JavaScript Number Patterns!

Indentation-based patterns help you master alignment logic for triangles and pyramids.

All Number Patterns →
Did you know?

Right alignment is often created by printing “padding” first. Once you can control padding, you can create centered and diamond patterns more easily.

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