Palindromic Number Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Ascend + descend (mirror)

What You’ll Learn

How to print a palindromic number triangle in JavaScript where each row increases from 1 up to the row number and then decreases back to 1.

This pattern is perfect practice for running two inner loops per row: one ascending, one descending.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
121
12321
1234321
123454321
1

Complete JavaScript Program

First print 1..i. Then print i-1..1 so the middle digit isn’t duplicated.

JavaScript
const rows = 5;

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

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

  for (let k = i - 1; k >= 1; k--) {
    line += k;
  }

  console.log(line);
}

🧠 How It Works

1

Set the row count

const rows = 5; sets the triangle height.

Setup
2

Outer loop controls rows

for (let i = 1; i <= rows; i++) creates row 1 through row 5.

Row control
3

Ascending part (1..i)

for (let j = 1; j <= i; j++) appends numbers increasing from 1 to the row value.

Ascend
4

Descending part (i-1..1)

for (let k = i - 1; k >= 1; k--) mirrors back down without repeating the middle digit.

Mirror
=

Row becomes a palindrome

Because the second half is the reverse of the first half, the row reads the same both ways.

2

Variation — Browser (document.write) Version

Print the pattern directly into an HTML page using document.write:

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

💡 Tips for Enhancement

Try These

  • Increase rows for a bigger triangle
  • Add spaces between numbers for readability when rows exceed 9
  • Center-align the triangle by printing leading spaces before each row
  • Replace numbers with letters to form an alphabet palindrome pattern

Avoid

  • Starting the second loop at i (duplicates the middle digit)
  • Mixing row/column logic (keep the outer loop for rows)
  • Forgetting the newline after each row

Key Takeaways

1

Each row is built from two parts: ascending then descending numbers.

2

Starting the reverse loop at i - 1 prevents duplicated centers.

3

This pattern demonstrates symmetry and mirroring with loops.

4

The same technique applies to diamonds and pyramid-style patterns.

❓ Frequently Asked Questions

Row 3 prints 1 2 3, then mirrors back with 2 1, producing 12321.
Print leading spaces before the ascending loop (for example, rows - i spaces) to align the apex in the center.
Yes. Append " " after each number and use trim() or trimEnd() to remove the last extra space.
For multi-digit numbers, add spacing or padding (like padStart) so the triangle keeps its shape.

Explore More JavaScript Number Patterns!

Try combining centering, spacing, and mirroring to create diamonds and pyramid patterns.

All Number Patterns →
Did you know?

Many symmetry-based patterns are just an ascending loop followed by a descending loop. The key is choosing the correct start value for the reverse (often i - 1).

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