I am working with try-catch blocks in JavaScript and have run into some variable scoping behaviour I don't completely understand.
I understand that console.log(boo) prints 20 to the console because the variable has been declared with the var keyword and hence it is functionally scoped (i.e. not block scoped to the catch block).
However, I don't understand why the err variable isn't also scoped to the IIFE in the same way as the boo variable. Therefore I don't understand why it is undefined outside of the catch block.
(function() {
try {
throw new Error();
} catch (err) {
var err = 10;
var boo = 20;
console.log(err); //'10' (as I expect)
}
// Why doesn’t this log '10' ???
console.log(err); // 'undefined' (but I expected '10')
console.log(boo); // '20' (as I expect)
})();
