Descending Number Triangle in JavaScript

What You’ll Learn
How to print a descending number triangle in JavaScript using nested for loops. Each row starts at 1 and prints up to a decreasing limit: rows, rows-1, ..., down to 1.
This pattern is a simple way to understand how the outer loop controls rows and the inner loop controls how many numbers appear per row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
12345
1234
123
12
1Complete JavaScript Program
The outer loop starts from rows and counts down. The inner loop adds numbers from 1 to the current row limit.
const rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
}🧠 How It Works
Choose the row count
const rows = 5; sets how many lines will be printed.
Outer loop (descending rows)
for (let i = rows; i >= 1; i--) controls the triangle height and ensures each next row has one fewer number than the previous.
Inner loop (print 1..i)
for (let j = 1; j <= i; j++) adds digits from 1 up to the current row limit i to line.
New line output
console.log(line); prints one completed row, then moves to the next line in the console.
Descending number triangle
Total numbers printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — Browser (document.write) Version
Print the same pattern directly in an HTML page using document.write:
<!DOCTYPE html>
<html>
<body>
<script>
const rows = 5;
for (let i = rows; i >= 1; i--) {
for (let j = 1; j <= i; j++) {
document.write(j);
}
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing the pattern - Print the ascending triangle by counting the outer loop up
- Add spaces between numbers (for example,
line += j + " ";) - Right-align the triangle by printing leading spaces before each row
- Replace numbers with characters to create alphabet patterns
Avoid
- Forgetting the newline after each row
- Using negative or zero rows without validating input
- Mixing row/column logic (keep the outer loop for rows)
- Overusing
document.writein production code; prefer console output or DOM updates
Key Takeaways
The outer loop decreases the row length (from rows down to 1).
The inner loop adds numbers from 1 to the current row limit i.
Total printed digits follow the triangular number count: n(n+1)/2.
The same nested-loop idea applies to star triangles and alphabet triangles.
❓ Frequently Asked Questions
12345, then 1234, then 123, and so on until 1.i (from rows down to 1). The inner loop builds values from 1 to i, then prints the row.for (let i = 1; i <= rows; i++) as the outer loop. Each row will then grow from 1 number up to rows numbers.Explore More JavaScript Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
The total number of printed digits grows like a triangular number. If you print n rows, the total count is 1+2+…+n = n(n+1)/2. For n = 5, that’s 15 digits.
12 people found this page helpful
