JavaScript repeat() to Repeat a String and Limit the Result
Use text.repeat(count) to concatenate copies of a JavaScript string. 'abc'.repeat(2) produces 'abcabc'. A count of 0 returns an empty string.
Repeat a string and add separators when needed
The original value stays the same. repeat() inserts no separator between copies. To put a separator only between copies, build an array and join it with Array(3).fill('ab').join('-').
const text = 'abc';
console.log(text.repeat(0)); // ''
console.log(text.repeat(2)); // 'abcabc'
console.log(text); // 'abc'
console.log('-'.repeat(5)); // '-----'
console.log(Array(3).fill('ab').join('-')); // 'ab-ab-ab'
Understand count conversion and RangeError
The method first converts the count to an integer or infinity. Fractions are truncated toward zero, so 2.9 becomes 2 and -0.8 is treated as 0. A converted count below zero or equal to positive infinity raises RangeError. ECMAScript · String.prototype.repeat()
console.log('ab'.repeat(2.9)); // 'abab'
console.log('ab'.repeat(-0.8)); // ''
console.log('ab'.repeat()); // ''
console.log('ab'.repeat(NaN)); // ''
for (const count of [-1, Infinity]) {
try {
'ab'.repeat(count);
} catch (error) {
console.log(error.name); // 'RangeError'
}
}
Omitting the count or passing NaN produces an empty string. If an input field requires an integer, handle blank input and check the allowed range yourself. Even a valid count can fail if the result exceeds the runtime’s supported string length.
Limit the count and output in a complete example
Save the code as string-repeat.html in UTF-8 and open it in a browser. The example rejects blank, fractional, negative, and over-20 counts. Before allocating the result, it also checks that word.value.length * times is at most 1,000. Both limits are choices for this demo.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>String repetition demo</title>
</head>
<body>
<label for="word">String to repeat</label>
<input id="word" value="abc">
<label for="count">Repeat count (0–20)</label>
<input id="count" type="number" min="0" max="20" step="1" value="2">
<button id="run" type="button">Repeat</button>
<pre id="result" aria-live="polite"></pre>
<script>
const word = document.querySelector('#word');
const count = document.querySelector('#count');
const result = document.querySelector('#result');
function update() {
const times = Number(count.value);
if (count.value.trim() === '' || !Number.isInteger(times) || times < 0 || times > 20) {
result.textContent = "Enter an integer from 0 to 20.";
return;
}
if (word.value.length * times > 1000) {
result.textContent = "The result must be at most 1,000 UTF-16 code units.";
return;
}
const repeated = word.value.repeat(times);
result.textContent = `Result: ${JSON.stringify(repeated)}\n` +
`String length: ${repeated.length}`;
}
document.querySelector('#run').addEventListener('click', update);
update();
</script>
</body>
</html>
With the defaults abc and 2, the demo shows abcabc and length 6. Enter 0 to see an empty string of length 0. Clear the count to see the validation message. Repeat 🙂 twice to see two emojis and length 4 because length counts UTF-16 code units.
The output limit applies independently of the count. Repeating 51 ASCII letters 20 times would require 1,020 code units, so the demo shows a message before creating that result.




