Repeat String in JavaScript?

To repeat string, you can use Array() along with join().


Following is the code −

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
   String.prototype.counter = function (value) {
      return new Array(value + 1).join(this);
   console.log("The repeat string
".counter(5)); </script> </html>

To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor.


This will produce the following output: on console −