lumian lumian - 2 years ago 98
Javascript Question

How to make characters in a string repeat using Javascript?

How can I make individual characters within a string repeat a given amount of times?

That is, how do I turn "XyZ" into "XXXyyyZZZ"?

Answer Source

Try this:

var foo = 'bar';

function makeString(str, repeat) {
  var str =, function(character) {
    var nascentStr = '';
    while (nascentStr.length < repeat) {
      nascentStr += character;
    return nascentStr;
  return str;

alert(makeString(foo, 3));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download