Computer >> Computer tutorials >  >> Programming >> Javascript

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


Use the JavaScript repeat() method to return a new string with a specified number of copies of an existing string.

The following is the parameter −

  • Count − Mention how many times you want to repeat the string

Example

You can try to run the following to repeat a string and display the result in a new string −

<!DOCTYPE html>
<html>
   <body>
      <script>
         var a = "Welcome!";
         document.write(a.repeat(3));
      </script>
  </body>
</html>