How to repeat a string multiple times in JavaScript

Repeating strings multiple times is useful for creating patterns, generating padding, building visual separators, and implementing features like progress indicators or formatted output in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented string repetition in components like loading animations, table formatting, progress bars, and text decorations where repeated patterns enhance visual presentation and functionality. From my extensive expertise, the most modern and efficient solution is using the ES6 repeat() method, which provides clean syntax for string duplication. This approach is straightforward, performant, and specifically designed for creating repeated string patterns.

Use the repeat() method to duplicate a string a specified number of times.

const pattern = '*'
const repeated = pattern.repeat(5)
// Result: '*****'

The repeat() method creates a new string by concatenating the original string the specified number of times. In this example, '*'.repeat(5) produces ‘*****’ by repeating the asterisk character 5 times. The method accepts a non-negative integer and returns an empty string if the count is 0. If you pass a decimal number, it will be truncated to an integer. The method throws a RangeError if the count is negative or if the result would exceed JavaScript’s maximum string length.

Best Practice Note:

This is the same approach we use in CoreUI components for creating visual separators, padding strings, and generating repeated UI patterns across our component library. For older browser support, use a loop or Array.join: new Array(count + 1).join(string). Be cautious with large repeat counts as they can create very long strings and impact memory. The repeat() method is supported in all modern browsers and provides the cleanest syntax for string repetition operations.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author