JavaScript: String.prototype.repeat

By Xah Lee. Date: . Last updated: .

New in JS2015.


repeat a string.

console.log("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 JavaScript: Reflect]

JavaScript in Depth