JS: String.prototype.repeat

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

String.prototype.repeat(count)

repeat a string.

"a".repeat(2) === "aa"

The function can also work on Value Types othe than string. It will first convert this (binding) to string, then return count copy of it, joined together.

/* digit 9 to string, then repeat 2 times */
console.log( Reflect.apply( String.prototype.repeat , 9, [2] ) === "99" );

〔see JS: Reflect (namespace)

JS String.prototype