var
var happyDays = function(day){
if(day === 'sunny') {
var a = 'apples'; //a is function-scoped
}
console.log(a); // apples
}
happyDays('sunny')
let
var happyDays = function(day){
if(day === 'sunny') {
let b = 'bananas'; //b is block-scoped
}
console.log(b); //ReferenceError: b is not defined
}
happyDays('sunny')
const
var happyDays = function(day){
if(day === 'sunny') {
const c = 'cherries'; //c is block-scoped
}
console.log(c); //ReferenceError: c is not defined
}
happyDays('sunny')
catch
try {
var d = 'doggies'; //d is function-scoped
throw 'my exception';
}
catch (err) {
console.log('error:', err); //err is block-scoped
}
console.log(d);
console.log(err);
// error: my exception
// doggies
// Uncaught ReferenceError: err is not defined