Skip to content

ProtoString protoRepeat() Method

Seth Clydesdale edited this page Jun 1, 2015 · 6 revisions

<< Back to Polyfill Methods

Examples

Repeating a string 6 times.

'ha'.protoRepeat(6); // returns 'hahahahahaha'

Repeating a string with a custom separator.

'Ho'.protoRepeat(3, '-'); // returns 'Ho-Ho-Ho'
'coffee'.protoRepeat(3, ' '); // returns 'coffee coffee coffee'

Description

The protoRepeat method can be used as a polyfiller for the native repeat() method. It repeats the string a defined number of times, and if specified, a text string may be passed along to serve as a separator between the repeated texts. An error will be thrown If the repeat count is equal to Infinity, or the repeated string length is greater than 1 << 28.

Syntax

String.protoRepeat(count, separator);

Parameters

Parameter Description
count A number which specifies the amount of times the string will be repeated. It will return the unrepeated texts if left undefined or a negative number is used.
separator A string that serves as a separator between each instance of the repeated texts. No separator will be used if left undefined.
Clone this wiki locally