String repeat in JavaScript

String repeat is a function that can be used to multiply a string for a given number of times, its a feature that is closely related to string multiplication operator in Python programming language, as compared to Python's syntax;

("String") * (number of times to repeat)
"A" * 3
// returns "AAA"

Something similar may be archived using string repeat function in JavaScript as;

("String").repeat(number Of times to repeat)

For example to triple "A" you can do as;

"A".repeat(3)

Practical example is with lyrics;

const part = "i feel you, "
const lyrics = part.repeat(4) + " A, B, C"
// "i feel you, i feel you, i feel you, i feel you, A, B, C"

Comments

Popular posts from this blog

How to create zip archives in nodejs

JavaScript math methods

JavaScript math constants

Tar basic commands to store and extract files

FlappingBird JS13K Games