I was reading and coding along with this tutorial from javascript.info. I can't understand why in these two examples the outcomes are different.
Here the userName variable changes from "John" to "Bob":
let userName = 'John';
function showMessage() {
userName = "Bob"; // (1) changed the outer variable
let message = 'Hello, ' + userName;
alert(message);
}
alert(userName); // John before the function call
showMessage();
alert(userName); // Bob, the value was modified by the function
And here it stays "Ann":
function showMessage(from, text) {
from = '*' + from + '*'; // make "from" look nicer
alert(from + ': ' + text);
}
let from = "Ann";
showMessage(from, "Hello"); // *Ann*: Hello
// the value of "from" is the same, the function modified a local copy
alert(from); // Ann
Can someone help me understand why this happens?