Border Number Square (Hollow 5×5) in JavaScript

What You’ll Learn
How to print a 5×5 square where only the border is filled with numbers (center cells are blank).
This pattern is great practice for combining multiple conditions with row/column indices.
⭐ Pattern Output
The pattern looks like this:
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9Complete JavaScript Program
We detect which border we are on (top, right, bottom, left). If a cell is not on the border, we print blank spacing.
const n = 5;
const cellWidth = 2; // pad to 2 characters
let top = 1;
let right = n + 1; // 6
let bottom = n * 2 + 3; // 13
let left = n * 3 + 1; // 16
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j <= n; j++) {
let v = null;
if (i === 1) v = top++; // top row
else if (j === n) v = right++; // right column
else if (i === n) v = bottom--; // bottom row (reverse)
else if (j === 1) v = left--; // left column (reverse)
if (v === null) line += " ".repeat(cellWidth + 2); // inner blank space
else line += String(v).padStart(cellWidth, " ") + " ";
}
console.log(line.trimEnd());
}🧠 How It Works
Create four counters
We maintain counters for top row, right column, bottom row (reverse), and left column (reverse).
Scan the grid
Nested loops visit every cell \((i, j)\) in a 5×5 grid.
Border detection
We assign a number only if the cell is on the top row, right column, bottom row, or left column.
Inner cells are blanks
If a cell is not on the border, we print spaces to keep the hollow center.
Border-only square
The border is numbered while the center stays empty.
Variation — Browser (aligned cells) Version
In the browser, use inline-block cells for alignment (and blanks for center cells):
<!DOCTYPE html>
<html>
<body>
<script>
var n = 5;
var top = 1;
var right = n + 1;
var bottom = n * 2 + 3;
var left = n * 3 + 1;
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= n; j++) {
var v = null;
if (i === 1) v = top++;
else if (j === n) v = right++;
else if (i === n) v = bottom--;
else if (j === 1) v = left--;
if (v === null)
document.write('<span style="display:inline-block;width:3em"> </span>');
else
document.write('<span style="display:inline-block;width:3em;text-align:right">' + v + '</span>');
}
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Change
nto build a larger hollow square - Fill the center with a different character (like
0) - Compute border values by formula instead of using counters
- Add colors for each border segment in browser mode
Avoid
- Printing inner blanks without fixed width (alignment breaks)
- Forgetting that bottom and left borders are printed in reverse
- Mixing 0-based and 1-based indices without adjusting formulas
Key Takeaways
Border-only printing uses simple row/col conditions.
Counters help assign sequential values to different borders.
Fixed-width output is key for a clean hollow center.
This idea generalizes to many border and frame patterns.
❓ Frequently Asked Questions
v is null, print 0 (and pad it) instead of spaces.Explore More JavaScript Number Patterns!
Next, try filling the inside too to build a spiral number square.
Many “border” patterns are the first step toward spiral matrix problems—once you can control borders, you can iterate inward.
12 people found this page helpful
