Skip to content

Instantly share code, notes, and snippets.

@stefanfrede
Last active June 7, 2016 05:08
Show Gist options
  • Save stefanfrede/f55aa49dee6098e4a79c to your computer and use it in GitHub Desktop.
Save stefanfrede/f55aa49dee6098e4a79c to your computer and use it in GitHub Desktop.
Convert arguments, an array-like object, into an array.
/**
* Common practice to convert arguments into an array:
*/
var args = Array.prototype.slice.call(arguments);
/* or the shorthand */
var args = [].slice.call(arguments);
/**
* If you want an array of the arguments that lets you
* use the optimization of the V8 JavaScript engine used
* in Chrome and Node you need to resort to this:
*/
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
/**
* Or if you are using ES2015 you can use the
* spread operator ...:
*/
const args = [...arguments];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment