String.prototype.repeat()
Baseline Widely availableThis feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
>The repeat() method of String values constructs and returns a new string
which contains the specified number of copies of this string, concatenated together.
Try it
const mood = "Happy! ";
console.log(`I feel ${mood.repeat(3)}`);
// Expected output: "I feel Happy! Happy! Happy! "
Syntax
jsrepeat(count)
Parameters
Return value
A new string containing the specified number of copies of the given string.
Exceptions
RangeError-
Thrown if
countis negative or ifcountoverflows maximum string length.
Examples
>Using repeat()
js"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (count will be converted to integer)
"abc".repeat(1 / 0); // RangeError
({ toString: () => "abc", repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)
Specifications
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-string.prototype.repeat > |
Browser compatibility
See also
- Polyfill of
String.prototype.repeatincore-js - es-shims polyfill of
String.prototype.repeat String.prototype.concat()
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Oct 26, 2026 by MDN contributors.
View this page on GitHub • Report a problem with this content