Decreasing Alphabet Rows in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Shorter prefixes

What You'll Learn

The first row prints A through E. Each next row lowers the last letter: ABCD, then ABC, until only A remains. The outer loop controls that shrinking cap; the inner loop always walks from A forward.

Total letters printed is \(5 + 4 + 3 + 2 + 1 = 15 = n(n+1)/2\) for five rows.

⭐ Pattern Output

Five rows from E down to A as the row cap:

Output
ABCDE
ABCD
ABC
AB
A
1

Node.js / console version

Outer loop decreases i from end to start; inner loop runs j from start to i:

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

for (let i = end; i >= start; i--) {
  let line = "";
  for (let j = start; j <= i; j++) {
    line += String.fromCharCode(j);
  }
  console.log(line);
}
2

Browser version (document.write)

Same logic with ASCII codes 6569. Save as .html or use the live editor:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 69; i >= 65; i--) {
  for (let j = 65; j <= i; j++) {
    document.write(String.fromCharCode(j));
  }
  document.write("<br>");
}
</script>
</body>
</html>

🧠 How It Works

1

Outer loop: shrink the row cap

for (let i = end; i >= start; i--) sets how far each row extends: first E, then D, …, finally A.

Descending i
2

Inner loop: always from A

for (let j = start; j <= i; j++) prints the forward alphabet from A through the current i.

Ascending j
3

Line break

After each row, console.log(line) or document.write("<br>").

New row
=

Inverted Program 1

Same character total as the growing triangle. Complexity O(n²) for n rows from A to E.

💡 Tips for Enhancement

Try These

  • Compare with inverted star triangle (same outer idea: wide row first)
  • Rebuild each row with String.fromCharCode inside Array.from({ length })
  • Revisit Program 1 side by side with this page

Avoid

  • Incrementing the outer loop by mistake (you need i--)
  • document.write after the document has loaded

Key Takeaways

1

Outer loop goes E → A; inner loop goes A → i.

2

Each row is a prefix of ABCDE.

3

Total letters: \(n(n+1)/2\).

4

Mirrors Program 1’s row lengths in reverse order.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

The rows appear in opposite order: Program 1 ends with ABCDE; Program 5 starts with it. The multiset of printed characters is the same.
That would change the pattern (see Programs 2–4). Here we always anchor at A on the left.
O(n²) for n rows between A and E.

Next: JavaScript Alphabet Pattern 6

Continue to the next program in the alphabet pattern series.

Program 6 →
Did you know?

If you listed Program 1’s rows from bottom to top, you would read the same lines as Program 5 for the same letter range.

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