lumian lumian - 6 months ago 14x
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"?


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));