Concat strings in JavaScript

In this article I will try to explain a couple of different methods to concat strings in JavaScript.
The first method is possibly the easiest way:

var string = "a" + " b" + " c";  
console.log(string); // this will echo "a b c"  

We are concating 3 strings in one line and set them to one variable. Strings are concated with the + sign in JavaScript but there are also different methods and ways.

In the second method new strings are appendet to a already existing string.

var string = "";  
string += "a";  
string += " b";  
string += " c";

console.log(string); //this will echo "a b c"  

The method is actually the same as the first one except that it is better to read because it is in multiple lines. In this case it is not that different but if yo got far longer strings this is better readable.

var string = [];  

console.log(string.join(" ")); // this will echo "a b c";  

In this Method we are using a array and pushing strings onto the end of the array and join the array in the end. Though it is far slower performance wise in comperison with just the first 2 methods. But in my opinion it is better readable.

var string = [];  
string.push(" b");  
string.push(" c");  
console.log(string.join(" ").replace(/\ {2,}/g, " ")); // this will echo "a b c";  

In this example we are checking that there aren't any double spaces in the ending string. I made this example just so nobody would what about double spaces. But because of the regular expression this example will be the slowest.

var string = ["a", "b", "c"];  
string = string.join(" ");  

Last but not least, the litereal example without any push function, this will be faster then the othere array solutions but it will still be a lot slower then the string methods.

But well we are talking about nanoseconds not milliseconds and in my honest opinion I do like the array solution since it is more versatile and even more readable and I do not need to include the space character in my strings. Multiline declaration becomes even cleaner without the + at the end of the line.

I've included a test: jsperf Test