0

With numbers you can multiply a number a certain number of times using the following code:

var y = 10;
var x = y * 3; // 30

Is there a way to do the same with strings with native methods?

var y = "test ";
var x = y * 3; // test test test

Or if not is there a succinct way to do this in a single line?

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

2 Answers2

2

You could do:

var y = "test ";
y = y.repeat(3);

And this would give you "test test test "

String.repeat()

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
Zachary McGee
  • 504
  • 1
  • 4
  • 16
1

What would be "string"*3 in Python, can be done with "string".repeat(3) in Javascript. That appears to be what you want.

Brilliand
  • 13,404
  • 6
  • 46
  • 58