Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

typescript
repeat
string
communitycreator

What is the string.repeat() method in TypeScript?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The repeat() method in TypeScript is used to return a new string a repeated number of times. This method is similar to JavaScript repeat() method. This is because anywhere that JavaScript runs, TypeScript runs too.

Syntax

string.repeat(n)
Syntax for repeat() method in TypeScript

Parameters

string: This is the string we want to return a number of times.

n: This represents how many times we want to return the specified string.

Return value

The value returned is a copy or copies of the number specified an n number of times.

Example

// create strings
let string1:string = "Edpresso"
let string2:string = "Is"
let string3:string = "The"
let string4:string = "Best!"
// print copies of this strings for some number of times
console.log(string1.repeat(4))
console.log(string2.repeat(3))
console.log(string3.repeat(2))
console.log(string4.repeat(1))
Return a string for a repeated number of times.

Explanation

  • Lines 2–5: We create some strings.
  • Lines 8–11: We print the strings some number of times to the console using the repeat() method.

RELATED TAGS

typescript
repeat
string
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring