Home » JavaScript Tutorial

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
*/	





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.