Is the order of the concatenation result always preserved?
var alpha = ['a', 'b', 'c'];
var numeric = [1, 2, 3];
alpha.concat(numeric);
// result in ['a', 'b', 'c', 1, 2, 3]
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
concat returns a new array while push returns the length of updated array. Because concat creates a new array to hold the result of merged arrays, it's slower than push . For small arrays, both methods do not produce a significant difference in term of performance.
The concat() method is more efficient and fast when the array size is large. ... is more efficient when the array size is small. Using ... for large array size will give error of Maximum call stack size exceeded , which can be avoided by using the concat() method.
The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.
Yes, it is.
From MDN (emphasis mine):
The concat method creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument
From the ECMAScript spec (emphasis mine):
When the concat method is called with zero or more arguments, it returns an array containing the array elements of the object followed by the array elements of each argument in order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With