JS: String.prototype.repeat

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

String.prototype.repeat(count)

repeat a string.

console.assert("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.assert(Reflect.apply(String.prototype.repeat, 9, [2]) === "99");

〔see JS: Reflect (namespace)

JS String.prototype