JavaScript Number Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
line + console.log

Definition and Usage

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

+= converts the number to a string and concatenates. Single digits sit next to each other ("123"). Once a value reaches 10, append a space or pad with padStart so 10 and 11 do not become "1011".

The loop variable is the number.

Append col for 1, 2, 3… Append row to repeat the same digit. A separate let value that you increment is how running sequences cross from one row to the next.

JavaScript Syntax

Count the outer loop down when the first row should be the longest. The inner loop still walks 1 through the current width.

JS
for (let row = rows; row >= 1; row--) {
  let line = "";
  for (let n = 1; n <= row; n++)
    line += n;           // digits, no space
  console.log(line);     // end the row
}

How JavaScript Prints a Number

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

PieceJavaScriptWhat it does
Digitline += nCoerces the number to a string and appends it
Same digitline += rowRepeats the row number on that line
Next valuevalue++Keeps counting across columns and rows
New rowconsole.log(line)Write the row and finish it
WidthString(n).padStart(2)Two characters, padded on the left
Odd or evenn % 20/1 patterns from the remainder
One digitfromCharCode("0".charCodeAt(0) + n)One character for 0–9

JavaScript Examples

Three programs. Each one changes only the JavaScript that chooses the number or its width.

Example 1 — Shrink the row with the outer loop

Start row at 5 and stop at 1. The inner loop appends 1 through row.

JS
const rows = 5;

for (let row = rows; row >= 1; row--) {
  let line = "";
  for (let n = 1; n <= row; n++)
    line += n;
  console.log(line);
}

How It Works

  1. row-- makes each later line one digit shorter.
  2. The inner for always restarts at 1, so every row begins with 1.
  3. line += n builds the string; console.log ends the row.

Example 2 — Append the row number, not the column

The inner loop only decides how many times to append. The value is row.

JS
const rows = 5;

for (let row = 1; row <= rows; row++) {
  let line = "";
  for (let col = 1; col <= row; col++)
    line += row;
  console.log(line);
}

How It Works

  1. Row 1 runs the inner loop once and appends 1.
  2. Row 3 runs it three times and appends 3 each time.
  3. Swap line += row for line += col and the line becomes 123.

Example 3 — Keep two-digit values aligned

padStart(2) reserves two characters. A separate space separates columns.

JS
let value = 1;

for (let row = 1; row <= 4; row++) {
  let line = "";
  for (let col = 1; col <= row; col++) {
    if (line) line += " ";
    line += String(value).padStart(2);
    value++;
  }
  console.log(line);
}

How It Works

  1. value lives outside both loops, so counting continues on the next row.
  2. padStart(2) turns 1 into " 1" and leaves 10 as two digits.
  3. The space is appended only between values, so the width does not pad that gap.

62 JavaScript Number Pattern Programs

Each card is a full JavaScript program: nested for loops, a number for the cell, and console.log. Open one for the walkthrough.

Number Pattern 1
Number Pattern 2
Number Pattern 3
Number Pattern 4
Number Pattern 5
Number Pattern 6
Number Pattern 7
Number Pattern 8
Number Pattern 9
Number Pattern 10
Number Pattern 11
Number Pattern 12
Number Pattern 13
Number Pattern 14
Number Pattern 15
Number Pattern 16
Number Pattern 17
Number Pattern 18
Number Pattern 19
Number Pattern 20
Number Pattern 21
Number Pattern 22
Number Pattern 23
Number Pattern 24
Number Pattern 25
Number Pattern 26
Number Pattern 27
Number Pattern 28
Number Pattern 29
Number Pattern 30
Number Pattern 31
Number Pattern 32
Number Pattern 33
Number Pattern 34
Number Pattern 35
Number Pattern 36
Number Pattern 37
Number Pattern 38
Number Pattern 39
Number Pattern 40
Number Pattern 41
Number Pattern 42
Number Pattern 43
Number Pattern 44
Number Pattern 45
Number Pattern 46
Number Pattern 47
Number Pattern 48
Number Pattern 49
Number Pattern 50
Number Pattern 51
Number Pattern 52
Number Pattern 53
Number Pattern 54
Number Pattern 55
Number Pattern 56
Number Pattern 57
Number Pattern 58
Number Pattern 59
Number Pattern 60
Number Pattern 61
Number Pattern 62

Frequently Asked Questions

line += 10 converts the number to the string "10" and appends it with no separator. The next += starts immediately, so 10 then 11 becomes "1011". Append a space, or pad with padStart, when values can be more than one digit.
line += n appends the digits to the same string. console.log(line) writes that string and ends the row. Call it once after the inner loop.
String(n).padStart(2) returns a string at least two characters wide, padded on the left with spaces. It only affects that value. Append a separate space after it when you want a gap between columns.
Yes, when the value is 0 through 9. String.fromCharCode("0".charCodeAt(0) + n) is one character. String(n) still works, and it is the conversion you need once the value reaches 10.

Next: Number Pattern 1

A full JavaScript walkthrough of the descending number triangle, from line += through console.log.

Number 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