var str="hello";
str ="world";
In fact, the steps performed by this code behind the scenes are as follows:
(1) Create a string to store "hello".
(2) Create a string to store "world".
(3) Create a string to store the connection result.
(4) Copy the current content of str to the result.
(5) Copy "world" into the result.
(6) Update str so that it points to the result.
Steps 2 to 6 will be executed every time string concatenation is completed, making this operation very resource-consuming. If this process is repeated hundreds, or even thousands, of times, it can cause performance problems. The solution is to use an Array object to store the string, and then use the join() method (the parameter is an empty string) to create the final string. Imagine replacing the previous code with the following code: