String.prototype.repeat()

repeat(count)
Returns: string · Added in vES6 · Updated March 13, 2026 · String Methods
string repeat concatenation

The repeat() method constructs and returns a new string by concatenating the original string with itself a specified number of times. The method accepts a single parameter count that specifies how many copies to repeat. This is useful for generating repeated patterns, padding strings, or creating delimiters programmatically.

The method throws a RangeError if the count is negative or results in a string that exceeds the maximum string length. It also handles edge cases like zero (returns empty string) and fractional values (truncates to integer).

Syntax

string.repeat(count)

Parameters

ParameterTypeDefaultDescription
countnumberAn integer between 0 and Infinity indicating how many times to repeat the string. Non-integer values are converted to integers.

Parameter Details

  • If count is 0, an empty string is returned.
  • If count is negative, a RangeError is thrown.
  • If count is a non-integer (like 3.5), it gets truncated to the integer part (3).
  • If count is Infinity, a RangeError is thrown because the resulting string would be too long.
  • The method works on any object with a toString() method due to its generic nature.

Examples

Basic Repetition

const word = "ha";
console.log(word.repeat(3));    // "hahaha"
console.log(word.repeat(0));    // ""
console.log(word.repeat(1));    // "ha"

Calling repeat(3) on “ha” produces “hahaha” — three copies concatenated together. Passing 0 gives you an empty string, which is handy when you need a default value.

Creating Visual Elements

const delimiter = "-".repeat(10);
console.log(delimiter); // "----------"

const indent = "  ".repeat(4);
console.log(indent + "Indented text");
// "    Indented text"

const stars = "*".repeat(5);
console.log(stars); // "*****"

A common use case is generating visual separators or formatting output. This approach is cleaner than using loops for simple tasks.

Working with Dynamic Values

const mood = "Happy! ";
console.log(`I feel ${mood.repeat(3)}`);
// "I feel Happy! Happy! Happy! "

const base = "abc";
const count = 2;
console.log(base.repeat(count)); // "abcabc"

// Fractional values get truncated
console.log("abc".repeat(3.7));  // "abcabcabc"

The method works well with variables and template literals. Non-integer counts are truncated toward zero, so 3.7 becomes 3.

Handling Edge Cases

// Using with objects that have toString()
const obj = {
  toString() { return "obj"; }
};
console.log(Stringrepeat.call(obj, 2)); // "objsbj"

console.log("test".repeat(Infinity)); // RangeError: Invalid string length
console.log("test".repeat(-1));       // RangeError: Invalid count

The method is generic and can be used with any object that implements toString(). Always handle the negative count and Infinity cases with try-catch if the count comes from user input.

Common Patterns

Creating a text-based loading indicator:

function loadingDots(n) {
  return ".".repeat(n);
}

console.log(loadingDots(3)); // "..."
console.log(loadingDots(0)); // ""

Generating aligned columns:

function padRight(str, length) {
  return str + " ".repeat(Math.max(0, length - str.length));
}

console.log(padRight("Name", 10) + "John");  // "Name      John"
console.log(padRight("Age", 10) + "25");     // "Age       25"

Building repeated patterns:

const row = (cell) => `| ${cell} |`.repeat(3);
console.log(row("x")); // "| x | | x | | x |"

Creating test data:

const buffer = "x".repeat(1024); // Create 1KB string

Next Steps

Now that you understand repeat(), explore other string methods for building and manipulating strings. If you need to combine multiple strings into one, concat() is the traditional approach, though template literals often work better for simple cases.

For aligning text within a specific width, padStart() and padEnd() are purpose-built for that task. If you need to extract portions of a string rather than repeat them, slice() works identically to its array counterpart.

See Also