Do I need to `return` after `throw` in JavaScript?

Matthew picture Matthew · Sep 26, 2014 · Viewed 24.5k times · Source

I'm throwing an Error from a method of mine that I want an early exit from, as below:

// No route found
if(null === nextRoute) {
    throw new Error('BAD_ROUTE');
}

Do I need to put a return; statement after my throw? It works for me, for now. If it's superfluous I'd rather not put it in, but I can't be sure what different browsers might do.

Answer

Rob M. picture Rob M. · Sep 26, 2014

You do not need to put a return statement after throw, the return line will never be reached as throwing an exception immediately hands control back to the caller.