JavaScript: why does changing an argument variable change the `arguments` "array"?

javascript

Solution

"Why does changing the value of `what` change the value of `arguments[0]`?"

Because that's how it's designed to work. The formal parameters are directly mapped to the indices of the arguments object.

That is unless you're in strict mode, and your environment supports it. Then updating one doesn't effect the other.

function hello(what) {
    "use strict"; // <-- run the code in strict mode
    what = "world";
    return "Hello, " + arguments[0] + "!";
}
hello("shazow"); // "Hello, shazow!"

Problem

Consider: ``` > function hello(what) { . what = "world"; . return "Hello, " + arguments[0] + "!"; . } > hello("shazow") "Hello, world!" ``` Why does changing the value of `what` change the value of `arguments[0]`?

Original source

Related problems