What is the most efficient way to create an N-times-a-character string in JavaScript? -


this question has answer here:

i have char ('-') , number (80) of times should repeated in string. need string of char repeated given number of times:

------------------------------------------------------------------------------------------------------------- 

i know can result += mychar x n times in loop. don't think efficient. there better way that?

by efficient mean: fastest

well, best come with, after couple minutes playing, is:

string.prototype.repeat = function (len) {     return (new array(len + 1)).join(this); }  console.log('-'.repeat(5)); 

js fiddle demo.

which is, unfortunately, cheapened little @nnnnnn commented correct answer 2 minutes beforehand. sigh...


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -