How to get a slice from "arguments"
Q. How to slice everything but first element from arguments
?
The following will return an array containing all arguments except the first:
var slicedArgs = Array.prototype.slice.call(arguments, 1);
You don't have to convert arguments
to an array first, do it all in one step.
Meddling with array functions is not actually necessary.
Using rest parameter syntax ...rest
is cleaner and more convenient.
Example
function argumentTest(first, ...rest) {
console.log("First arg:" + first);
// loop through the rest of the parameters
for (let arg of rest) {
console.log("- " + arg);
}
}
// call your function with any number of arguments
argumentTest("first arg", "#2", "more arguments", "this is not an argument but a contradiction");
...Rest
- See the example Fiddle
- See MDN Documentation page