The repeat()
method of String
values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.
The repeat()
method of String
values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.
repeat(count)
A new string containing the specified number of copies of the given string.
RangeError
Thrown if count
is negative or if count
overflows maximum string length.
"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)
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
repeat |
41 | 12 | 24 | 28 | 9 | 36 | 24 | 28 | 9 | 3.0 | 41 | 1.0 | 4.0.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat