2

In Python I can use:

s = "x" * 10
xxxxxxxxxx

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

0

3 Answers 3

14

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent answer, sadly it doesn't work with ExtendScript (ESTK 3.8), which is what I'm using :(
Please note that you need to do Array(len + 1).join(chr) in order to get the desired number of characters. (Join adds a character between each item in the array, so it will add n-1 characters).
1

Use repeat method.

'x'.repeat(10);

Comments

0
new Array(11).join('x')

For 10 x you need 11 in new Array so that 10th x is visible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.