Left-Aligned 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 left-aligned descending number triangle in JavaScript using nested for loops. Every row starts from the same top number, but row length decreases.

This pattern helps you learn how to keep one loop boundary fixed while adjusting the other boundary per row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
5432
543
54
5
1

Complete JavaScript Program

The outer loop controls row count. The inner loop always starts at rows and prints down to the current row boundary.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Choose row count

const rows = 5; sets both pattern size and top value.

Setup
2

Outer loop (row boundary)

for (let i = 1; i <= rows; i++) increases the minimum value allowed in each row.

Row control
3

Inner loop (print rows..i)

for (let j = rows; j >= i; j--) prints from top value down to current boundary i.

Number printing
4

Output each row

console.log(line); prints one full row per line.

Line break
=

Left-aligned descending triangle

The total digits printed follow n(n+1)/2, so this pattern also runs in 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 = rows; j >= i; j--) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate row input before generating output
  • Insert spaces between numbers for readability
  • Use line += j + " "; and trim right spaces
  • Swap loop boundaries to generate related descending patterns
  • Render output in a DOM element instead of document.write

Avoid

  • Changing both loop limits at once without checking output shape
  • Using incorrect comparison signs in descending loops
  • Missing newline output between rows
  • Relying on document.write in production interfaces

Key Takeaways

1

The outer loop controls how much each row shrinks.

2

The inner loop starts from a fixed top value and stops at the current boundary.

3

Output shape depends on loop start, end, and direction choices.

4

This loop strategy is useful across many number pattern variations.

❓ Frequently Asked Questions

Because inner loop starts from rows each time. Only the loop stop value changes row by row.
Program 3 starts each row from a changing value (i) and prints down. Program 4 starts from a fixed value (rows) and shrinks the row end.
Yes. Build each row in a string and print with console.log(line), as shown in Example 1.
O(n²), with total printed digits equal to n(n+1)/2.

Explore More JavaScript Number Patterns!

Keep practicing nested loops to quickly recognize and build new number pattern combinations.

All Number Patterns →
Did you know?

Small loop-boundary changes can generate entirely new visual patterns. That’s why pattern programming is great loop practice for interviews and coding rounds.

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