var c = 1;
function myFunction(){
c = 2;
var c = 4;
console.log(c);
}
console.log(c);
myFunction();
console.log(c);
最後の console.log が 1 を出力するのはなぜですか? これが私の脳内でどのように機能するかを示しています。
var c = 1; // create global variable called 'c'
function myFunction(){
c = 2; // assign value of global 'c' to 2
var c = 4; // create a new variable called 'c' which has a new address in memory (right?) with a value of 4
console.log(c); // log the newly created 'c' variable (which has the value of 4)
}
console.log(c); //log the global c
myFunction(); //log the internal c
console.log(c); //log the updated global c