Convert arguments to Array

By  on  

The arguments object thats automatically available within functions can be a source of confusion for some people; it's kind of an array but it's kinda not. JavaScript is awesome in that you can pass any number of arguments to a function, and oftentimes developers need to iterate over every argument provided.  The arguments object doesn't have a forEach method, but using a quick JavaScript technique, you can convert arguments to an array:

function myFn(/* any number of arguments */) {
	var args = Array.prototype.slice.call(arguments);
		// or [].slice.call(arguments)

	args.forEach(function(arg) {
		// do something with args here
	});
}

Much like converting a NodeList to an array, Array's slice method takes the arguments object and converts it to a true array, allowing for forEach, map, and traditional array iteration.  Keep that trick up your sleeve for future development.

Recent Features

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    JavaScript Battery API

    Mozilla Aurora 11 was recently released with a bevy of new features. One of those great new features is their initial implementation of the Battery Status API. This simple API provides you information about the battery's current charge level, its...

  • By
    CSS Vertical Center with Flexbox

    I'm 31 years old and feel like I've been in the web development game for centuries.  We knew forever that layouts in CSS were a nightmare and we all considered flexbox our savior.  Whether it turns out that way remains to be seen but flexbox does easily...

Discussion

  1. If you’re using Firefox, or in the future when other browsers support ES6, you can do it more elegantly:

    function myFn(...args) {
      /* code */
    }
  2. In case you are using Mootools, Array.from does the job, isn’t it ?

  3. Dmitry Tsvettsikh

    Why not it:

    var args = Array.apply(null, arguments);
    
    • David

      You can do that but be aware if you do something like:

        function x(){
         return Array.apply(null, arguments);
        };
        x(10); //=> it will return empty array with a length of 10 instead of [10]
      
  4. islomjon

    Cool trick, but why even typeof arguments returns object?

  5. We have a new method, Array.from(arguments)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!