I’m trying to push multiple elements as one array, but getting an error:

> a = []
[]
> a.push.apply(null, [1,2])
TypeError: Array.prototype.push called on null or undefined

I’m trying to do similar stuff that I’d do in ruby, I was thinking that apply is something like *.

>> a = []
=> []
>> a.push(*[1,2])
=> [1, 2]

13 Answers
13

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *