Return to site

JavaScript: scope of 'var' versus the scope of 'let' 🔭

· react

with var in if :

var name ="outside";
console.log('before:'+name);


if(true){
    var name="inside"
    console.log('in the block:'+name)
}


console.log('after:'+name)


//outputs
// before:outside
// in the block:inside
// after:inside

with let in if :

var name ="outside";
console.log('before:'+name);


if(true){
    let name="inside"
    console.log('in the block:'+name)
}


console.log('after:'+name)


//outputs
// before:outside
// in the block:inside
// after:outside