Alphabet Diamond (A to E)

Beginner
⏱️ 11 min read
📚 Updated: Aug 2025
🎯 2 Code Examples

⭐ Pattern Output

Output
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A
1

Node.js / console version

JavaScript
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);

function printRow(i) {
  let line = "";
  for (let j = top; j >= A; j--) line += (i === j ? String.fromCharCode(j) : " ");
  for (let k = A + 1; k <= top; k++) line += (i === k ? String.fromCharCode(k) : " ");
  console.log(line);
}

for (let i = A; i <= top; i++) printRow(i);
for (let i = top - 1; i >= A; i--) printRow(i);
2

Browser version (document.write)

Reference-style output uses &nbsp; for spacing.

HTML
<!DOCTYPE html>
<html>
<body>
<script>
  const A = 65;
  const last = 69;

  function printRow(i) {
    for (let j = last; j >= A; j--) {
      if (i === j) document.write(String.fromCharCode(j));
      else document.write("&nbsp;&nbsp;");
    }
    for (let k = A + 1; k <= last; k++) {
      if (i === k) document.write(String.fromCharCode(k));
      else document.write("&nbsp;&nbsp;");
    }
    document.write("<br>");
  }

  for (let i = A; i <= last; i++) printRow(i);
  for (let i = last - 1; i >= A; i--) printRow(i);
</script>
</body>
</html>

🧠 How It Works

1

Core idea

This is Program 33 (top half) plus a mirrored bottom half. First part prints rows for i = A..E; second part prints rows for i = D..A. Each row is built with two scans so the letter appears at two diagonal positions, forming a diamond outline.

Logic
2

Loops + condition

An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.

Nested loops
=

Connect it to the output

Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.

❓ Frequently Asked Questions

The first part builds the diamond from A up to E; the second part mirrors it back down to A.
At i = E, the diagonals are farthest apart, creating the widest part of the diamond outline.
HTML collapses spaces, so use &nbsp; or fixed-width cells to preserve alignment.

More JavaScript alphabet patterns

Browse the full list for more variations.

All alphabet patterns →

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.

10 people found this page helpful