javascriptarguments

How does assignment to properties of the `arguments` object work?


Why does this function output 10?

function b (x, y, a) {
  arguments[2] = 10;
  console.log(a);
}

b(1, 2, 3);


Solution

  • javascript arrays are zero indexed and arguments refers to parameters passed into function as arguments :

    arguments[2] === a === 10
    

    and

    1 === x === arguments[0];
    2 === y == arguments[1];
    

    (and triple equality operator is not a mistake)