mozilla

Revision 647035 of String.prototype.repeat()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/repeat
  • Revision title: String.prototype.repeat()
  • Revision id: 647035
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Grammar fixes, more specific explanation of parameter

Revision Content

{{JSRef("Global_Objects", "String")}} {{harmony}}

Summary

The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Syntax

str.repeat(count)

Parameters

count
An integer between 0 and +∞ : [ 0, +∞), indicating the number of times to repeat the string in the newly-created string that is to be returned.

Throws

{{jsxref("Global_Objects/RangeError", "RangeError")}}
The repeat count must be positive and less than infinity.

Examples

"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)

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-string.prototype.repeat', 'String.prototype.repeat')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatGeckoDesktop("24")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile("24")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

Revision Source

<p>{{JSRef("Global_Objects", "String")}} {{harmony}}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>repeat()</strong></code> method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><var>str</var>.repeat(<var>count</var>)</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>count</code></dt>
 <dd>
  An integer between 0 and +∞ : [ 0, +∞), indicating the number of times to repeat the string in the newly-created string that is to be returned.</dd>
</dl>
<h3 id="Throws">Throws</h3>
<dl>
 <dt>
  {{jsxref("Global_Objects/RangeError", "RangeError")}}</dt>
 <dd>
  The repeat count must be positive and less than infinity.</dd>
</dl>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: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 : () =&gt; "abc", repeat : String.prototype.repeat}).repeat(2)    
// "abcabc" (repeat is a generic method)</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.repeat', 'String.prototype.repeat')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatGeckoDesktop("24")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatGeckoMobile("24")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>&nbsp;</p>
Revert to this revision