Why use `concat` instead of `push` in this instance?
Push()
The push()
method is a generic method similar to call()
or apply()
. It will mutate your array (or object), pushing a new value into it.
Push: MDN
Concat()
The concat()
method returns a new array, with the values merged. This also avoids mutation side effects.
Concat: MDN
If some other code has a reference to the existing array in actionQueue
, using concat
will not affect that.
var a1 = [1];
var b1 = a1
a1 = a1.concat([2])
var a2 = [1];
var b2 = a2
a2.push(2)
console.log('b1', b1)
console.log('b2', b2)