Hollow Diamond in Square Star Pattern in JavaScript

Intermediate
⏱️ 14 min read
📚 Updated: Aug 2025
🎯 1 Code Example
width = 2*rows

What You'll Learn

This program prints a square made of stars with a hollow diamond shape inside it (as empty spaces).

For rows = 5, the height is 2*rows - 1 and width is 2*rows.

⭐ Pattern Output

When you run the program with rows = 5:

Output
**********
****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****
**********
1

Complete JavaScript Program

Fixed rows = 5 version:

JavaScript
const rows = 5;
const height = 2 * rows - 1;
const width = 2 * rows;

for (let line = 1; line <= height; line++) {
  if (line === 1 || line === height) {
    console.log("*".repeat(width));
    continue;
  }

  const i = line <= rows ? line : (2 * rows - line);
  const leftStars = rows - i + 1;
  const gap = 2 * (i - 1);

  const out = "*".repeat(leftStars) + " ".repeat(gap) + "*".repeat(leftStars);
  console.log(out);
}

🧠 How It Works

1

Grid size

const height = 2 * rows - 1; counts lines. const width = 2 * rows; is the length of the solid top and bottom bars. The frame is intentionally even width, unlike the standalone hollow diamond (Program 9).

Dimensions
2

Top and bottom frame rows

for (let line = 1; line <= height; line++) walks every row. If line === 1 or line === height, console.log("*".repeat(width)); prints the full border, then continue; skips the middle logic.

Frame
3

Middle rows: mirror index and string build

const i = line <= rows ? line : (2 * rows - line); maps lines from top and bottom to the same pattern. const leftStars = rows - i + 1; and const gap = 2 * (i - 1);. const out = "*".repeat(leftStars) + " ".repeat(gap) + "*".repeat(leftStars); prints the hollow diamond cavity between two star columns.

Interior
4

Finish each line

console.log(out) prints interior rows. O(n²) output for n = rows, O(1) extra space besides short strings. Lines of length width scroll horizontally in the green preview on phones.

Output
=

Diamond inside a box

Solid bars cap a hollow diamond carved from paired star runs. Matches rows = 5 sample output (first lines shown).

💡 Tips for Enhancement

Try These

  • Change rows to print a larger or smaller pattern
  • Replace * with # or @
  • Print the diamond as stars and the background as spaces (invert the look)
  • Try printing a hollow diamond (Program 9) without the box
  • Try a filled diamond (Program 10) and compare

Avoid

  • Forgetting that width is 2*rows (not 2*rows - 1)
  • Printing the top/bottom rows using the same inner-row formula
  • Using tabs for alignment (rendering differs)
  • Mixing &nbsp; with normal spaces
  • Not mirroring the i value for the lower half

Key Takeaways

1

Height is 2*rows - 1 and width is 2*rows.

2

Top and bottom lines are fully filled with stars.

3

Middle lines print two star blocks with a space gap between.

4

The inner space gap is 2*(i - 1) after mirroring.

5

Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.

❓ Frequently Asked Questions

They form the square border.
Because each middle line prints two star blocks of size rows - i + 1 on both sides.
It’s O(n²) for n rows, since output size is \(\Theta(n^2)\).

More Star Patterns

Browse the full JavaScript star pattern series for more variations.

All Programs →
Did you know?

This pattern mixes two ideas: a boxed border and a mirrored diamond interior, which is a common technique in ASCII art.

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.

9 people found this page helpful