Sometimes, we want to create a string with n characters with JavaScript.
In this article, we’ll look at how to create a string with n characters with JavaScript.
How to create a string with n characters with JavaScript?
To create a string with n characters with JavaScript we can use the JavaScript string’s repeat
instance method.
For instance, we write:
const result = "x".repeat(20);
console.log(result)
We call 'x'.repeat
with 20 to return a string that repeats ‘x’ 20 times.
Therefore, result
is 'xxxxxxxxxxxxxxxxxxxx'
.
Conclusion
To create a string with n characters with JavaScript we can use the JavaScript string’s repeat
instance method.