String.prototype.repeat
Repeats a string a given number of times.
Available since: ES2015
Syntax
str.repeat(count)Parameters
| Parameter | Type | Description |
|---|---|---|
| count | number | How many times to repeat. Must not be negative. |
Return value
stringA new string repeated `count` times.
Examples
Repeating a fragment:
console.log('ab'.repeat(3));Output:
abababA separator line:
console.log('-'.repeat(20));Output:
--------------------Notes
When count is zero, an empty string is returned. A negative value throws an error.
See also
- String.prototype.padStart — Pads a string on the left to a given length.
- String.prototype.padEnd — Pads a string on the right to the desired length.
Related recipes
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.