Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does try catch have block level scope?

Tags:

javascript

If i create a variable in the catch block, is this block level scope?

It seems to create it in the global scope.

I thought it was block level because don't transpilers convert let statement to try/catch?

try {
    throw Error("test");
} catch (e) {
    var x = 15;
    console.log(x);
}

console.log(x);
like image 333
runners3431 Avatar asked Feb 18 '26 10:02

runners3431


1 Answers

If i create a variable in the catch block, is this block level scope?

If you create a block scoped variable (using let) it is.

It seems to create it in the global scope.

You are using var which always creates the variable in the scope of the current function (or global if you aren't in a function).

like image 86
Quentin Avatar answered Feb 20 '26 02:02

Quentin