String.prototype.repeat

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App

概要

現在の文字列を引数に指定した回数だけ繰り返した、新しい文字列を返します。

String のメソッド
実装されたバージョン JavaScript 1.8
ECMAScript エディション ECMAScript 6st Edition

構文

newString = string.repeat(count)
newString
新しい文字列
count
0 以上、正の無限数未満の整数 : [ 0, +∞).

例

"abc".repeat(0);      // ""
"abc".repeat(1);      // "abc"
"abc".repeat(2);      // "abcabc"

"abc".repeat(3.5);    // "abcabcabc", 少数は丸められ、整数の結果が返る

"abc".repeat(-1);     // RangeError: repeat count must be positive and less than inifinity
"abc".repeat(1/0);    // RangeError: repeat count must be positive and less than inifinity

({toString : () => "abc", repeat : String.prototype.repeat}).repeat(2)    //"abcabc" が返る (repeat は一般メソッド)

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート ? 24 ? ? ?
機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート ? ? 24 ? ? ?

タグ (2)