loading...

re: Do You Still Use Var? If So Why? VIEW POST

FULL DISCUSSION
 

I use it. At the very least is useful for this kind of thing.

try {
  var stuff = await unsafe_stuff();
} catch (err) {
  return Promise.reject(err.message);
}
 

Why is it necessary to use var instead of let in your example? You don't access stuff variable in catch block anyway.

 

But I want to use it outside the try/catch block.

Just define it in the parent scope. It has extra line if compare with var, but I think this code is more obvious, because there is no implicit hoisting.

async function foo() {
  let stuff;
  try {
    stuff = await unsafe_stuff();
  } catch (err) {
    return Promise.reject(err.message);
  }
  console.log(stuff);
}

That is what I'm trying to avoid. And you could still do the same with var.

I choose to believe var's implicit hoisting is a feature and not a bug.

I agree, this is a valid use case for var if you prefer implicit hoisting.

Code of Conduct Report abuse