I mistakenly wrote a re-declaration of an argument as a const
in a function and instead of throwing SyntaxError: Identifier \'bar\' has already been decla
It's a myth that const
and let
aren't hoisted at all. They're half-hoisted. :-) That is: Within a block, if const bar
or let bar
(or class bar { }
for that matter) appears anywhere in that block, then bar
cannot be used prior to that declaration in the block — even if it exists in a containing scope. This area between the beginning of the block and the declaration is called the Temporal Dead Zone:
function foo(bar) {
// `bar` is fine here
try {
// Temporal Dead Zone, `bar` cannot be used here
console.log(bar);
// End of TDZ
const bar = 123;
// `bar` would be fine here
} catch(err) { console.log(err) }
}
foo(456);