โvar in block can't shadow outer letโ๏ธ
๐ง under construction
JS โฉ variable โฉ shadowing โฉ var in block can't shadow outer let
replit๏ผvar in block can't shadow outer let
function f() {
let a = 1; // let declaration
// var a = 2; // <----- var declaration is hoisted hereโ๏ธ
// which raises a redeclaration SyntaxError.
// (let doesn't allow redeclaration)
// block scope
{
// โญ๏ธ `var` doesn't have block scope,
// cannot shadow `let` variable in outer enclosing scope.
var a = 2;
// ^
// โ SyntaxError: Identifier 'a' has already been declared
}
// another block
{
let b = 3;
// โ although `var` doesn't have "block scope",
// we can't put let/var in the same block either.
var b = 4;
// ^
// โ SyntaxError: Identifier 'b' has already been declared
}
}
Previousvar redeclaration applied even in strict modeโ๏ธNextvar can shadow parameter even in strict modeโ๏ธ
Last updated
Was this helpful?