Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.repeat — repeating text

The repeat(count) method returns consecutive copies of a string without changing the original. A Symbol or BigInt count causes TypeError during numeric conversion. If conversion and truncation yield a negative integer or either infinity, the method throws RangeError.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

str.repeat(count)

String.prototype.repeat returns a string containing count copies of the text, joined without a separator. The original remains unchanged. It repeats UTF-16 code units rather than separately interpreting visible characters.

The count argument undergoes numeric conversion and truncation toward zero: 2.9 produces two repetitions, while -0.5 produces an empty string. 0, NaN, and an omitted argument also produce an empty string. A Symbol or BigInt value causes TypeError during numeric conversion. If conversion and truncation yield a negative integer, Infinity, or -Infinity, the method throws RangeError; this also applies to empty input text.

The method can be borrowed through String.prototype.repeat.call(value, count): the receiver is converted to text, but null and undefined cause TypeError. For external input, limit the repetition count to avoid requesting an excessively large result.

Examples

const text = "ab";
console.log(text.repeat(3));
console.log(JSON.stringify(text.repeat(0)));
console.log(text.repeat(2.9));
console.log(text);
try {
  text.repeat(-1);
} catch (error) {
  console.log(error.name);
}

Result

ababab
""
abab
ab
RangeError

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources