lumian lumian - 1 year ago 52
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));