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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

  • By
    Instagram For MooTools

    If you're still rocking an iPhone and fancy taking a photo every now and then, you'd be crazy not to be using an app called Instagram.  With Instagram you take the photos just as you would with your native iPhone camera app, but Instagram...

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!