JavaScript Glossary: String repeat()

Written by Scotch.io - - Aggregated on Saturday February 9, 2019

Basics

The String repeat() method is a very fun one. You can take any string and repeat over it so that it displays multiple times.

"hello ".repeat(5);
// output: hello hello hello hello hello

https://scotch.io/embed/gist/a22d8df7f2ff64422a7b012b1ced59bf

It can also work with emojis!

"?✨?".repeat(10);

// output: ?✨??✨??✨??✨??✨??✨??✨??✨??✨??✨?

This can be useful as filler text or for annoying your friends.

Syntax

myString.repeat(numberOfTimesToRepeat);

1 parameter

count (integer)

The number of times you want to repeat over the string.

Returns a new string

Repeat() does not affect the original string. It creates a new one and returns that.


« JavaScript Glossary: String replace() - Scotch.io

Symfony blog - A Week of Symfony #632 (4-10 February … »