Reverse-to-A Alphabet Rows in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Ascending / descending mix

What You'll Learn

The outer loop walks A through E. On each row, the inner loop prints from that letter backward to A, so the first row is just A, then BA, then CBA, and so on.

Total letters printed is \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\) for n rows.

⭐ Pattern Output

For five rows (AE):

Output
A
BA
CBA
DCBA
EDCBA
1

Node.js / console version

Outer loop increases i; inner loop decreases j from i to start:

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

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

Browser version (document.write)

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

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

🧠 How It Works

1

Outer loop: grow the peak letter

for (let i = start; i <= end; i++) picks the highest letter on the row: A, then B, …, up to E.

Ascending i
2

Inner loop: walk down to A

for (let j = i; j >= start; j--) prints i, then i-1, …, until A.

Descending j
3

Line break

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

New row
=

Reverse runs

Same row lengths as Program 1, but each row reads right-to-left in alphabetical order. Complexity O(n²) for n rows.

💡 Tips for Enhancement

Try These

Avoid

  • Using j++ by mistake in the inner loop (you need j--)
  • document.write after the document has loaded

Key Takeaways

1

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

2

Each row ends with A; the row length equals the row index offset from A plus one.

3

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

4

This is a descending-letter variant of the right triangle family.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

That would be Program 1’s pattern (AB, ABC, …). Here the goal is explicitly BA, CBA, … by decrementing j.
When i === start, the inner loop runs only once at j === 65, which is A.
O(n²) for n rows, same as other triangle alphabet patterns on this site.

Next: JavaScript Alphabet Pattern 5

Continue to the next program in the alphabet pattern series.

Program 5 →
Did you know?

If you reverse the string of each row from Program 1, you obtain the rows of this pattern 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