Repeating string for specific number of times using JavaScript

JavaScript provides several methods to repeat a string for a specific number of times. This functionality is useful for generating patterns, creating padding, or building repeated text sequences in web applications.

Problem Statement

Write a JavaScript function that takes a string and a positive integer as input and returns a new string that repeats the input string for the specified number of times. If the input string is empty or the specified number of times is zero, the function should return an empty string.

Sample Input:

String: "Hello"
Number of Times: 3

Sample Output:

"HelloHelloHello"

Using the repeat() Method (Recommended)

The simplest and most efficient way is using JavaScript's built-in repeat() method:

function repeatString(str, times) {
    return str.repeat(times);
}

console.log(repeatString('abc', 3));
console.log(repeatString('Hello', 2));
console.log(repeatString('X', 5));
abcabcabc
HelloHello
XXXXX

Using a For Loop

For browsers that don't support repeat() or when you need custom logic:

function repeatString(str, times) {
    let result = '';
    for (let i = 0; i < times; i++) {
        result += str;
    }
    return result;
}

console.log(repeatString('abc', 3));
console.log(repeatString('Hi', 4));
abcabcabc
HiHiHiHi

Using Array and join() Method

This method creates an array of the desired length and joins the elements:

function repeatString(str, times) {
    return Array(times).fill(str).join('');
}

console.log(repeatString('abc', 3));
console.log(repeatString('*', 6));
abcabcabc
******

Using Recursion

A recursive approach that calls itself until the base case is reached:

function repeatString(str, times) {
    if (times === 0) {
        return '';
    }
    return str + repeatString(str, times - 1);
}

console.log(repeatString('abc', 3));
console.log(repeatString('Go', 2));
abcabcabc
GoGo

Using Array.from() and map()

Creates an array using Array.from() and maps each element to the string:

function repeatString(str, times) {
    return Array.from({ length: times }, () => str).join('');
}

console.log(repeatString('abc', 3));
console.log(repeatString('Test', 2));
abcabcabc
TestTest

Performance Comparison

Method Performance Browser Support Readability
repeat() Fastest ES6+ Excellent
For loop Good All browsers Good
Array methods Slower All browsers Good
Recursion Slowest All browsers Fair

Handling Edge Cases

Here's a robust function that handles edge cases:

function repeatString(str, times) {
    // Handle edge cases
    if (typeof str !== 'string' || times < 0 || !Number.isInteger(times)) {
        return '';
    }
    
    return str.repeat(times);
}

console.log(repeatString('abc', 3));    // Normal case
console.log(repeatString('', 5));       // Empty string
console.log(repeatString('test', 0));   // Zero times
console.log(repeatString('hi', -1));    // Negative number
abcabcabc

test

Conclusion

The repeat() method is the most efficient and readable solution for repeating strings in modern JavaScript. For older browser support, use a simple for loop or Array-based approaches.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements