CodePark/ questions / Errors & memory
START PRACTICINGPRACTICE
Frontend question · JavaScript › Errors & memory

What does this log?

function f() {
  try {
    throw new Error("x");
  } catch (e) {
    return "catch";
  } finally {
    return "finally";
  }
}
console.log(f());
finally

The answer is finally. A finally block always runs, even after a return in try or catch. A return inside finally replaces the pending return value. It would also swallow (hide) a pending exception. This is why returning from finally is considered bad practice.

The tempting option "catch" is wrong because the
return in finally runs after it and replaces it.