Question about the "arguments" object

Question about the "arguments" object
0

#1

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?


#2

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

#3

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