Open In App

JavaScript String repeat() Method

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The repeat() method in JavaScript returns a new string by concatenating the original string a specified number of times.

Syntax:

string.repeat(count);

Parameters:

This method accepts a single parameter.

  • count: count is an integer value that shows the number of times to repeat the given string. The range of the integer “count” is from zero (0) to infinity.

Return values:

It returns a new string that contains the specified number of copies of the string.

JavaScript String repeat() Method Examples

Example 1: Repeating a String Twice Using repeat()

The code repeats the string “forGeeks” two times using the repeat() method, creating a new string “forGeeksforGeeks”. This method returns a new string with the original string repeated the specified number of times.

JavaScript
let str = "forGeeks";
let repeatCount = str.repeat(2);
console.log(repeatCount);

Output
forGeeksforGeeks

Example 2: Repeating a String Five Times Using repeat()

The code repeats the string “gfg” five times using the repeat() method, resulting in the string “gfggfggfggfggfg”. This method returns a new string with the original string repeated the specified number of times.

JavaScript
// Taking a string "gfg"
let str = "gfg";

// Repeating the string multiple times
let repeatCount = str.repeat(5);
console.log(repeatCount);

Output
gfggfggfggfggfg

Example 3: Repeating a String Two Times Using repeat()

The code repeats the string “gfg” two times using the repeat() method. Even though the repeat count is 2.9, it’s converted to 2, resulting in “gfggfg” as the output.

JavaScript
// Taking a string "gfg"
let str = "gfg";

// Repeating the string 2.9 times i.e, 2 times
// because 2.9 converted into 2
let repeatCount = str.repeat(2.9);
console.log(repeatCount);

Output
gfggfg

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers:


Previous Article
Next Article

Similar Reads

How to access index of the parent ng-repeat from child ng-repeat in AngularJS ?
In this article, we will see How to access the index of the parent ng-repeat from child ng-repeat in AngularJS. In AngularJS applications, when we implement the nested ng-repeat directives and if we need to access the index of the parent ng-repeat from the child ng-repeat, then we can do this by using 2 different approaches like by using the $paren
6 min read
How to Repeat a String in JavaScript ?
The javascript string is an object that represents a sequence of characters. We are going to learn how can we repeat a string using JavaScript. Below are the methods to repeat a string in JavaScript: Table of Content Using the repeat() methodUsing a LoopUsing the Array constructor and join() methodUsing the repeat() methodThis method is used to cre
2 min read
TypeScript String repeat() Method
In TypeScript the repeat() method helps in generating a new string by replicating the original string up to a certain number of iterations. It's an inbuilt method of TypeScript as well as JavaScript. Syntaxstring.repeat(count: number): string;Parameters:count: No. of times you want to repeat the string. Return Value:A new string consisting of the o
1 min read
JavaScript RangeError - Repeat count must be less than infinity
This JavaScript exception repeat count must be less than infinity occurs if the passed argument of String.prototype.repeat() method is infinity. Output message: RangeError: argument out of range (Edge) RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox) RangeError: Invalid count value (Chrome) Error Ty
2 min read
JavaScript RangeError - Repeat count must be non-negative
This JavaScript exception repeat count must be non-negative occurs if the argument passed to String.prototype.repeat() method is a negative number. Message: RangeError: argument out of range RangeError: repeat count must be non-negative (Firefox) RangeError: Invalid count value (Chrome) Error Type: RangeError Cause of Error: The repeat() method has
1 min read
How to repeat a string to a specific number of times in PHP ?
A string is a sequence of characters stored in PHP. The string may contain special characters or numerical values or characters. The strings may contain any number of characters and may be formed by the combination of smaller substrings. Approach 1: Using for loop: An empty string variable is declared in order to store the contents of the final str
2 min read
Underscore.js _.repeat() Method
The _.repeat() method takes an integer and a value and creates an array of that size containing the given the value given integer times. Syntax: _.repeat(n, value) Parameters: n: The given integer for the times the value will be in the array.value: The value to be in the array n times.Return Value: This method returns a newly created array. Note: T
2 min read
Lodash _.repeat() Method
Lodash _.repeat() method is used to repeat the given string n times. Syntax:_.repeat(string, n);Parameters: string: This parameter holds the string to repeat.n: n is the number of times to repeat the string.Return Value: This method returns the repeated string. Example 1: In this example, we are repeating the given string the given number of times
1 min read
Angular-JS ng-repeat Directive
Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects. ng-repeat is similar to a loop that we have in C, C++ or other languages but technically it instantiates a template(normally a set of HTML structures) for each e
2 min read
CSS background-repeat Property
The background-repeat property in CSS is used to repeat the background image both horizontally and vertically. It also decides whether the background-image will be repeated or not. Syntax: background-repeat: repeat|repeat-x|round|space|repeat-y|no-repeat|initial|inherit;Default Value: Its default value is initial. Property Values: repeat: This prop
2 min read