JavaScript Code

How to repeat a string N times in JavaScript?

Repeat a string N times

Given a string and a number N, repeat the string for N times in the resulting string, using JavaScript.


To repeat a given string N times in JavaScript, use a looping statement, repeat the loop for N times, and append the given string to an output string variable in each iteration.


1. Given a string str . Repeat the str for N number of times.

var str = 'hello';
var N = 5;

var output = '';

for ( let i = 0; i < N; i++ ) {
     output += str;