Variable
variableVAR
is visible outside the
if
statement because it uses function scope instead of block scope
function callMePlease() {
if (1 == 1) {
let variableLET = 'LET'
var variableVAR = 'VAR'
}
document.write('My ' + variableVAR + '</br>')
document.write('My ' + variableLET + '</br>')
}
callMePlease()
Another variable
variableLET
is not visible outside the block because it uses block scope and the code above will generate the following error:
Uncaught ReferenceError: variableLET is not defined