I have the following document https://www.codementor.io/cchilder/draft/bcgnrvw5p where I try to explain this:
// declare variables
const x = 1;
let y = 2;
var z = 3;
console.log(`Global scope - x, y, z: ${x}, ${y}, ${z}`);
if (true) {
console.log(`A new block scope - x, y, z: ${x}, ${y}, ${z}`);
// redeclare variables
// const x = 4;
let y = 5;
var z = 6;
}
At the top of the if block, y is not defined:
$ node variables.js
Global scope - x, y, z: 1, 2, 3
/Users/cchilders/variables.js:9
console.log(`A new block scope - x, y, z: ${x}, ${y}, ${z}`);
^
ReferenceError: y is not defined
I wasn't expecting this, and don't know how to explain. I have now:
When we redeclare these variables using the same names, we remove access to those names inside the block scope:
...
if (true) {
// inside this block, we lost access to the old x and y because they're going to be redeclared inside this block scope...but we can't use them yet, they haven't been declared yet
console.log(`A new block scope - x, y, z: ${x}, ${y}, ${z}`);
// redeclare variables
const x = 4;
let y = 5;
// now we can use the new x and y, 4 and 5 respectively
var z = 6;
}
...
Why does this happen, and how exactly does the JavaScript/Node interpreter read the code that causes this error?