Skip to content

Instantly share code, notes, and snippets.

@oconn
Created April 29, 2014 12:25
Show Gist options
  • Save oconn/11398705 to your computer and use it in GitHub Desktop.
Save oconn/11398705 to your computer and use it in GitHub Desktop.
Javascript Arguments

Javascript Arguments

Javascript arguments are stored in an array like structure where indicies can be accesses using bracket notation, however none of the objects properties are available except length.

function args(){
  return arguments;
}
console.log(args(1,2,3) instanceof Array); // -> false

Converting arguments to and array

Using iteration

function argumentsToArray(){  
  var args = []

  for(var i = 0; i < arguments.length; i++){
    args[i] = arguments[i]
  };
  return args
};

var newArray = argumentsToArray(1, 2, 3, [1,2,3], {"city" : "Kilua", "state" : "Hawaii" });
console.log(newArray instanceof Array); // -> true

Using apply

function argumentsToArray(){
  return Array.apply(Array, arguments);
};

var newArray = argumentsToArray(1, 2, 3, [1,2,3], {"city" : "Kilua", "state" : "Hawaii" });
console.log(newArray instanceof Array); // -> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment