How to use the Array constructor and join() method In Javascript
First define a string which you want to repeat. Then use the Array constructor to create an array with a specified length, where each element will represent a repetition of the original string. After that use the fill() method to fill each element in the array with the original string. Then use the join() method to concatenate all the elements of the array into a single string. At last display the string.
Example: This example shows the implementation of the above-explained approach.
const str = "javascript ";
const repeatedStr = new Array(3).fill(str).join('');
console.log(repeatedStr);
Output
javascript javascript javascript
How to Repeat a String in JavaScript ?
The javascript string is an object that represents a sequence of characters. We are going to learn how can we repeat a string using JavaScript.
Below are the methods to repeat a string in JavaScript:
Table of Content
- Using the repeat() method
- Using a Loop
- Using the Array constructor and join() method