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)

引数

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

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
repeat

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報