Centered Increasing Number Pyramid in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Fixed-width cells + counter

What You’ll Learn

How to print a centered number pyramid where numbers increase continuously across rows: 1, then 2 3, then 4 5 6, and so on.

You’ll learn how to keep alignment stable by printing empty cells first and using a single counter variable.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
            1
         2  3
      4  5  6
   7  8  9  10
11 12 13 14 15
1

Complete JavaScript Program

We print a fixed number of โ€œcellsโ€ per row. If we’re before the row start (j > i), we print an empty cell; otherwise we print the next number.

JavaScript
const size = 5;
let k = 1;

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

  for (let j = size; j >= 1; j--) {
    if (j > i) {
      line += "   ";
    } else {
      line += String(k++).padStart(2, " ") + " ";
    }
  }

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

🧠 How It Works

1

Initialize the counter

let k = 1; is the next value to print, and it keeps increasing across rows.

Counter
2

Outer loop controls rows

i runs from 1..size, so each row prints one more number than the previous row.

Rows
3

Inner loop creates fixed-width cells

The loop j = size..1 ensures each row has the same number of cell positions, which makes centering easy.

Cells
4

Decide empty vs filled cell

If j > i, we add empty spacing; otherwise we print k and increment it.

Condition
=

Centered pyramid

Empty cells on the left push the printed numbers to the center.

2

Variation — Browser (fixed-width <div> cells)

In the browser, using fixed-width inline blocks keeps multi-digit numbers aligned:

HTML
<!DOCTYPE html>
<html>
<head>
  <style>
    .cell {
      display: inline-block;
      width: 28px;
      text-align: center;
    }
  </style>
</head>
<body>
<script>
var i, j, k = 1;
for (i = 1; i <= 5; i++) {
  for (j = 5; j >= 1; j--) {
    if (j > i)
      document.write("<span class='cell'></span>");
    else
      document.write("<span class='cell'>" + (k++) + "</span>");
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Increase size to print more rows (numbers will continue beyond 15)
  • Pad numbers (e.g., padStart(3)) for larger sizes
  • Restart numbering each row by resetting k inside the outer loop
  • Center-align using DOM instead of document.write in real projects

Avoid

  • Using variable-width spacing when numbers reach 2+ digits (alignment breaks)
  • Forgetting line breaks after each row
  • Relying on document.write for production code

Key Takeaways

1

A single counter k creates a continuous sequence across rows.

2

Printing empty cells first helps center-align the pyramid.

3

Fixed-width formatting is essential once values become multi-digit.

4

Total numbers printed are 1+2+…+n = n(n+1)/2.

❓ Frequently Asked Questions

With size 5, total printed numbers are 1+2+3+4+5 = 15, so the counter ends at 15.
For single-digit values, yes. Once values reach 10+, you need padding or fixed-width cells to keep the pyramid aligned.
Initialize k to your start value, for example let k = 10;.
Use fixed-width padding for each cell (like padStart) and print consistent empty-cell spacing before the numbers.

Explore More JavaScript Number Patterns!

Try mixing centering + counters to build Floyd triangles and number pyramids.

All Number Patterns →
Did you know?

Floyd-style patterns use one counter across rows. That means the last number is the triangular number n(n+1)/2 for n rows.

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