Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result order of Javascript concat method

Tags:

javascript

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]
like image 344
ps0604 Avatar asked Apr 30 '18 22:04

ps0604


People also ask

How does concat work in JavaScript?

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.

Is concat faster than push?

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.

Is concat faster than spread Operator?

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.

Can you concat multiple arrays in JavaScript?

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.


1 Answers

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.

like image 88
Paul Avatar answered Oct 31 '22 06:10

Paul