JavaScript: Repeat string

I wanted to build a string of 20 repeating characters. This function makes it really easy:

String.prototype.repeat = function(num) {
    return new Array(num + 1).join( this );
}

Now I can write:

var separator = "=".repeat(20);
Leave A Reply - 1 Reply
Replies
Kentscode 2014-01-30 06:25am - laserblast@comcast.net - Logged IP: 63.117.2.18

Thanks you so much for the this function. Not only did I need it, but I learned from it.

All content licensed under the Creative Commons License