Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. String
  6. repeat()

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.

Therepeat() method ofString values constructs and returns a new stringwhich contains the specified number of copies of this string, concatenated together.

Try it

const mood = "Happy! ";console.log(`I feel ${mood.repeat(3)}`);// Expected output: "I feel Happy! Happy! Happy! "

Syntax

js
repeat(count)

Parameters

count

An integer between0 andInfinity, indicating the number of times to repeat the string.

Return value

A new string containing the specified number of copies of the given string.

Exceptions

RangeError

Thrown ifcount is negative or ifcount overflows maximum string length.

Examples

Using repeat()

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: () => "abc", repeat: String.prototype.repeat }).repeat(2);// 'abcabc' (repeat() is a generic method)

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.repeat

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp