In one part of a (Udacity) lesson on JavaScript objects, it says that because primitives are immutable, any changes made to an argument inside a function effectively creates a copy local to that function, without affecting the primitive outside of it. This is the example provided:
function changeToEight(n) {
n = 8; // whatever n was, it is now 8... but only in this function!
}
let n = 7;
changeToEight(n);
console.log(n); // 7
However, as shown in the following example, you can change the value assigned to a global variable from within a function (as long as the variable name isn't passed as an argument):
let counter = 1;
function changeCounter() {
counter = 2;
}
changeCounter();
console.log(counter); // 2
If primitives are immutable, why is the variable mutable in the latter example? Does the example provided in the lesson actually have anything to do with primitives not being immutable, or is it just because the variable name in the function is identical to the name of the argument passed to the function?