Could someone please elaborate on the distinction between these two sets of JavaScript code snippets?
var orange = { prop: "I am a simple fruit"};
console.log(orange.prop); //output: "I am a simple fruit"
function go(orange) {
orange.prop = "Now I have been changed by the function";
};
go(orange);
console.log(orange.prop); // output: "Now I have been changed by the function"
.
In the block above, the go
function clearly modified the orange
object in the outer scope
.
var apple = "I am a simple apple";
console.log(apple); // output "I am a simple apple"
function goApple(apple) {
apple = "Now I have been changed by the function";
};
goApple(apple);
console.log(apple); // output "I am a simple apple"
In this block, the initial apple
variable remains unchanged
I seem to be overlooking something fundamental and obvious here, or could it be that JavaScript follows different scoping rules for various variable types?