JavaScript String.prototype.repeat() Method
The repeat() method returns a new string with the original repeated a given number of times.
Description
The repeat() method builds a new string containing the given number of copies of the original, joined together. A count of 0 gives an empty string.
Non-integer counts are truncated, so repeat(2.9) gives two copies. A count so large that the result would exceed the maximum string length also throws a RangeError.
It's handy for simple text formatting, such as separator lines, indentation and star ratings, and for generating test data of a known size. For padding a string to a fixed length, padStart() and padEnd() work out how many characters are needed for you.
Syntax
string.repeat(count)Parameters
- countHow many times to repeat the string, 0 or more.
Return Value
A new string containing the given number of copies.
Exceptions
- RangeErrorThrown if
countis negative or infinite.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.repeat() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





