How to concatenate the string value length -1 in JavaScript.

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

For this, use join(). It will concatenate the string value length-1.

Example

Following is the code −

var count = 5;
var values = new Array(count + 1).join('John');
console.log(values);
var count1 = 5;
var values1 = new Array(count1).join('John');
console.log(values1);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo274.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo274.js
JohnJohnJohnJohnJohn
JohnJohnJohnJohn
raja
Published on 09-Nov-2020 12:24:56
Advertisements