Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create string of n characters without loop [duplicate]

In Python I can use:

s = "x" * 10
xxxxxxxxxx

How in JavaScript, do I create a string n characters long without a loop?

like image 789
Mr Mystery Guest Avatar asked Jun 18 '26 04:06

Mr Mystery Guest


2 Answers

use String.repeat

"x".repeat(10);

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

On old browsers you could do

new Array(10).join('x')
like image 66
Peter Avatar answered Jun 20 '26 18:06

Peter


Use repeat method.

'x'.repeat(10);
like image 22
COAX_Software Avatar answered Jun 20 '26 18:06

COAX_Software



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!