Finally overrides any return statement from the try/catch.
We write try/catch/finally for error handling in Javascript -
function tryTryTry() {
try {
return 4;
} catch (e) {
console.log(e);
} finally {
return 42;
}
}
console.log(tryTryTry());
//42
The function returns 42 and not 4.
Why? Because finally always gets executed - both for the normal execution flow and errors. In the above code block -
tryreturns 4- control flow jumps to
finallyblock finallyreturns 42- caller gets 42