I just read the discussion about var
and let
in Kyle Simpsons "You Dont Know Javascript."
The function foo
uses block declaration of variables with let
, the function bar
uses ordinary declaration with var
. For my clarity, in this example, the variables b
and c
are actually available in the same scopes, correct? So what is the point of presenting the foo
function here?
function foo() {
var a = 1;
if (a >= 1) {
let b = 2;
while (b < 5) {
let c = b*2;
b++;
console.log(a + b);
}
}
}
function bar() {
var a = 1;
if (a >= 1) {
var b = 2;
while (b < 5) {
var c = b*2;
b++;
console.log(a + b);
}
}
}