allow try...catch to return a value

My suggestion is to allow return in try...catch statements, such that this code:

/** @type { User | false } */
let user
try {
  user = getUser()
} catch (e) {
  user = false
}

can be replaced by this one:

const user = try {
  return getUser()
} catch(e) {
  return false
}

The braces could be made optional, which would mean an implicit return (as for arrow functions):

const user = try getUser() catch(e) false

the try...catch blocks therefore become statements, but I don't think this will be a problem with older code.

That (the "by this one" version) already works just fine.

@ljharb I think you missed the const user = part, try is not an expression.
It would need to be

const user = (() => {
  try {
    return getUser();
  } catch(e) {
    return false;
  }
})();

Related:

ah, lol, i did thanks. In that case, see https://github.com/tc39/proposal-do-expressions

1 Like