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


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

Updated on: 09-Nov-2020

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements