4

This code is part of a javascript course I am doing. And this exercise was in the error handling part where I needed to guess the output.

Code:

(function () {
  try {
    throw new Error();
  } catch (err) {
    var err = 5;   //creating another local err variable
    var boo = 10;
    console.log(err);
  }
  console.log(err);
  console.log(boo);
})();

Output:

5  
undefined   //I don't understand this part  
10  

The error is passed as a parameter err in the catch block. But with var, the err variable gets overwritten. So instead of logging the error, 5 gets logged in the first console.log(err).

But I couldn't figure out why the second console.log(err) outputs undefined. As far as I understood, var is not block-scoped, so, inside the same function scope, I should have access to the err variable. And, due to hoisting, var becomes undefined initially. But as try/catch block is synchronous and I am logging the variable after defining it, shouldn't it log 5? Because console.log(boo) returned 10 as expected.

Is it due to creating the variable with the same name as the local parameter?

NB: This is my first question on stackoverflow. So forgive any mistake!

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • The catch exception variable is only available inside the catch block: This: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#the_exception_identifier – Randy Casburn Mar 16 '21 at 23:33
  • 1
    A quick Google search yielded [this](https://medium.com/@sarbbottam/the-curious-case-of-variable-declarations-inside-a-catch-block-891a4369aaf0). My head hurts from reading it. – Lil Devil Mar 16 '21 at 23:33
  • I didn't know the spec hat places, where there are [alternatives](https://tc39.es/ecma262/#sec-try-statement-static-semantics-early-errors) to choose when wanted. Maybe i am also just misinterpreting, what i am reading. – ASDFGerte Mar 17 '21 at 00:07

1 Answers1

4

You're right that var is function scoped and not block scoped, however there's an exception for catch blocks if the var declaration has the same name as a catch block bound variable such that the shadowed var declaration will not be lifted to the function scope.

Relevant piece of the spec: https://262.ecma-international.org/10.0/#prod-annexB-CatchParameter

The Block of a Catch clause may contain var declarations that bind a name that is also bound by the CatchParameter. At runtime, such bindings are instantiated in the VariableDeclarationEnvironment. They do not shadow the same-named bindings introduced by the CatchParameter and hence the Initializer for such var declarations will assign to the corresponding catch parameter rather than the var binding.

Alex D
  • 672
  • 4
  • 12
  • This is the correct answer and should be accepted. – Randy Casburn Mar 17 '21 at 01:01
  • I was still a little bit confused about the exception bindings, but reading this [link](https://medium.com/@sarbbottam/the-curious-case-of-variable-declarations-inside-a-catch-block-891a4369aaf0) given by Lil Devil really clarified my concept. Thank you. It makes sense now. – Md. Arefin Rabbi Emon Mar 17 '21 at 18:12