Course contentsShow
← JavaScript
Lesson 1133 of 2,72326. Error HandlingFree lesson

Return Values in Try/Catch/Finally

How return statements interact with try, catch, and finally blocks in surprising ways.

Return Values in Try/Catch/Finally

You'll learn how return statements interact with try, catch, and finally blocks in ways that might surprise you.

When you place return statements inside try/catch/finally blocks, JavaScript follows a specific order of operations that can feel counterintuitive at first.

The Core Behavior

If you have a return statement in a try block and another in finally, the finally block's return always wins. The finally block executes after the try or catch block, and if it returns a value, that value overrides whatever was returned earlier.

function example1() {
  try {
    return "from try";
  } finally {
    return "from finally"; // This wins!
  }
}

console.log(example1()); // "from finally"

What About Catch?

The same rule applies: finally overrides catch:

function example2() {
  try {
    throw new Error("oops");
  } catch (error) {
    return "from catch";
  } finally {
    return "from finally"; // Still wins
  }
}

console.log(example2()); // "from finally"

Without a Return in Finally

If finally doesn't have a return statement, it won't override the try or catch return values:

function example3() {
  try {
    return "from try";
  } finally {
    console.log("cleanup happens");
    // No return here
  }
}

console.log(example3()); // "from try"

Think of finally as having the "last word" — if it speaks (returns), everyone else is silenced.

Key Takeaway: A return statement in a finally block overrides any return from try or catch. Use this power carefully — it's often clearer to avoid returning from finally altogether.