Safe string concatenation
Suppose you have a couple of variables with unknown types and you want to concatenate them in a string. To be sure that the arithmetical operation is not be applied during concatenation, use
var one = 1; var two = 2; var three = '3'; var result = ''.concat(one, two, three); //"123"
This way of concatenting does exactly what you’d expect. In contrast, concatenation with pluses might lead to unexpected results:
var one = 1; var two = 2; var three = '3'; var result = one + two + three; //"33" instead of "123"
Speaking about performance, compared to the
join type of concatenation, the speed of
concat is pretty much the same.
You can read more about the
concat function on MDN page.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.GET THE BOOK NOW
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.GET THE BOOK NOW