Monday, September 18, 2023
HomeProgrammingRepeating Strings in JavaScript

Repeating Strings in JavaScript


String manipulation is a elementary a part of coding, like duplicating or repeating strings/characters – a process that will appear trivial, however essential for sure situations, whether or not it is formatting output to show to a person or saving sure sorts of knowledge to a file. On this Byte, we’ll see what it takes to repeat strings and methods to do it in JavaScript.

The Want for Repeating Strings

Think about you are constructing a command line device that should generate a dynamically-sized border round a component, like a desk. The border can be made up of a string of characters that have to be repeated based mostly on the dimensions of the ingredient. Or, take into account a state of affairs the place it is advisable create a string of a selected size for testing functions.

String repetition turns into a useful device in circumstances like these. It is a easy operation, however figuring out methods to do it the “proper” means could make your code a lot less complicated and extra idiomatic.

The right way to Repeat a String in JavaScript

JavaScript has a few methods to repeat a string. The 2 commonest strategies are utilizing the built-in repeat() technique and utilizing a for loop. Let’s discover these strategies in additional element.

Utilizing the repeat() Technique

Launched in ES6, the repeat() technique is a built-in JavaScript technique that merely repeats the string a specified variety of instances.

This is how you should utilize it:

let str = "StackAbuse ";

This may output:

StackAbuse StackAbuse StackAbuse 

The repeat() technique is an extremely simple technique to repeat a string. All it is advisable do is name the tactic on the string you wish to repeat, and go within the variety of repetitions as an argument.

Word: The repeat() technique doesn’t change the unique string. It returns a brand new string with the repeated content material.

The repeat() technique is a good device when it is advisable repeat a string a selected variety of instances. It is simple to make use of, extremely readable, and a part of the JavaScript normal – making it a dependable alternative for string repetition.

Utilizing a For Loop

Whereas the repeat() technique is essentially the most simple technique to repeat a string in JavaScript, there’s one other technique that does not depend on a bespoke technique – the nice outdated for loop. This technique to do it is a little more handbook, however it might additionally mean you can customise the repetition a bit greater than the repeate() technique, like when you wanted to conditionally repeat a string or characters.

This is a easy instance:

let textual content = "Stackabuse ";
let repeatedText = "";

for (let i = 0; i < 5; i++){
    repeatedText += textual content;

console.log(repeatedText); // "Stackabuse Stackabuse Stackabuse Stackabuse Stackabuse "

Within the above code, we’re creating an empty string repeatedText after which appending the textual content to it 5 instances within the for loop. The result’s the string “Stackabuse” repeated 5 instances.

This technique is usually a little bit extra cumbersome than utilizing repeat(), but it surely’s good to know that it exists, particularly when you’re working in a JS atmosphere that does not assist repeat().

Evaluating the Strategies

Now that we have seen two other ways to repeat a string, you is perhaps questioning which one you need to use. The reply? It relies upon. Let’s check out a few of the components you would possibly wish to take into account.


In the case of efficiency, the repeat() technique is quicker than the for loop. It’s because the repeat() technique is a built-in JavaScript operate and is optimized for efficiency.

Nonetheless, the distinction in efficiency is negligible except you are coping with very giant strings or repeating a string an enormous variety of instances.

"Stackabuse ".repeat(1000000);
console.timeEnd('repeat'); // repeat: 0.179ms

console.time('for loop');
let str = "";
for (let i = 0; i < 1000000; i++){
    str += "Stackabuse ";
console.timeEnd('for loop'); // for loop: 94.756ms

Within the above code, we’re timing how lengthy it takes to repeat a string a million instances. As you may see, repeat() is considerably sooner, coming in at over 500x sooner! However bear in mind, except you are working with equally giant numbers, you are unlikely to note a distinction.


By way of readability, the repeat() technique wins arms down. It is clear, concise, and tells you precisely what it is doing. The for loop, alternatively, requires a bit extra cognitive load to know what’s taking place.

// Clear and concise
let str1 = "Stackabuse".repeat(5);

// Requires a bit extra pondering
let str2 = "";
for (let i = 0; i < 5; i++) {
    str2 += "Stackabuse";

As Donald Knuth as soon as stated, “Applications are supposed to be learn by people and solely by the way for computer systems to execute.” So, when unsure, go for readability over minor efficiency features.

Browser Compatibility

After all, when you’re working with a cross-platform library or frontend code, you actually need to think about browser compatibility.

The repeat() technique is a contemporary addition to JavaScript, launched in ES6. And fortuitously, it is supported by most trendy browsers, together with Chrome, Firefox, Safari, and Edge. Nonetheless, when you’re creating for an viewers that could be utilizing older browsers—like Web Explorer—you would possibly run into some compatibility points as IE does not assist the repeat() technique.

Word: All the time test the Can I exploit web site to confirm the compatibility of JavaScript strategies with totally different browsers. It will prevent complications down the street when you by accident use a way that is not broadly supported

Alternatively, utilizing a for loop to repeat strings is a extra conventional method and is supported throughout all browsers, together with older ones like IE, so it is a bit safer.


On this Byte, we have explored methods to repeat strings in JavaScript utilizing the repeat() technique and a for loop. We additionally examined the velocity of every technique, the browser compatibility, and famous that whereas the repeat() technique is a contemporary addition to the language and isn’t supported in older browsers, a for loop affords wider compatibility.



Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments