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.
repeat()
メソッドは、呼び出し元の文字列を指定した数だけコピーして結合した新しい文字列を構築して返します。
試してみましょう
const mood = "Happy! ";
console.log(`I feel ${mood.repeat(3)}`);
// Expected output: "I feel Happy! Happy! Happy! "
構文
js
repeat(count)
引数
返値
与えられた文字列の指定した回数分のコピーを含む新しい文字列です。
例外
RangeError
-
count
が負の数であるか、count
が文字列の最大長を超えた場合に発生します。
例
repeat() の使用
js
"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (小数点以下は切り捨てられます)
"abc".repeat(1 / 0); // RangeError
({ toString: () => "abc", repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() は汎用メソッドです)
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-string.prototype.repeat |
ブラウザーの互換性
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.