String.prototype.repeat()
Returns a new string containing the given number of copies of the string.
Syntax
str.repeat(count) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
count | number | Yes | The number of times to repeat the string. Must be non-negative. |
Returns
string — A new string with the original repeated count times.
Throws
RangeError— count is negative or infinite.
Examples
console.log('ab'.repeat(3));
Output
ababab
console.log('x'.repeat(0));
Notes
A count of 0 returns an empty string. Negative or Infinity counts throw a
RangeError. The count is truncated to an integer. Does not mutate the original.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 41 |
| firefox | 24 |
| safari | 9 |
| edge | 12 |
| node | 4.0 |