7

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]
ps0604
  • 1,227
  • 23
  • 133
  • 330
  • 3
    Downvotes but no one tries to explain whats wrong with the question? And no one casts a close vote? Weird ... – Jonas Wilms Apr 30 '18 at 22:09
  • 1
    +1 from my side because it's a valid question but I would still like to recommend you to google out questions like these though since it's less of a debugging question and leads to opinionated answers and links. – AndrewL64 Apr 30 '18 at 22:13
  • @JonasW. This question is on-topic, clear and answerable, but it shows absolutely no research effort. – Sebastian Simon Apr 30 '18 at 22:24
  • @xufox sure. Then why did the three downvoters not write this?! A downvote is a meaningless sign of ignorance – Jonas Wilms Apr 30 '18 at 22:27
  • @JonasW. This reason is already covered in the tooltip on the downvote arrow: _“This question does not show any research effort; it is unclear or not useful”_. – Sebastian Simon Apr 30 '18 at 22:28
  • @Xufox My most [upvoted question](https://stackoverflow.com/questions/6989902/is-it-safe-to-assume-strict-comparison-in-a-javascript-switch-statement) is very similar to this one. The answer can be paraphrased to "Yes, it says it here in the spec". It has had no downvotes to date. When I asked it in 2011 I had never even heard of ECMAScript and wouldn't have known where to look; that could very well be the case for this OP today. He's tried at least one test-case and got the result he expects. Now just wants to confirm that he can rely on that behaviour. – Paul Apr 30 '18 at 23:02

1 Answers1

15

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.

Paul
  • 139,544
  • 27
  • 275
  • 264