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

HTMLProgramming ScriptsJavascript

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 −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var a = "Welcome!";
         document.write(a.repeat(3));
      </script>
  </body>
</html>
raja
Published on 26-Feb-2018 11:09:50
Advertisements