Home >Web Front-end >JS Tutorial >Analysis of the difference between JS array merge push and concat_javascript skills
The example in this article describes the difference between JS array merge push and concat. Share it with everyone for your reference, the details are as follows:
Pay attention to the spelling of concat. The two functions are very similar, but there are two differences.
Look at the code first:
var arr = []; arr.push(1); arr.push([2, 3]); arr.push(4, 5); arr = arr.concat(6); arr = arr.concat([7, 8]); arr = arr.concat(9, 10); arr.each(function(index, value){ alert(value); });
alert result:
1 2,3 4 5 6 7 8 9 10
Difference:
When push encounters an array parameter, it treats the entire array parameter as one element; while concat splits the array parameter and adds it element by element.
push directly changes the current array; concat does not change the current array.
Summary:
If you want to append to the array, use concat, but it is the same as java's replace. Remember arr1=arr1.concat(arr2)
I hope this article will be helpful to everyone in JavaScript programming.