you have one single paramenter in the function, which is args, but you are passing in two arguments (the first is the array, the second is the number), so the first argument is assigned to the parameter, but the other argument is not
If you want to access all the arguments you can or use the arguments object
or like this:
function some(...args) { // this is storing all the arguments passed in in an array called args
return args;
}
console.log(some([1,2,3], 5)); // -> [[1,2,3], 5]
I was not aware that if I forward ...someName as a function’s argument, then all the arguments will be passed to the array like someName.
While you were answering, I was looking through google and found another solution, which suits me very well for solving the problem I’ve faced:
function some(arg, ...args) {
return `The first argument is saved to arg - [${arg}] and the rest of the arguments are saved to args - [${args}]`;
}
console.log(some([1,2,3], 5,4,3,2,1)); // -> [[1,2,3], 5]
// -> "The first argument is saved to arg - [1,2,3] and the rest of the arguments are saved to args - [5,4,3,2,1]"
likewise when you do some([1,2,3], 5) you’re sending 2 values to your function so it must have 2 variables to assign those values, example function some(arg1, arg2).
The same applies if you want 3, 10 or 50 arguments, you would need to add the same number of arguments to your function.
Alternative you can use the spread operator as stated by @ILM , the arguments variable or like you stated to have access to every argument.
then the count of arguments will be hardcoded and some of them will be missed, if their count will be increased.
I need more flexible solution, cause I do not know the count of the arguments, but I only know that the first one is an array and all the others are some numbers.