I have this code:
(function() {
var ex;
try {
throw new Error('blah');
} catch(ex) {
console.log('ex i here:', ex);
}
console.log('ex out here:', ex);
return 'hi';
})()
This logs:
ex i here: Error('blah');
ex out here: undefined
Why is this so? I would think due to hoisting, ex
would get set outside of this block scope, so it should be available in ex out here
.
I expected it to work similar to a for loop:
for (i=0; i<2; i++) {
}
console.log(i); // gives 2