Hollow Square Border of 1s in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Border condition (i/j edges)

What You’ll Learn

How to print a hollow square border using 1 on the edges and spaces inside.

This teaches a common pattern-printing trick: decide output based on whether a cell is on the border.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1
1

Complete JavaScript Program

If the current cell is on the first/last row or first/last column, print 1. Otherwise print spacing.

JavaScript
const size = 5;

for (let i = 1; i <= size; i++) {
  let line = "";
  for (let j = 1; j <= size; j++) {
    if (i === 1 || i === size || j === 1 || j === size) {
      line += "1 ";
    } else {
      line += "  ";
    }
  }
  console.log(line.trimEnd());
}

🧠 How It Works

1

Set the grid size

size = 5 means we print a 5×5 grid.

Setup
2

Outer loop controls rows

i goes from 1..size and represents the current row.

Rows
3

Inner loop controls columns

j goes from 1..size and represents the current column.

Columns
4

Border condition prints 1

When i or j is on an edge (1 or size), we print 1; otherwise we print spacing.

Condition
=

Hollow inside

Only border cells print 1; all inner cells are blank.

2

Variation — Browser (document.write) Version

Print the hollow square in an HTML page using document.write and &nbsp; for spaces:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j;
for (i = 1; i <= 5; i++) {
  for (j = 1; j <= 5; j++) {
    if (i == 1 || i == 5 || j == 1 || j == 5)
      document.write("1 ");
    else
      document.write("&nbsp;&nbsp; ");
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change size to print a bigger hollow square
  • Replace 1 with another digit or symbol
  • Fill the inside with another value (like 0) instead of spaces
  • Use CSS monospace fonts to keep spacing consistent

Avoid

  • Using normal spaces in HTML (they can collapse); prefer &nbsp;
  • Forgetting the edge condition (you’ll fill the whole square)
  • Mixing inconsistent spacing (output alignment may look off)

Key Takeaways

1

Border patterns are made with simple edge checks on i and j.

2

Only the first/last row and first/last column print 1.

3

Everything else prints spaces to create a hollow interior.

4

The same idea applies to hollow rectangles and frames.

❓ Frequently Asked Questions

Those checks detect the top and bottom border rows. Similarly, j == 1 and j == size detect the left and right borders.
Yes. Use different values for rows and columns and update the border condition accordingly.
In the else branch, print "0 " instead of spaces.
It prints a single 1, which is both the border and the entire square.

Explore More JavaScript Number Patterns!

Border-based patterns are a great way to practice conditional logic inside grids.

All Number Patterns →
Did you know?

Hollow shapes are usually easier than they look: you only need to identify the boundary cells. Everything else is “inside” and can be spaces or another fill.

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