String.prototype.repeat()

repeat() メソッドは、呼び出し元の文字列を指定した数だけコピーして結合した新しい文字列を構築して返します。

試してみましょう

構文

js
repeat(count)

引数

count

0 から正の無限大までの間の整数で、文字列を繰り返す数を示します。

返値

与えられた文字列の指定した回数分のコピーを含む新しい文字列です。

例外

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 Language Specification
# sec-string.prototype.repeat

ブラウザーの互換性

BCD tables only load in the browser

関連情報