โ—var in block can't shadow outer letโ—๏ธ

๐Ÿšง under construction

JS โŸฉ variable โŸฉ shadowing โŸฉ 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
    }
    
}

Last updated