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.

JavaScript
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

Similar Reads

Using the repeat() method

This method is used to create a new string by repeating an existing string a specified number of times using the repeat() method. If a user provides the count to be 0 or negative, then an empty string is returned....

Using for Loop

At first define a function “repeatString” and inside a funcrtion define a for loop which repeats a given string a specified number of times....

Using the Array constructor and join() method

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....

Contact Us