String repeat() Method with Example in JavaScript

JavaScript String repeat() Method: Here, we are going to learn about the repeat() method of the string in JavaScript with Example.
Submitted by Shivang Yadav, on February 16, 2020

JavaScript | String repeat() Method

The String.repeat() method in JavaScript is used to generate a string by repeating the calling string n number of times. n can be any integer from o to any possible number in JavaScript.

Syntax:

    String.repeat(n);

Parameter(s):

The method accepts one parameter which is the number of times a number is repeated.

Return value:

The return type of this method is string which is the result of the repeating the strings.

Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.

Example:

str = "IncludeHelp"
//"IncludeHelp"
str.repeat(3)
//"IncludeHelpIncludeHelpIncludeHelp"
str.repeat(0)
//""
str.repeat(str.length)
//"IncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelp"
str.repeat(-1)
/*
VM424:1 Uncaught RangeError: Invalid count value
    at String.repeat (<anonymous>)
    at <anonymous>:1:5
*/	

JavaScript String Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.