Symmetric Number Pattern Centered at 0 in JavaScript

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Ascend + 0 + descend

What You’ll Learn

How to print a symmetric number pattern centered around 0 in JavaScript. Each row prints an increasing sequence up to 9, prints 0, then prints a decreasing sequence back.

This pattern is a great exercise for combining two inner loops that build the left and right halves.

⭐ Pattern Output

For this example, the pattern looks like this:

Output
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
1

Complete JavaScript Program

The outer loop decreases the row start from 10 to 1. The left half prints i..9, then we print 0, then the right half prints 9..i.

JavaScript
for (let i = 10; i >= 1; i--) {
  let line = "";

  for (let j = i; j < 10; j++) {
    line += j;
  }

  line += "0";

  for (let k = 9; k >= i; k--) {
    line += k;
  }

  console.log(line);
}

🧠 How It Works

1

Outer loop chooses the row start

for (let i = 10; i >= 1; i--) controls how wide each row becomes as i gets smaller.

Row control
2

Left half prints i..9

for (let j = i; j < 10; j++) appends the increasing part. When i = 8, it prints 89.

Left side
3

Center value is always 0

line += "0"; inserts the center digit to keep the pattern symmetric.

Center
4

Right half prints 9..i

for (let k = 9; k >= i; k--) mirrors the left half back down to the row start.

Right side
=

Row becomes symmetric

The left and right sides mirror around 0, forming the symmetric sequence.

2

Variation — Browser (document.write) Version

Print the pattern directly into an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j, k;
for (i = 10; i >= 1; i--) {
  for (j = i; j < 10; j++)
    document.write(j);
  document.write("0");
  for (k = 9; k >= i; k--)
    document.write(k);
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Make the max digit configurable (instead of hard-coding 9 and 10)
  • Add spaces between digits when printing multi-digit values
  • Change the center character from 0 to another symbol (like *)
  • Skip the first row if you don’t want a single 0 line

Avoid

  • Changing one loop bound without updating the others (breaks symmetry)
  • Using j <= 10 (prints an extra 10 in the left half)
  • Forgetting the line break in browser output

Key Takeaways

1

Two inner loops can build a symmetric row: left side up, right side down.

2

Printing a fixed center value (here 0) helps create symmetry.

3

The row start (i) controls the width of each line.

4

Careful loop bounds (< 10, >= i) prevent off-by-one errors.

❓ Frequently Asked Questions

When i = 9, the left loop prints only 9, then we print 0, then the right loop prints 9 again, giving 909.
It ensures the largest digit printed on the left side is 9. Using j <= 10 would accidentally print 10.
Yes. Append " " after each digit and trim at the end, especially if you plan to print multi-digit values.
It prints 10 rows because the outer loop runs from i = 10 down to 1.

Explore More JavaScript Number Patterns!

Symmetric patterns like this teach you how to mirror sequences and avoid off-by-one issues.

All Number Patterns →
Did you know?

Mirroring patterns often come from two loops that run in opposite directions. Adding a single center value (like 0) makes it easy to keep both halves balanced.

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