Open In App

TypeScript String repeat() Method

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax

string.repeat(count: number): string;

Parameters:

  • count: No. of times you want to repeat the string.

Return Value:

A new string consisting of the original string repeated count times.

Example 1: To demonstrate creating a string by repetition of a string using repeat() method.

Javascript




const geek: String = "GeeksForGeeks";
const repeatedGeek: String =
    geek.repeat(3);
console.log(repeatedGeek);


Output:

GeeksForGeeksGeeksForGeeksGeeksForGeeks

Example 2: To demonsrating creating a String by repetition of a string.

Javascript




const numString: String = "12345";
const repeatedNumString: String =
    numString.repeat(2);
console.log(repeatedNumString);


Output:

1234512345

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads