- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Repeating string for specific number of times using JavaScript
Problem
We are required to write a JavaScript function that takes in a string and a number as two arguments.
Our function should repeat the input string the number of times specified by the second argument and return the new repeating string.
Example
Following is the code −
const str = 'this'; const num = 8; const repeatSpecificTimes = (str = '', num = 1) => { const arr = [str]; let i = 1; while(i < num){ arr.push(arr[0]); i++; }; return arr.join(''); }; console.log(repeatSpecificTimes(str, num));
Output
thisthisthisthisthisthisthisthis
- Related Articles
- Repeating each character number of times their one based index in a string using JavaScript
- Repeating letter string - JavaScript
- Number of times a string appears in another JavaScript
- Find first repeating character using JavaScript
- Return index of first repeating character in a string - JavaScript
- Inserting empty string in place of repeating values in JavaScript
- Finding number that appears for odd times - JavaScript
- Finding the first non-repeating character of a string in JavaScript
- First non-repeating character using one traversal of string in C++
- Repeating tuples N times in Python
- Find number of spaces in a string using JavaScript
- How to replicate a string for a specified number of times in MySQL?
- How to repeat a string for a specified number of times in Golang?
- JavaScript construct an array with elements repeating from a string
- Check if a string is repeating in itself in JavaScript

Advertisements