52

As described here, a quick way to append array b to array a in javascript is a.push.apply(a, b).

You'll note that the object a is used twice. Really we just want the push function, and b.push.apply(a, b) accomplishes exactly the same thing -- the first argument of apply supplies the this for the applied function.

I thought it might make more sense to directly use the methods of the Array object: Array.push.apply(a, b). But this doesn't work!

I'm curious why not, and if there's a better way to accomplish my goal. (Applying the push function without needing to invoke a specific array twice.)

1
  • +1 for discovering Firefox's Array.push, even if by accident. :-)
    – RobG
    Commented Mar 16, 2013 at 1:35

4 Answers 4

72

It's Array.prototype.push, not Array.push

4
  • 2
    Ah, thanks. For some reason Array.push is defined in Firefox, but not in Chrome. (At least in the respective javascript consoles.) Using the prototype method works just fine. Figures it was something relatively obvious. :)
    – starwed
    Commented Mar 16, 2013 at 0:42
  • @starwed—note that in Firefox, Array.push !== Array.prototype.push, wonder what it does?
    – RobG
    Commented Mar 16, 2013 at 0:58
  • 7
    For future reference, in Firefox, Array.push(a, b, c) is equivalent to Array.prototype.push.apply(a, b, c). It's just for convenience.
    – Casey Chu
    Commented May 12, 2013 at 10:35
  • note you can also use an instance of an array to append another array like so: (in node REPL) var a = [1,2,3,4];var b = [5,6,7,8]; a.push.apply(a,b) returns '[ 1, 2, 3, 4, 5, 6, 7, 8 ]` in response to '>a' Commented Jun 8, 2017 at 3:47
10

You can also use [].push.apply(a, b) for shorter notation.

3
  • For shorter notation you can use a.push(b) Commented Sep 22, 2016 at 0:13
  • 18
    @EugeneMala No, that would result in the whole array "b" being pushed into "a" as a new element: var a = [1,2], b = [3,4]; a.push(b); // [1, 2, Array[2]] Commented Dec 23, 2016 at 20:34
  • Note that [].push.apply(a, b) creates an unneeded new Array instance [], whereas a.push.apply(a, b) uses the existing one a
    – kca
    Commented Aug 11, 2021 at 5:30
10

The current version of JS allows you to unpack an array into the arguments.

var a = [1, 2, 3, 4, 5,];
var b = [6, 7, 8, 9];

a.push(...b); //[1, 2, 3, 4, 5, 6, 7, 8, 9];
1
  • Though I just did a very quick and dirty performance test and it looks like [].push.apply(a,b); is slightly faster in this case in my browser and concat is slightly slower. Commented Feb 27, 2019 at 16:27
4

What is wrong with Array.prototype.concat?

var a = [1, 2, 3, 4, 5];
var b = [6, 7, 8, 9];

a = a.concat(b); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
4
  • 4
    That's not really relevant here (my question wasn't about how to join two arrays) -- but the distinction is made clear in the question I linked to. concat returns a new function, rather than appending b to a.
    – starwed
    Commented Mar 16, 2013 at 0:43
  • 11
    Fair enough :) And it actually turns out Array.prototype.push.apply(a, b), is much faster: jsperf.com/arrayconcatvsarraypushapply Commented Mar 16, 2013 at 0:47
  • 3
    @phenomnomnominal, that's not necessarily true in all cases. The jsperf you link to extends the Array length in each loop, which of course is going to make the copying concat slower at each iteration, but if the Array length stays fixed through each iteration then concat can perform better, as seen here: jsperf.com/array-prototype-push-apply-vs-concat/5 (looking through all the other revisions reveals many neat approaches to this simple problem) Commented Jun 8, 2014 at 22:28
  • FWIW: a.push(4, 5, 6) adds the elements to the existing array a, whereas a = a.concat([4, 5, 6]) creates a new array instance and overwrites the variable a. Also consider var a = [1,2,3], a2 = a;, then a2 would stay unchanged with concat, but changes with push.
    – kca
    Commented Aug 11, 2021 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.