JavaScript String repeat() Method



The JavaScript String repeat() method returns a string that contains the specified number of copies of the current string, and concatenates them all together. It accepts an integer parameter named 'count', whose value must be in between 0 and +infinity.

It throws a 'RangeError' exception if the count parameter values are negative or exceed the maximum string length.

Syntax

Following is the syntax of JavaScript String repeat() method −

repeat(count)

Parameters

This method accept a parameter named 'count', which is described below −

  • count − Indicating the number of times to repeat the string.

Return value

This method returns a new string containing the specified number of copies of this string.

Example 1

If we pass the count parameter value as 0, it returns an empty string.

In the following program, we use the JavaScript String repeat() method to retrieve a new string containing the specified number (count = 0) of copies of this string "Tutorials Point".

<html>
<head>
<title>JavaScript String repeat() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   let count = 0;
   document.write("String value: ", str);
   document.write("<br>Count value: ", count);
   document.write("<br>New string value: ", str.repeat(count));
</script>    
</body>
</html>

Output

The above program returns an empty string ''.

String value: Tutorials Point
Count value: 0
New string value:

Example 2

If we pass count parameter values as 3, it returns a new string containing a number of specified copies of this string.

Here, is another example of the JavaScript Stirng repeat() method. We use this method to retrieve a new string containing the number(count = 3) of specified copies of the string "Hello World".

<html>
<head>
<title>JavaScript String repeat() Method</title>
</head>
<body>
<script>
   const str = "Hello World ";
   let count = 3;
   document.write("String value: ", str);
   document.write("<br>Count value: ", count);
   document.write("<br>New string value: ", str.repeat(count));
</script>    
</body>
</html>

Output

After executing the above program, it returns a new string "Hello World " repetition of 3 as −

String value: Hello World
Count value: 3
New string value: Hello World Hello World Hello World

Example 3

The String repeat() method throws a 'RangeError' exception if the count parameter value is negative or exceeds the maximum string length.

<html>
<head>
<title>JavaScript String repeat() Method</title>
</head>
<body>
<script>
   const str = "Learn JavaScript";
   let count1 = 1/0;
   document.write("String value: ", str);
   document.write("<br>Count1 value: ", count1);
   try {
      document.write("New string value: ", str.repeat(count1));
   } catch (error) {
      document.write("<br>", error);
   }

   let count2 = -2;
   document.write("<br>Count2 value: ", count2);
   try {
      document.write("<br>New string value: ", str.repeat(count2));
   } catch (error) {
      document.write("<br>", error);
   }
</script>    
</body>
</html>

Output

The above program throws a 'RangeError' exception.

String value: Learn JavaScript
Count1 value: Infinity
RangeError: Invalid count value: Infinity
Count2 value: -2
RangeError: Invalid count value: -2
Advertisements