Skip to content

Instantly share code, notes, and snippets.

@RinatValiullov
Last active Nov 26, 2017
Embed
What would you like to do?
Function list(), which return an array from arguments
// #1 With spread operator (ES6 syntax). Verbose, but interesting
let list1 = function(...args) {
let array = [];
for(let i = 0, len = args.length; i < len; ++i) {
array.push(args[i]);
};
return array;
};
// #1.1 Amazing arrow functions and spread syntax
let list1 = (...args) => {
let array = args;
return array;
};
// #2 With array-like object -> arguments
let list = function() {
// Convert object arguments to array (make copy of initial array with slice method)
let args = Array.prototype.slice.call(arguments);
return args;
};
/*
* list(8,6,4,2,11); // => [8, 6, 4, 2, 11]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment