UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ String/ String.prototype.repeat()

The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.

Syntax

repeat(count)

Parameters

Return value

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

Exceptions

Examples

Using repeat()

"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

Browser compatibility

See also