Consider this:
'use strict';
{
let p = 1;
{
console.log(p);
let p = 2;
}
}
A gut feeling tells us it should log "1" (since the var must retain its old value before redeclared). However, the actual result is a ReferenceError. Why is that? (A standard-based explanation will be appreciated).
Please do note that I already have p
declared in the outer scope, so it's known in the inner block. If you comment out the p=2
line, everything works just fine.
As a post mortem note, although this behavior appears to be documented, it's still very counter-intuitive, cf. this C code:
void main() {
int p = 1;
{
printf("%d\n", p); // prints '1'
int p = 2;
}
}
Yet another JS fuckup peculiarity to make a note of!