JavaScript return vs throw: When to Return Errors or Throw

Understand return vs throw in JavaScript, when to return error values, when to throw errors, and how try...catch handles exceptions.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

JavaScript return vs throw: When to Return Errors or Throw

return and throw both stop the current function, but they communicate different outcomes. return sends a normal value back to the caller. throw signals an exception and transfers control to the nearest matching catch block.

Use return for expected results, including expected validation states. Use throw when the function cannot continue normally and the caller should handle an exceptional failure.

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Method 1: Return a Normal Value

A return statement exits a function and gives a value to the caller.

javascript
function square(number) {
  return number * number;
}

console.log(square(8));
Output

You should see one line logging 64.

This is normal control flow. Nothing is considered an error.


Method 2: Return an Error Object as Data

Sometimes an error is an expected result. In that case, returning a status object can be clearer than throwing.

javascript
function parseAge(value) {
  const age = Number(value);

  if (Number.isNaN(age)) {
    return { ok: false, error: "Invalid age" };
  }

  return { ok: true, value: age };
}

console.log("return-error:", JSON.stringify(parseAge("abc")));
Output

You should see one line logging return-error: {"ok":false,"error":"Invalid age"}.

This pattern is common when validation failure is part of the normal user flow.


Method 3: Throw an Error for Exceptional Failure

Use throw when the function cannot complete correctly.

javascript
function requireAge(value) {
  const age = Number(value);

  if (Number.isNaN(age)) {
    throw new TypeError("Invalid age");
  }

  return age;
}

try {
  requireAge("abc");
} catch (error) {
  console.log("throw-error:", error.name + ":" + error.message);
}
Output

You should see one line logging throw-error: TypeError:Invalid age.

The thrown error skips normal return flow and is handled by catch.


Method 4: return Inside try...catch

A return inside a try block returns normally unless an error is thrown before it.

javascript
function readMode(value) {
  try {
    if (!value) return "default";
    return value;
  } catch (error) {
    return "fallback";
  }
}
Output

catch only runs for thrown exceptions. It does not run because a function returned an error-shaped object.


Common Questions About return vs throw

Should I return an error or throw an error?

Return an error value when failure is expected and part of normal logic. Throw an error when the function cannot continue correctly.

Can try...catch catch a returned error?

No. try...catch catches thrown exceptions, not returned values.

Does throw stop function execution?

Yes. throw stops the current function unless the error is caught inside that function.


Summary

Use return for normal JavaScript function results and expected validation states. Use throw for exceptional failures that should interrupt normal flow and be handled with try...catch or promise rejection handling. Returned errors are data; thrown errors are exceptions.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …