JavaScript Alphabet Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
fromCharCode

Definition and Usage

A JavaScript alphabet pattern is a console program that prints letters in rows. The outer for loop chooses the row. The inner for loop appends each letter with line +=, then console.log(line) ends that row.

JavaScript has no char type. "A" is a string, so "A" + col concatenates ("A1", "A2"). Add the codes instead: String.fromCharCode("A".charCodeAt(0) + col) walks A, B, C. Keep the offset inside 0–25 when the shape should stay in A–Z.

Build the row, then log it.

line += letter stays on the same string. console.log(line) runs once after the inner loop and starts the next row.

JavaScript Syntax

The same three pieces show up in every program below: a row loop, a code, and console.log.

JS
const start = "A".charCodeAt(0);



for (let row = 1; row <= 5; row++) {

  let line = "";

  for (let col = 0; col < row; col++)

    line += String.fromCharCode(start + col);  // same row

  console.log(line);                            // end the row

}

How JavaScript Prints a Letter

Map each part of the console shape to a JavaScript value or call.

PieceJavaScriptWhat it does
Letter code"A".charCodeAt(0)Number 65, the code for A
Next letterString.fromCharCode(code)Turns that number back into a one-character string
JumpfromCharCode(start + col)Column 0 is A, column 1 is B
Same lineline += letterNo newline between letters
New rowconsole.log(line)Write the row and finish it
Indent" ".repeat(n)Leading spaces for pyramids
A–Z sliceletters.slice(0, n)Same growing row without a code loop

JavaScript Examples

Three programs. Each one changes only the JavaScript that picks the letter or the space.

Example 1 — Grow a row with fromCharCode

Restart at "A".charCodeAt(0) on every row, then add the column.

JS
const start = "A".charCodeAt(0);



for (let row = 1; row <= 5; row++) {

  let line = "";

  for (let col = 0; col < row; col++) {

    line += String.fromCharCode(start + col);

  }

  console.log(line);

}

How It Works

  1. The outer for sets how many letters this row prints.
  2. start + col is a number; fromCharCode turns it into a letter.
  3. console.log(line) writes the finished string and ends the row.

Example 2 — Add the column to the code, not the string

"A" + col is string concatenation. The letter comes from the numeric code.

JS
const start = "A".charCodeAt(0);

const rows = 4;



for (let row = 0; row < rows; row++) {

  let line = "";

  for (let col = 0; col <= row; col++) {

    line += String.fromCharCode(start + col);

  }

  console.log(line);

}

How It Works

  1. "A" + 1 is the string "A1", not the letter B.
  2. charCodeAt(0) + col is a number, and fromCharCode maps it back to a letter.
  3. Column 0 is A, column 1 is B, and so on.

Example 3 — Indent with " ".repeat

Put spaces on the string before the letters so the triangle sits on the right.

JS
const start = "A".charCodeAt(0);

const rows = 4;



for (let row = 1; row <= rows; row++) {

  let line = " ".repeat(rows - row);

  for (let col = 0; col < row; col++) {

    line += String.fromCharCode(start + col);

  }

  console.log(line);

}

How It Works

  1. " ".repeat(rows - row) builds the leading spaces in one call.
  2. The inner loop appends letters with the same fromCharCode sum.
  3. Fewer spaces and more letters on each later row pull the shape right.

34 JavaScript Alphabet Pattern Programs

Each card is a full JavaScript program: nested for loops, charCodeAt and fromCharCode, and the letters console.log writes. Open one for the walkthrough.

Alphabet Pattern 1
Alphabet Pattern 2
Alphabet Pattern 3
Alphabet Pattern 4
Alphabet Pattern 5
Alphabet Pattern 6
Alphabet Pattern 7
Alphabet Pattern 8
Alphabet Pattern 9
Alphabet Pattern 10
Alphabet Pattern 11
Alphabet Pattern 12
Alphabet Pattern 13
Alphabet Pattern 14
Alphabet Pattern 15
Alphabet Pattern 16
Alphabet Pattern 17
Alphabet Pattern 18
Alphabet Pattern 19
Alphabet Pattern 20
Alphabet Pattern 21
Alphabet Pattern 22
Alphabet Pattern 23
Alphabet Pattern 24
Alphabet Pattern 25
Alphabet Pattern 26
Alphabet Pattern 27
Alphabet Pattern 28
Alphabet Pattern 29
Alphabet Pattern 30
Alphabet Pattern 31
Alphabet Pattern 32
Alphabet Pattern 33
Alphabet Pattern 34

Frequently Asked Questions

JavaScript has no char type. "A" is a string, so "A" + 1 concatenates and becomes "A1". Add the codes instead: String.fromCharCode("A".charCodeAt(0) + col) is the letter.
line += letter appends to the same string, which is how each letter in a row is placed. console.log(line) writes that string and ends the row. Call it once after the inner loop.
fromCharCode continues past Z into other characters. For A–Z demos, keep rows between 1 and 26. Number(input) returns NaN for a bad token; check Number.isInteger before you use the count.
Yes. Store "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and call slice(0, length) for a row that always starts at A. Nested fromCharCode loops are the skill these programs teach; slice is a shortcut once the shape is clear.

Next: Alphabet Pattern 1

A full JavaScript walkthrough of the right-angled letter triangle, from "A".charCodeAt(0) through console.log.

Alphabet Pattern 1 →

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.

28 people found this page helpful