• 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
  • VBA
  • About
You are here: Home / JavaScript / How to Repeat Character N Times in JavaScript

How to Repeat Character N Times in JavaScript

May 11, 2022 Leave a Comment

In JavaScript, we can repeat a character n times by making use of the JavaScript String repeat() method.

var characterToRepeat = "s";
characterToRepeat = characterToRepeat.repeat(10);

console.log(characterToRepeat);

#Output:
ssssssssss

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 character in strings many times. We can repeat characters in a string 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);

Repeating strings is very similar to repeating characters. Check out how to do that here.

Repeat all characters in a string n times in JavaScript

In the above example, we showed how to repeat one character. Let’s say we have a string called, “string” and we want to repeat all characters n times.

We would just need the help of a for loop along with the repeat() method and the length() and charAt() methods to make this happen.

Here is our code to repeat characters in a string n times:

var theString = "string";
var newString = "";
for (var i=0; i < theString.length; i++){
  var currChar = theString.charAt(i);
  currChar = currChar.repeat(n);
  newString = newString + currChar;
}

So say we wanted to repeat all characters of a string 5 times, we could use our code above to do this.

var theString = "string";
var newString = "";
for (var i=0; i < theString.length; i++){
  var currChar = theString.charAt(i);
  currChar = currChar.repeat(5);
  newString = newString + currChar;
}

console.log(newString);

#Output:
ssssstttttrrrrriiiiinnnnnggggg

Hopefully this article has been helpful for you to learn how to use JavaScript to repeat a character n times.

Other Articles You'll Also Like:

  • 1.  Insert Character Into String In JavaScript
  • 2.  JavaScript value – Get the Value from an Input Field
  • 3.  Using JavaScript to Compare Dates
  • 4.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 5.  Get a List of Prime Numbers Using JavaScript
  • 6.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 7.  Reverse a String in JavaScript
  • 8.  Using JavaScript to Remove the First Character From a String
  • 9.  Create Array of Zeros in JavaScript
  • 10.  Add Hours to a Date Using 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy