Finally always executes. That's what it's for, which means its return value gets used in your case.

You'll want to change your code so it's more like this:

function example() { 
    var returnState = false; // initialization value is really up to the design
    try { 
        returnState = true; 
    } 
    catch {
        returnState = false;
    }
    finally { 
        return returnState; 
    } 
} 

Generally speaking you never want to have more than one return statement in a function, things like this are why.

Answer from annakata on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › try...catch
try...catch - JavaScript | MDN
Of course, any new exceptions raised in the "inner" block (because the code in catch block may do something that throws), will be caught by the "outer" block. If the finally block returns a value, this value becomes the return value of the entire try-catch-finally statement, regardless of any ...
🌐
W3Schools
w3schools.com › jsref › jsref_try_catch.asp
JavaScript try/catch/finally Statement
JavaScript creates an Error object with two properties: name and message. The try...catch...finally statements combo handles errors without stopping JavaScript.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › error handling
Error handling, "try...catch"
The finally clause works in case of any exit from try...catch, even via the return statement: right after try...catch is done, but before the calling code gets the control.
🌐
Educative
educative.io › answers › how-to-handle-the-try-catch-finally-blocks-in-javascript
How to handle the try/catch/finally blocks in JavaScript
The return 10 in the try block will not be reached because we throw a Return error before reaching the return statement. Now, the catch block will catch the Return Error, and the return 20 will not be considered because there is a finally block present.
🌐
Medium
medium.com › @roy.gopal1992 › things-to-dont-know-about-try-catch-finally-c4165126e934
Things to know about Javascript try-catch-finally | Medium
January 7, 2022 - So when try/catch encounters return, it will immediately move to finally and return value of function will be in order of finally -> try/catch -> function.
🌐
Medium
medium.com › @goradux › back-to-basics-javascripts-try-catch-finally-181f8f6bc370
Back to Basics: JavaScript’s try/catch/finally | by Albert Asratyan | Medium
September 17, 2023 - Sometimes people use nested try ... catch blocks inside of the original catch statement. However, please note that in order to produce a similar behavior where we want to both clean up and throw the error, we would need to duplicate the finally code, since it has to run both on success and failure: try { console.log("try code"); mightError("try error"); console.log("finally code"); // trying to replicate the cleanup behavior // return "try return"; // if you need a return value } catch (exception) { try { console.log("catch code"); mightError("catch error"); console.log("finally code"); // try
Find elsewhere
🌐
GitConnected
levelup.gitconnected.com › 5-things-you-dont-know-about-try-catch-finally-in-javascript-5d661996d77c
5 things you don’t know about try-catch-finally in JavaScript
December 18, 2019 - function test() {try { return 10; throw "error"; // this is not executed, control goes to finally } catch { console.log("catch"); return 1; } finally {… ... Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev ... Articles related to JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Promise › finally
Promise.prototype.finally() - JavaScript | MDN
Moreover, notice the Promise.resolve() call above — in reality, onFinally()'s return value is resolved using the same algorithm as Promise.resolve(), but the actual constructor used to construct the resolved promise will be the subclass. finally() gets this constructor through promise.constructor[Symbol.species]. ... let isLoading = true; fetch(myRequest) .then((response) => { const contentType = response.headers.get("content-type"); if (contentType && contentType.includes("application/json")) { return response.json(); } throw new TypeError("Oops, we haven't got JSON!"); }) .then((json) => { /* process your JSON further */ }) .catch((error) => { console.error(error); // this line can also throw, e.g.
🌐
Flaming Codes
flaming.codes › posts › advanced try/catch/finally in javascript and typescript
Advanced try/catch/finally in Javascript and Typescript
June 24, 2021 - // Here's a better use case for // finally. async function getData(){ let error: Error; try { const result = await fetch(); return { result }; } catch(e) { error = e; return { error } } finally { // No return in finally, // so it's safe to use. ...
🌐
Reddit
reddit.com › r/dartlang › return statement in try catch with finally
r/dartlang on Reddit: Return Statement In Try Catch With Finally
January 6, 2023 -

So I learned this the hard way today - does anyone have any documentation for this behavior? It seems weird to me. Even with a return statement, the finally block will run. I tested this in our Flutter mobile app as well as Dart Pad just now.

void main() {
  print(doThing());
  // prints "still running finaly block" then 42
}

doThing() {
  try {
    return aux();
  } finally {
    print('still running finally block');
  }
}

int aux() => 42;

Of course, code shouldn't be doing this when you think about it, because that's what finally blocks are there for, but it came about from a refactor and I thought it was interesting.

EDIT: Thanks for the responses; turns out I was not versed in the interaction of a finally blocks and return statements, because this is the way it works in many languages.

🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript try…catch…finally
JavaScript try...catch...finally
October 6, 2023 - First, declare the result variable and initialize its value with 0. let result = 0;Code language: JavaScript (javascript) Second, call the add() function and assign the return value to the result variable in the try block.
🌐
Hacker News
news.ycombinator.com › item
In JS functions, the ‘last’ return wins | Hacker News
July 6, 2021 - Exceptions aren't "cleared," they are "finally-d." Because that's how finally works[2]. The final block, if evaluated, always overrides the result of the previous blocks. A slightly more interesting example of try-catch weirdness is how catch blocks are one of the few constructs that create ...
🌐
DocStore
docstore.mik.ua › orelly › webprog › jscript › ch06_17.htm
try/catch/finally (JavaScript: The Definitive Guide, 4th Edition)
The try/catch/finally statement is JavaScript's exception-handling mechanism. The try clause of this statement simply defines the block of code whose exceptions are to be handled. The try block is followed by a catch clause, which is a block of statements that are invoked when an exception ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Control_flow_and_error_handling
Control flow and error handling - JavaScript | MDN
If any statement within the try ... thrown in the try block, the catch block is skipped. The finally block executes after the try and catch blocks execute but before the statements following the try...catch statement....
🌐
Reddit
reddit.com › r/javascript › [askjs] i just found out that "finally" block runs even when "try" / "catch" block returns
r/javascript on Reddit: [AskJS] I just found out that "finally" block runs even when "try" / "catch" block returns
December 20, 2021 - Even though the result is immediately returned in the try block, finally block will actually run right before that, ensuring our teardown still gets called and making code simpler. Share ... This has been the case for every programming language which supports finally. ... That's the whole point about finally. If it weren't for that, you could just put the code after the try/catch ...
🌐
2ality
2ality.com › 2013 › 03 › try-finally.html
JavaScript: try-finally
This blog post is a quick reminder of how try-finally works. ... var count = 0; function foo() { try { return count; } finally { count++; } } console.log(foo()); console.log(count); The output is:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Promise › try
Promise.try() - JavaScript | MDN
async function doSomething(action) { try { const result = await action(); console.log(result); } catch (error) { console.error(error); } finally { console.log("Done"); } } Promise.try() is a generic method. It can be called on any constructor that implements the same signature as the Promise() ...