JavaScript String Concat

The concat() method combines the two or more strings and returns a new string.

Syntax:

yourString.concat(string1, [string2, string3]);

Parameters:

We can pass the one or more string parameters.

Description:

The concat() method combines two or more string and returns the new string.

var yourString = "JavaScript";
document.write(yourString.concat(" Hive"));

The concat() vs +=

JavaScript experts said that concat() method is slow down the operation of string joining and they recommended to use the += operator to join one or more string because it much faster than any other joining techniques.

We can also use the join() method of array to convert array of strings to single string but it is also bit slower than the += operator.

The only one advantage of using the concat() method is to increase the readability of the program. Everyone wants the program than much more easy to understand and more readable. But we recommended to  use the += operator because it much more faster.

But I personally believe that it purely depends on the browsers and it’s JavaScript engine.

function a() {
    var a = new Date().toDateString();
    a += new Date().toDateString();
    return a;
}

function b() {
    var a = new Date().toDateString();
    var b = a.concat(new Date().toDateString());
    return b;
}

var t1 = new Date();

var times = 10000;
for (i = 1; i <= times; i++) {
    a();
}

var t2 = new Date();


document.writeln("Using the += opeartor string concatination 10000 times: " + (t2 - t1) + "
");

var t1 = new Date();

for (i = 1; i <= times; i++) {
    b();
}

var t2 = new Date();

document.writeln("Using the concat() method string concatination 10000 times: " + (t2 - t1));

But when I wrote the above program and run in the different – different browsers like the Firefox, Chrome, and IE, but however I found that concat() method is works faster in the all the browsers than += operator,

Please post your reviews about the concat() vs the += operator.

 Happy Coding!!! 

🙂