JavaScript Star Pattern Programs

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

Definition and Usage

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

console.log("*") adds a newline, so a star logged inside the inner loop lands on its own line. "*".repeat(n) builds a whole run, and console.log prints it once. "*" + "*" is "**". "*" + 1 is the string "*1", because + concatenates when either side is a string.

Build the row, then log it once.

line += "*" stays on the same string. console.log(line) writes that string and starts the next row. A solid row can skip the inner loop and use "*".repeat(row).

JavaScript Syntax

The inner loop runs once per star. Append to line, then log that string after the inner loop.

JS
for (let row = 1; row <= rows; row++) {
  let line = "";
  for (let col = 1; col <= row; col++)
    line += "*";          // same row
  console.log(line);      // end the row
}

How JavaScript Prints a Star

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

PieceJavaScriptWhat it does
Starline += "*"Appends one star to the row string
Run of stars"*".repeat(n)A string of n stars
Indent" ".repeat(n)n spaces
Join"*" + "*"The string "**"
String +"*" + 1The string "*1"
New rowconsole.log(line)Writes the row and ends it
Edge onlyline += (col === 1 || col === width) ? "*" : " "A hollow row

JavaScript Examples

Three programs. Each one changes only the JavaScript that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 appends one star. Each later row appends one more, because the inner loop stops at row.

JS
const rows = 5;

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

How It Works

  1. The outer loop walks row from 1 to 5.
  2. line += "*" builds the row without a newline.
  3. console.log(line) writes that string and starts the next row.

Example 2 — Print the whole row with repeat

Count row down from 5. One console.log writes every star and the newline.

JS
const rows = 5;

for (let row = rows; row >= 1; row--)
  console.log("*".repeat(row));

How It Works

  1. "*".repeat(row) returns the stars as one string.
  2. console.log writes that string and ends the row, so line += is not needed.
  3. row-- makes each later line one star shorter.

Example 3 — Join the spaces and the stars

" ".repeat builds the indent. "*".repeat builds an odd run of stars. + joins the two strings.

JS
const rows = 5;

for (let row = 1; row <= rows; row++) {
  const spaces = " ".repeat(rows - row);
  const stars = "*".repeat(2 * row - 1);
  console.log(spaces + stars);
}

How It Works

  1. Row 1 builds 4 spaces and 1 star. Row 5 builds 0 spaces and 9 stars.
  2. + concatenates the two strings. "*" + 1 would be "*1", not another star.
  3. console.log writes the joined line and ends the row.

11 JavaScript Star Pattern Programs

Each card is a full JavaScript program: nested for loops, line += "*" or "*".repeat, and console.log. Open one for the walkthrough.

Frequently Asked Questions

The + operator concatenates when either side is a string, so "*" + 1 is "*1". Another star is line += "*", or "*".repeat(n).
line += "*" appends the star to the same string. console.log(line) writes that string and ends the row. Call it once after the inner loop. console.log("*") inside the inner loop puts each star on its own line.
Yes, when the row is a solid run of stars. console.log("*".repeat(row)) writes that row and the newline in one call. Use line += when the row mixes spaces and stars, or when only the edges should be stars.
console.log(" ".repeat(rows - row) + "*".repeat(2 * row - 1)) builds the indent and the stars as one string. console.log adds the newline. Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full JavaScript walkthrough of the right-angled star triangle, from line += "*" through console.log.

Star Pattern 1 →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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