How to repeat a string in JavaScript?


In this article we are going to learn about how to repeat a string in JavaScript. We can find the three different ways to repeat a string in JavaScript they are listed below.

  • using a while loop

  • using recursion

  • using ES6 repeat() method

Let’s dive into the article to learn more about how to repeat a string in JavaScript.

While loop method

A while loop in JavaScript is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.

Syntax

Following is the syntax for while loop

while (condition)
   statement

With a condition that is assessed before to each iteration of the loop. The statement is carried out if the condition is true. If the condition is false, any statement after the while loop is executed.

Example

In the following example we are running a while loop to repeat string

<!DOCTYPE html>
<html>
   <script>
      function repeatStringNumTimes(string, times) {
         var repeatedString = "";
         while (times > 0) {
            repeatedString += string;
            times--;
         }
         return repeatedString;
      }
      document.write(repeatStringNumTimes("Varma ", 5));
   </script>
</html>

When the script gets executed, it will display an output consisting of a string repeated five times on the webpage. As we mentioned, the number of repetitions is 5.

Recursion method

Recursion is an approach to iterating over an action where a function repeatedly calls itself until it yields a result. Recursion must have a few essential components in order to function correctly.

The first is a base case, which is a statement that ends the recursion and is typically contained within a conditional clause like if.

The second case involves a recursive function, which is called on itself in the statement.

Example

Considering the following example where we are using the recursion method to repeat the string.

<!DOCTYPE html>
<html>
   <script>
      function repeatStringNumTimes(string, times) {
         if(times < 0)
            return "";
         if(times === 1)
            return string;
         else
            return string + repeatStringNumTimes(string, times - 1);
      }
      document.write(repeatStringNumTimes("TP", 3));
   </script>
</html>

On running the above script, the web-browser displays the string that is repeated three times, because the event gets triggered when the user runs the script.

ES6 repeat() method

You'll employ the String.prototype.repeat() technique, for this solution

The repeat() method creates and returns a new string that is composed of the specified number of concatenated copies of the string on which it was called.

Example

Let’s look into the example where we are using ES6 repeat() method to repeat the string.

<!DOCTYPE html>
<html>
   <script>
      function repeatStringNumTimes(string, times) {
         if (times > 0)
            return string.repeat(times);
         else
            return "";
      }
      document.write(repeatStringNumTimes("FOX", 3));
   </script>
</html>

When the script gets executed, it will generate an output consisting of a string repeated three times as the event gets triggered; when the user runs the script.

We can also find the another way for repeating a string in JavaScript. Let’s have a look into it.

fill()method

This method initially takes a number and allocates those many numbers of spaces. It inserts the provided string in all those places and joins them to get a repeated string.

Syntax

Following is the syntax for fill() method

Array(number).fill(string).join('');

Example

In the following example, Initially, an array is created with 3 slots and the provided string is kept in all those slots and later on, using join() method.

<html>
<body>
   <script>
      const str = 'Tutorialspoint '
      var d = Array(3).fill(str).join('')
      document.write(d, "<br>");
   </script>
</body>
</html>

On the running the above script the event gets triggered and displays a string repeated for three times on the webpage.

Updated on: 18-Jan-2023

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements