Skip to content Skip to sidebar Skip to footer

How To Unshift Or Add To The Beginning Of Arguments Object In Javascript

I've just learned the convention for popping off the first element of the arguments array (which I also learned is actually an Object). Now I need to do the opposite. I need to use

Solution 1:

  1. use .call() instead of .apply() to invoke unshift()

  2. set arguments as the this value of unshift()

  3. set 'hello' as the argument to unshift()


Array.prototype.unshift.call(arguments, 'hello');

As @lonesomeday pointed out, you can use .apply() instead of .call(), but you need to pass an array-like argument as the second argument. So in your case, you'd need to wrap 'hello' in an Array.

Post a Comment for "How To Unshift Or Add To The Beginning Of Arguments Object In Javascript"