Safe string concatenation

来源:互联网 发布:淘宝直播刷人软件下载 编辑:程序博客网 时间:2024/05/22 13:13

Suppose you have a couple of variables with unknown types and you want to concatenate them. To be sure that the arithmetical operation would not be applied during the concatenation:

var one = 1;var two = 2;var three = '3';var result = ''.concat(one, two, three); //"123"

This way of concatenation does exactly what you expect. On the contrary, 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, in comparison with join type of concatenation, the speed of the concat type is pretty much the same.

You can read more about concat method on MDN page.

from:github/loverajoel

0 0