My question is why is a variable (when using var keyword) accessible outside the if statement block, but not outside the function block? Consider this code:
let age = 30;
if (true){
let age = 40
console.log('inside', age) //logs inside, 40//
var name = 'shaun'
}
console.log('outside if loop', age, name) //logs outside if loop, 30, shaun //
function test (){
var xyz = 'ashley';
console.log(xyz)
}
test ()
console.log('outside function', xyz) //throws an error//
Thank you for your time. Any insights would be appreciated!
Aucun commentaire:
Enregistrer un commentaire