• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Repeat a String in JavaScript

How to Repeat a String in JavaScript

May 11, 2022 Leave a Comment

In JavaScript, we can easily repeat a string as many times as we would like. The easiest way to repeat a string n times is to use the JavaScript String repeat() method.

var stringToRepeat = "string";
stringToRepeat = stringToRepeat.repeat(3);

console.log(stringToRepeat);

#Output:
stringstringstring

When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is repeating a string n times. We can repeat strings using the JavaScript String repeat() method.

The repeat() method takes one parameter, an integer, which is the number of times you want to repeat the string.

For example, if we want to repeat a string 3 times, we can simply just give the repeat() method the parameter 3, repeat(3);

Below is an example of how to repeat a string 3 times using JavaScript.

var stringToRepeat = "string";
stringToRepeat = stringToRepeat.repeat(3);

console.log(stringToRepeat);

#Output:
stringstringstring

If we want to repeat characters instead of strings, we can also do that using the repeat() method.

Repeat a String in JavaScript With a Loop

Another way we can repeat a string in JavaScript is by using a while loop.

var theString = "string";
var i = 0;
var repeatedString = "";
while (i < 3){
  repeatedString = repeatedString + theString;
  i++;
}
theString = repeatedString;

console.log(theString);

#Output:
stringstringstring

One of the benefits of repeating a string with a loop over the repeat() method is that we have more control.

So say that we wanted to add a space and comma in between each repeated string. We can do this easily with a loop.

var theString = "string";
var i = 0;
var repeatedString = "";
while (i < 3){
  if( repeatedString != "" ){
    repeatedString = repeatedString + ", " + theString;
  } else {
    repeatedString = theString;
  }
  i++;
}
theString = repeatedString;

console.log(theString);

#Output:
string, string, string

Hopefully this article has been useful for you to learn how to use JavaScript to repeat a string.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if String Contains Only Numbers
  • 2.  Add Hours to a Date Using JavaScript
  • 3.  Using JavaScript to Increment a Variable by 1
  • 4.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 5.  innerHTML vs outerHTML
  • 6.  Create Array of Zeros in JavaScript
  • 7.  Remove Undefined Values From an Array in JavaScript
  • 8.  JavaScript if else Shorthand Statement
  • 9.  Remove Brackets from String Using JavaScript
  • 10.  Insert Character Into String In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x