Descending Prefix Number Triangle in JavaScript

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a descending prefix number triangle in JavaScript using nested loops. Each row starts from rows and expands by one value.

This pattern helps you practice loops where the start is fixed and the stop boundary shifts.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5
54
543
5432
54321
1

Complete JavaScript Program

Outer loop decreases row boundary, while inner loop prints from rows down to that boundary.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set row count

const rows = 5; defines top value and total rows.

Setup
2

Outer loop (boundary down)

for (let i = rows; i >= 1; i--) lowers the minimum value allowed in each row.

Row control
3

Inner loop (print rows..i)

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

Number printing
4

Print each row

console.log(line); outputs one row at a time.

Line break
=

Descending prefix triangle

Total printed values follow triangular growth: 1+2+…+n = n(n+1)/2, so 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 = rows; j >= i; j--) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate input and prevent non-positive row values
  • Add spaces between printed values for readability
  • Set rows = 7 to see a larger triangle
  • Swap loop direction to produce mirrored variants
  • Use DOM APIs instead of document.write for UI output

Avoid

  • Using wrong inner loop stop conditions
  • Forgetting row breaks after inner loop execution
  • Mixing fixed start and shifting start logic accidentally
  • Depending on document.write in production pages

Key Takeaways

1

The outer loop shifts the lower boundary each row.

2

The inner loop always starts from a fixed top value.

3

Row length grows by one while values remain in descending order.

4

Small loop-boundary edits create many new pattern variants.

❓ Frequently Asked Questions

On that iteration, boundary value is i = 3, so inner loop prints from 5 down to 3.
Program 4 shrinks rows from 54321 down to 5. Program 8 grows rows from 5 to 54321.
Yes. Example 1 uses console.log(line), which is better for algorithm practice.
O(n²), since total printed values are n(n+1)/2.

Explore More JavaScript Number Patterns!

Keep experimenting with loop ranges to unlock more pattern combinations quickly.

All Number Patterns →
Did you know?

Pattern programming is mostly about controlling loop start and end conditions. Master those, and most pattern questions become much easier.

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