2
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
function fun()
{
    console.log(arguments)
}
const fun1 = (...n) =>{
    console.log(n)
}
fun.call(...arr1, ...arr2)

output : [object Arguments] { 0: -2, 1: 3, 2: 4, 3: 8, 4: 3, 5: -8, 6: 1 }

fun1.call(...arr1,...arr2)

output : [-2, 3, 4, 8, 3, -8, 1]

arr1 and arr2 combined have 8 values but output is only 7 values why? how to get all the values ?

ramya sri
  • 21
  • 3

1 Answers1

5

Because you've used Function.prototype.call to call fun, and the first argument to call isn't an argument to pass to fun, it's the value to use as this during the call. So that first value doesn't show up in the arguments fun sees (it's the value of this instead, wrapped in an object wrapper if this is loose mode code).

There's no reason to use fun.call in your example, just:

fun(...arr1, ...arr2);

but if you needed to us call for some reason, you'd specify a different first argument (to use as this during the call):

fun.call(nullOrWhateverYouWantThisToBe, ...arr1, ...arr2);

(Side note: In modern JavaScript, there's almost never any reason to use the arguments pseudo-array. Use a rest parameter instead, which gives you a true array.)

With those changes:

let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
function fun(...args) {
    console.log(`args.length = ${args.length}`);
    console.log(`args:`, args);
}

fun(...arr1, ...arr2);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875