Course contentsShow
← JavaScript
Lesson 176 of 2,7235. StringsFree lesson

Repeating Strings with repeat

Create new strings by repeating the original a specified number of times using the repeat method.

Repeating Strings with repeat

You'll learn how to create a new string by duplicating an original string multiple times using a built-in method.

What is the repeat Method?

The repeat() method takes a string and repeats it a specified number of times, returning a brand new string. Think of it like a copy-paste machine: you give it a word and tell it "make 5 copies of this," and it hands you back a longer string with that word repeated 5 times in a row.

How It Works

The syntax is straightforward:

string.repeat(count)

The count parameter tells JavaScript how many times to repeat the string. Here are some examples:

let word = "ha";
let laugh = word.repeat(3);
console.log(laugh); // "hahaha"

let dashes = "-".repeat(10);
console.log(dashes); // "----------"

let greeting = "Hello! ".repeat(2);
console.log(greeting); // "Hello! Hello! "

Important Details

  • The count must be a non-negative integer (0, 1, 2, 3, etc.)
  • Using repeat(0) returns an empty string ""
  • Using repeat(1) returns the original string unchanged
  • The original string is never modified—repeat() always returns a new string
let star = "★";
let rating = star.repeat(5);
console.log(rating); // "★★★★★"
console.log(star);   // "★" (original unchanged)

Key Takeaway: Use repeat() to quickly generate a new string by duplicating an original string a specific number of times—perfect for creating patterns, separators, or visual elements.