Mastering JavaScript Pattern Programming: Hollow Square and Complex Star Patterns
Published on June 9, 2025 • 5 min read

Introduction: Understanding Hollow Square and Complex Pattern Programs in JavaScript
If you're diving into JavaScript loops and want to master the art of pattern programs, you're in the right place. Pattern programming helps beginners visualize loops and understand control flow more intuitively. In this post, we’ll explore how to create a hollow square pattern, a fundamental example showing conditional logic inside loops, and then advance to complex star patterns with multiple lines and diagonals. By dissecting these step-by-step using JavaScript’s loop and conditional statements, you’ll enhance your programming skills and logical thinking—essential for coding interviews and real-world applications.
Creating a Hollow Square Pattern Using Loops and Conditional Statements
A hollow square pattern looks like a square border made of stars with spaces inside. To achieve this, you must know where to place stars and where to place spaces inside nested loops.
Key Concepts:
- Use two nested loops: one for rows (
i
) and one for columns (j
). - Use conditional checks to print stars only when the position is on the border (first or last row or column).
- For other positions, print spaces.
Example Logic:
let n = 5;
for(let i = 1; i <= n; i++) {
let row = "";
for(let j = 1; j <= n; j++) {
if(i === 1 || i === n || j === 1 || j === n) {
row += "*";
} else {
row += " ";
}
}
console.log(row);
}
This code prints stars on the edges (row 1, row n, column 1, column n) and spaces inside, forming a hollow square. You can increase the value of n
to create larger squares. For more about loops and conditional statements, see the MDN loops guide.
Handling Dynamic Size Patterns with Variables
When you change the size n
, hardcoding conditions like i === 5
will fail if n
is not 5. Instead, always use dynamic conditions:
- Replace fixed numbers (e.g., 5) with
n
to match the current row or column limits. - Understand zero-based vs one-based counting: Start loops from either 0 or 1, but adjust conditions accordingly.
Example:
let n = 8; // size changes dynamically
for(let i = 1; i <= n; i++) {
let row = "";
for(let j = 1; j <= n; j++) {
if(i === 1 || i === n || j === 1 || j === n) {
row += "*";
} else {
row += " ";
}
}
console.log(row);
}
This will reliably print a hollow square regardless of n
.
For zero-based looping:
for(let i = 0; i < n; i++) {
for(let j = 0; j < n; j++) {
// conditions adjusted to i === 0, i === n-1, etc.
}
}
Learn more about loop counts and indexing in the JavaScript control flow guide.
Building Complex Patterns: Diagonals and Center Lines
With mastering the hollow square, you can expand to complex star patterns with multiple lines such as two diagonals and center lines.
Understanding the Pattern:
- Outer borders still have stars.
- Center row and column generate additional lines.
- Two diagonals run from top-left to bottom-right (
i === j
) and top-right to bottom-left (i + j === n - 1
).
Implementing Floor Division in JavaScript:
Since JavaScript’s /
operator returns a floating value, use Math.floor()
to get the integer division result:
const center = Math.floor(n / 2);
Full Conditional Logic Example:
let n = 11;
for(let i = 0; i < n; i++) {
let row = "";
let center = Math.floor(n / 2);
for(let j = 0; j < n; j++) {
if(i === 0 || i === n - 1 || j === 0 || j === n -1 ||
j === center || i === center ||
i === j ||
i + j === n - 1) {
row += "*";
} else {
row += " ";
}
}
console.log(row);
}
This prints a square with star borders, a central cross, and two diagonals. You can experiment by changing n
to see different sizes.
For more in-depth understanding, explore JavaScript Math Object on MDN.
Tips for Pattern Programming and Logic Building
- Trace values: Always note down row (
i
) and column (j
) values when analyzing patterns. - Break complex patterns into conditions: Identify lines, borders, and diagonals separately.
- Use mathematical relations: Diagonal conditions typically involve equality or sum comparisons like
i===j
ori+j === n-1
. - Remember zero vs one indexing: Your loops indexing affects conditions and output shape.
Solving such pattern problems not only improves your understanding of loops and if-else logic but also strengthens your problem-solving skills, crucial for coding interviews. For beginners interested in JavaScript fundamentals, check out the official JavaScript Guide.
Conclusion
Pattern programming in JavaScript is an excellent exercise for beginners to grasp loops, conditionals, and logical reasoning. Starting with simple hollow squares and building up to intricate star patterns involving multiple diagonals and center lines demonstrates how effective conditional logic guides output. Remember to write adaptable code by using variables like n
and functions like Math.floor()
to handle dynamic input sizes. Practice such problems regularly to build strong programming logic, setting a firm foundation for more advanced JavaScript topics and coding interviews. Ready to apply these concepts? Try creating your own patterns or explore other topics like functions and arrays next!
Happy coding!
This blog post is based on content from a YouTube video. Watch it here. All rights reserved by the original creator.