Categories
JavaScript Answers

How to create a string of variable length, filled with a repeated character with JavaScript?

Spread the love

To create a string of variable length, filled with a repeated character with JavaScript, we call the array join method.

For instance, we write

const str = new Array(len + 1).join(character);

to create an array with length len + 1 with Array.

And then we call join with character to return a string filled with character of length len.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *