Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

repeat

String.prototype.repeat() returns a new string containing the receiver repeated a specified number of times. The count is converted to an integer and invalid or excessive counts throw RangeError.

How it works

String.prototype.repeat() returns a new string made by concatenating the receiver a specified number of times. The count is converted to an integer; zero produces an empty string, while invalid or overly large counts throw a RangeError.

Syntax

repeat(count)

Parameters

NameTypeDescription
countnumberAn integer between 0 and Infinity indicating how many times to repeat the string.

Returns

A new string containing the specified number of copies of the given string.

Key points

  • It creates and returns a separate string rather than modifying the original string.
  • Each copy is joined directly after the previous one, with no separator inserted.
  • The count argument controls how many copies are produced and may be zero.
  • The return value is always a string representing the repeated content.
  • The method can be borrowed when the receiver can be converted to a string.

Common mistakes

  • A negative count, Infinity, or a result too large for the implementation's maximum string length causes a RangeError.
  • Fractional counts are truncated through integer conversion, so 3.5 repeats the string three times.

Example: Create a repeated separator

const separator = "-";
const result = separator.repeat(5);
console.log(result); // "-----"

Creates a five-character separator by concatenating five copies of the same string.

When to use it

Formatting text

Generate a visual divider or indentation by repeating a character or short string.

Constructing strings

Create a repeated token sequence when building output or test fixtures.

Aligning output

Produce padding based on a calculated width.

Interview questions

QWhat does String.prototype.repeat() return?

It returns a new string containing the receiver repeated count times. A count of zero returns an empty string.

Follow-up: What happens with a fractional count? It is converted to an integer, so 3.5 behaves like 3.

QWhen does repeat() throw?

The count must be non-negative and finite, and the resulting string must fit within the maximum string length. Otherwise, the method throws RangeError.

Follow-up: Does Infinity mean unlimited repetition? No. Infinity is invalid and results in RangeError.

QCan repeat() be called on a non-String object?

repeat() is generic: it can be borrowed by an object whose this value can be converted to a string. For example, an object with toString() returning "abc" can use String.prototype.repeat.call(object, 2).

Follow-up: What must a borrowed receiver provide? It must be coercible to a string; the method does not require a String instance.

  • String
  • String.prototype.toString
  • String.prototype.concat

Source: String.prototype.repeat() - JavaScript | MDN. Checked 2026-10-15.