Centered Hollow Number Pyramid in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Indentation + inner gap

What You’ll Learn

How to print a centered hollow pyramid where row i prints i on the left and right edges:

1, 2 2, 3 3, 4 4, 5 5.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
    1
   2 2
  3   3
 4     4
5       5
1

Complete JavaScript Program

We print leading spaces for centering, then print i. For rows > 1, we print an inner gap and then print i again.

JavaScript
const n = 5;

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

  // 1 leading space per step (matches the reference output style)
  line += " ".repeat(n - i);

  if (i === 1) {
    line += "1";
  } else {
    const innerSpaces = 2 * i - 3;
    line += i + " ".repeat(innerSpaces) + i;
  }

  console.log(line);
}

🧠 How It Works

1

Set n

n = 5 controls pyramid height.

Setup
2

Leading spaces for centering

" ".repeat(n - i) shifts rows right so the pyramid is centered.

Indent
3

Row 1 prints only one digit

Row 1 has no inner gap, so we print just 1.

Edge case
4

Rows 2..n print two digits

We print i, then 2*i - 3 spaces, then i again.

Gap
=

Hollow pyramid

Only the edges print numbers; everything inside is spaces.

2

Variation — Browser (document.write) Version

In HTML, use &nbsp; so spaces don’t collapse:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var n = 5;
for (var i = 1; i <= n; i++) {
  for (var s = 1; s <= n - i; s++) document.write("&nbsp;");
  if (i === 1) {
    document.write("1");
  } else {
    document.write(i);
    for (var gap = 1; gap <= 2 * i - 3; gap++) document.write("&nbsp;");
    document.write(i);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Increase n to print a taller pyramid
  • Replace i with * to create a hollow star pyramid
  • Use fixed-width padding for multi-digit i values
  • Add a mirrored bottom half to form a hollow diamond

Avoid

  • Using normal spaces in HTML (alignment will break)
  • Printing the second digit for row 1 (it duplicates the same position)
  • Forgetting that the gap grows by 2 each row

Key Takeaways

1

Row 1 prints one digit; other rows print two.

2

Inner gap for row i is 2*i - 3 spaces.

3

Leading spaces are n - i for centering.

4

Use &nbsp; for browser-aligned output.

❓ Frequently Asked Questions

Because \(2*2 - 3 = 1\). That creates exactly one space between the two 2s on row 2.
Yes—just change the gap string (e.g., use " " blocks) and adjust the formula accordingly.
Print the same pyramid from n-1 down to 1 after the top half.
Replace String(i) with "*" in both edge print positions.

Explore More JavaScript Number Patterns!

Hollow patterns are great for practicing spacing rules. Next, try building a hollow diamond using the same inner-gap idea.

All Number Patterns →
Did you know?

The inner gap grows by 2 because each new row adds one space on the left and one on the right of the “hollow” area.

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