String.prototype.repeat()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
Die repeat()
-Methode von String
-Werten erstellt und gibt einen neuen String zurück, der die angegebene Anzahl von Kopien dieses Strings enthält, zusammengefügt zu einem String.
Probieren Sie es aus
const mood = "Happy! ";
console.log(`I feel ${mood.repeat(3)}`);
// Expected output: "I feel Happy! Happy! Happy! "
Syntax
js
repeat(count)
Parameter
Rückgabewert
Ein neuer String, der die angegebene Anzahl von Kopien des gegebenen Strings enthält.
Ausnahmen
RangeError
-
Wird ausgelöst, wenn
count
negativ ist oder wenncount
die maximale Stringlänge überschreitet.
Beispiele
Verwendung von repeat()
js
"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)
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-string.prototype.repeat |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
repeat |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.