Question about the "arguments" object

Hi all!

I just finished the Symmetric Difference challenge but came across something involving the “arguments” object while working it that I don’t understand.

I’ve outlined the core of the problem below in case anyone that hasn’t done the challenge knows the answer to this question.

When I execute the following function,

function test () {
  var ans = 1;
  if (arguments[1] == arguments[0]) {
    ans = 2;
  }
  return ans;
}
test(1, 1);

the function returns 2. However, if I execute the exact same function but change the arguments as follows,

function test () {
  var ans = 1;
  if (arguments[1] == arguments[0]) {
    ans = 2;
  }
  return ans;
}
test([1, 2], [1, 2]);

the function returns 1.

Why exactly does this happen?

When comparing arrays in javscript it checks if they are the same instance.

let x = [1,1];
let y = [1,1];
let z = x;

console.log(x == y);    // false
console.log(x == x);   // true
console.log(x == z);  // true
1 Like

Ok, so it doesn’t necessarily apply only to arrays passed through with the arguments object, but to all arrays in general. Thanks!