JS strings "+" vs concat method [duplicate]
I have some experience with Java and I know that strings concatenation with "+" operator produces new object.
I'd like to know how to do it in JS in the best way, what is the best practice for it?
MDN has the following to say about string.concat()
:
It is strongly recommended to use the string concatenation operators (+, +=) instead of this method for perfomance reasons
Also see the link by @Bergi.
In JS, "+" concatenation works by creating a new String
object.
For example, with...
var s = "Hello";
...we have one object s.
Next:
s = s + " World";
Now, s is a new object.
2nd method: String.prototype.concat
There was a time when adding strings into an array and finalising the string by using join
was the fastest/best method. These days browsers have highly optimised string routines and it is recommended that +
and +=
methods are fastest/best