Open In App

How to Repeat a String in JavaScript ?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Using the repeat() method

This method is used to create a new string by repeating an existing string a specified number of times using the repeat() method. If a user provides the count to be 0 or negative, then an empty string is returned.

Syntax:

str.repeat(count);

// Here str is your string
// And count is how many times you want to repeat the string

Example: This example shows the implementation of the above-explained approach.

JavaScript
const str = "Gfg ";
const repeatedStr = str.repeat(3);
console.log(repeatedStr);

Output
Gfg Gfg Gfg 

Using for Loop

At first define a function “repeatString” and inside a funcrtion define a for loop which repeats a given string a specified number of times.

Syntax:

  for (i = 0; i < Number_of_time ; i++) {
ans += Your_String;
}

Example: Below example shows the implementation of the above-explained approach.

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

console.log(repeatString('hello gfg ', 3));

Output
hello gfg hello gfg hello gfg 

Using the Array constructor and join() method

First define a string which you want to repeat. Then use the Array constructor to create an array with a specified length, where each element will represent a repetition of the original string. After that use the fill() method to fill each element in the array with the original string. Then use the join() method to concatenate all the elements of the array into a single string. At last display the string.

Example: This example shows the implementation of the above-explained approach.

JavaScript
const str = "javascript ";
const repeatedStr = new Array(3).fill(str).join('');
console.log(repeatedStr); 

Output
javascript javascript javascript 

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
JavaScript String repeat() Method
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 inf
2 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
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
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
CSS border-image-repeat Property
The border-image-repeat property in CSS is used to scale and tiling the border images. It can be used to match the middle part of the border image to the size of the border. It can have either one or two values. One is for the horizontal and one for the vertical axis. Only one value is given then it applies to all sides, but two values are given it
2 min read
How to hide or show one record from an ng-repeat within a table based on ng-click?
The way to hide or show a particular record in a table is similar to what is used to hide or show any elements of the DOM. The basic and the first thing that comes to mind is the ng-show and ng-hide directives which show or hides respectively based on binary results of the expressions bound to them. Another way can be the use of ng-if which works l
3 min read