I am new to coding and cannot figure out the following code. If anyone can give me a hand I will deeply aprreciate!!
function a() {
function b() {
console.log(x);
}
var x = 2;
b();
}
var x = 1;
a();
When I run this code it makes perfect sense that the console is 2
. Super!
But when I run this code:
function a() {
b();
function b() {
console.log(x);
}
var x = 2;
}
var x = 1;
a();
When this code runs, I would have thought that the console answer would be 2
as well! but I get undefined
as the answer. At least I would have thought that 1
could be the answer in case it was not 2, but never ever: undefined
.
Can anybody please give me a hand?
Many thanks!