How to return a new string with a specified number of copies of an existing string with JavaScript?


In this tutorial, we will explore the methods by which we can repeat a string a certain number of times, again and again, to make a new string in JavaScript. There are many scenarios where we may want to make a new string which is just another string repeated ‘x’ number of times and we will see how to achieve that easily using inbuilt methods provided by JavaScript.

We want to create a new string which is just another string copied some number of times. Although we can accomplish this task manually by using a for loop or a while loop, JavaScript provides many functions by which we can accomplish the same task very easily. This is precisely what we will discuss in this article along with a manual method that we can create.

JavaScript strings have an inbuilt method called repeat(). As the name of the method suggests, it helps us to repeat a string a certain specified number of times, concatenates all these repeated strings together, and returns it as a new string. You will notice that this is exactly what we were trying to achieve in this article. Hence, by using this function we can generate a new string which is made by repeating the input string a certain number of times.

Example

Let us first take a look at the manual method and then we can move on to the repeat() method in the JavaScript.

<!DOCTYPE html> <html> <body> <script> var st = "Today is Monday."; var times = 5; var repeatedSt = ""; while(times > 0){ repeatedSt += st; times-- ; } document.write("String is: " + st ); document.write("<br>Repeated String is: " + repeatedSt); </script> </body> </html>

In the above code, we manually repeat the String st Integer times(which is 5) using a while loop. We add String st in String repeatedSt at every iteration while times is greater than 0 and we decrease times after every iteration by 1.

Let us now take a look at the syntax and parameters of the repeat() method in JavaScript.

Syntax and Parameters

The syntax of the repeat() function is −

s.repeat(count);

Here s is the string which is to be repeated a certain number of times. Let us look at the count parameter in detail −

  • count − This refers to the number of times the string will be repeated and concatenated. Count is of the data type Integer. This means that in the resultant string, will be repeated count number of times. Naturally, this is a required parameter. Also, this parameter can only contain positive integer values, as we cannot repeat a string a negative number of times. Additionally, the resulting string should be within the range of a string in JavaScript, else this method throws an error.

This function gives another string with repeated count number of times and concatenated together one after the other. For example,

"aabc".repeat(3);

The resulting string will be "aabcaabcaabc" which will be returned by this method.

Example 1: Without the global modifier ‘g’.

Let us take a look at how we can achieve this through the code.

<!DOCTYPE html> <html> <body> <script> var st = "Today is Monday."; var repeatedSt = st.repeat(5); document.write("String is: " + st ); document.write("<br>Repeated String is: " + repeatedSt); </script> </body> </html>

In the above code, we used the repeat function of JavaScript to repeat the string five times back to back. This code is also shorter and much cleaner than the above manual code.

Conclusion

In this tutorial, we saw how we can repeat a string a certain number of times in JavaScript. This can be achieved by writing a code snippet manually and also by using the inbuilt string.repeat() function in JavaScript which takes in an integer and repeats the string that number of times.

Updated on: 07-Nov-2022

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements