String.prototype.repeat
Constructs and returns a new string which contains the specified number of copies of the string on which it was called
Syntax
string.repeat(count)Parameters
| Parameter | Type | Description |
|---|---|---|
| count | number | Number of times to repeat the string. Must be non-negative and less than Infinity |
Return Value
A new string containing the specified number of copies
Examples
console.log('abc'.repeat(3)); // 'abcabcabc'const divider = '-'.repeat(40);
console.log(divider); // '----------------------------------------'function indent(level: number) {
return ' '.repeat(level);
}
console.log(indent(3) + 'hello'); // ' hello'Understanding String.prototype.repeat
The String.prototype.repeat method in JavaScript constructs and returns a new string which contains the specified number of copies of the string on which it was called. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.repeat(count). It accepts 1 parameter: count. When called, it returns a new string containing the specified number of copies. Understanding when and how to use repeat() helps you write more expressive, readable code.
Common use cases for String.prototype.repeat include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-padstart, string-padend, string-concat, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.repeat is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.padStartPads the current string from the start with a given string until the resulting string reaches the given length
String.prototype.padEndPads the current string from the end with a given string until the resulting string reaches the given length
String.prototype.concatConcatenates the string arguments to the calling string and returns a new string
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.