<story><title>In JS functions, the ‘last’ return wins</title><url>https://jakearchibald.com/2021/last-return-wins/</url></story><parent_chain></parent_chain><comment><author>dvt</author><text>This article omits getting into try-catch-finally semantics, and it&amp;#x27;s not really that mysterious when you think about it (although, yes, please don&amp;#x27;t ask people about this in an interview):&lt;p&gt;- A catch block is only executed if an exception is thrown in the try block.&lt;p&gt;- A finally block is executed always after a try(-catch) block, if an exception is thrown or not.&lt;p&gt;&amp;gt; As a side-effect, returning from finally clears a thrown error:&lt;p&gt;Exceptions aren&amp;#x27;t &amp;quot;cleared,&amp;quot; they are &amp;quot;finally-d.&amp;quot; Because that&amp;#x27;s how finally &lt;i&gt;works&lt;/i&gt;[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 new scope (technically &lt;i&gt;augment&lt;/i&gt; scope):&lt;p&gt;&lt;pre&gt;&lt;code&gt;    function F() {
        try {
            throw &amp;quot;error&amp;quot;;
        } catch (err) {
            err = {
                &amp;quot;hello&amp;quot;: &amp;quot;world&amp;quot;
            }
            var hoisted = &amp;quot;foo&amp;quot;
            console.log(err)
        } finally {
            return hoisted; &amp;#x2F;&amp;#x2F; works, since this gets hoisted to the top of F
            return err; &amp;#x2F;&amp;#x2F; breaks, since err is spooky and only &amp;quot;scoped&amp;quot; in the catch block
        }
    }
&lt;/code&gt;&lt;/pre&gt;
[2] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;tc39.es&amp;#x2F;ecma262&amp;#x2F;multipage&amp;#x2F;ecmascript-language-statements-and-declarations.html#sec-try-statement&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;tc39.es&amp;#x2F;ecma262&amp;#x2F;multipage&amp;#x2F;ecmascript-language-statem...&lt;/a&gt;</text></comment>
<story><title>In JS functions, the ‘last’ return wins</title><url>https://jakearchibald.com/2021/last-return-wins/</url></story><parent_chain></parent_chain><comment><author>lucideer</author><text>&amp;gt; &lt;i&gt;The same happens in Java and Python too.&lt;/i&gt;&lt;p&gt;And yet, I expect this will soon appear in those &amp;quot;JS is weird, wtf&amp;quot; listicles.</text></comment>