Centered Square Number Pyramid in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Odd row widths + squares

What You’ll Learn

How to print a centered pyramid of square numbers, where values increment as 1², 2², 3² and so on.

You’ll also see how odd row widths (1, 3, 5, 7, 9) make centering straightforward.

⭐ Pattern Output

For this example, the pattern looks like this:

Output
                1
            4   9   16
        25  36  49  64  81
    100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete JavaScript Program

We print indentation first, then print count squares. A counter m increases each time so the squares continue across rows.

JavaScript
let m = 1;

for (let count = 1; count <= 9; count += 2) {
  let line = "";

  for (let s = count; s < 9; s++) {
    line += "    ";
  }

  for (let k = 1; k <= count; k++) {
    line += String(m * m).padStart(3, " ") + " ";
    m++;
  }

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

🧠 How It Works

1

Initialize m

m = 1 is the current number whose square we print.

Counter
2

Outer loop increases width by 2

count = 1, 3, 5, 7, 9 controls how many squares appear on each row.

Width
3

Indentation loop centers the row

Printing spaces while s < 9 creates left padding that shrinks as count grows.

Center
4

Print squares and increment m

Each printed cell uses m*m, then m++ advances to the next square.

Squares
=

Centered row of squares

Numbers are squares of consecutive integers, laid out in a centered pyramid.

2

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

In the browser, fixed-width cells help keep 3-digit squares aligned:

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

💡 Tips for Enhancement

Try These

  • Change the maximum width (9) to print a larger pyramid
  • Print cubes instead of squares (use m*m*m)
  • Add color styling to highlight perfect squares visually
  • Use DOM elements instead of document.write for modern pages

Avoid

  • Using variable-width spacing for 3-digit numbers (alignment breaks)
  • Resetting m accidentally inside the loop (sequence restarts)
  • Forgetting the line break after each row in browser mode

Key Takeaways

1

Squares are generated by printing m*m and incrementing m.

2

Odd row widths (1, 3, 5, ...) keep the pyramid symmetric.

3

Indentation is the key to centering.

4

Fixed-width cells help when values become 2-3 digits.

❓ Frequently Asked Questions

The last printed square is 25² = 625, because the pyramid prints 1+3+5+7+9 = 25 values.
Yes. Initialize m to a different start value (for example, m = 3 starts at 9).
For widths 1, 3, 5, 7, 9, total values are 1+3+5+7+9 = 25.
Yes. Remove the indentation loop and just print the square values per row.

Explore More JavaScript Number Patterns!

Square-number patterns combine simple math with loops—great practice for both.

All Number Patterns →
Did you know?

1+3+5+...+(2n-1) = n². That’s why the pyramid prints 25 values and ends at 25² = 625.

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