<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Byte Corner</title>
    <description>The latest articles on DEV Community by Byte Corner (@bytecorner).</description>
    <link>https://dev.to/bytecorner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4157251%2F0dfe6e6d-00a2-4017-85bc-4814c7b28b56.png</url>
      <title>DEV Community: Byte Corner</title>
      <link>https://dev.to/bytecorner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bytecorner"/>
    <language>en</language>
    <item>
      <title>The Back Button Is More Powerful Than You Think</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Wed, 18 Nov 2026 20:53:56 +0000</pubDate>
      <link>https://dev.to/bytecorner/the-back-button-is-more-powerful-than-you-think-174j</link>
      <guid>https://dev.to/bytecorner/the-back-button-is-more-powerful-than-you-think-174j</guid>
      <description>&lt;p&gt;You open a website, scroll down, fill out a form, and navigate to another page.&lt;/p&gt;

&lt;p&gt;Then you press Back.&lt;/p&gt;

&lt;p&gt;The page appears almost instantly. Your form is still filled out. Your scroll position is exactly where you left it.&lt;/p&gt;

&lt;p&gt;You might think the browser reloaded the page and restored some information.&lt;/p&gt;

&lt;p&gt;But here's the interesting part: &lt;strong&gt;the browser may have preserved the entire page, including its JavaScript execution environment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No HTML parsing. No component initialization. No JavaScript re-execution.&lt;/p&gt;

&lt;p&gt;Welcome to the &lt;strong&gt;back/forward cache&lt;/strong&gt;, or &lt;strong&gt;bfcache&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 What Actually Happens When You Navigate Away?
&lt;/h2&gt;

&lt;p&gt;Normally, when you navigate to a new document, the browser can discard the previous page.&lt;/p&gt;

&lt;p&gt;Returning to it means constructing a new document, executing its scripts, and rebuilding its state.&lt;/p&gt;

&lt;p&gt;Even with resources stored in the HTTP cache, there's still work to do.&lt;/p&gt;

&lt;p&gt;But modern browsers have another option.&lt;/p&gt;

&lt;p&gt;Instead of destroying the previous page, they can &lt;strong&gt;freeze it and preserve it in memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The DOM, including changes made by JavaScript.&lt;/li&gt;
&lt;li&gt;The JavaScript heap, including variables, objects, and application state.&lt;/li&gt;
&lt;li&gt;Registered event listeners.&lt;/li&gt;
&lt;li&gt;Form values and scroll position.&lt;/li&gt;
&lt;li&gt;The existing document and its execution context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you navigate back, the browser can resume this preserved page.&lt;/p&gt;

&lt;p&gt;Think of it as suspending an application rather than closing and reopening it.&lt;/p&gt;

&lt;p&gt;And this distinction matters because &lt;strong&gt;restoring a page is not the same as loading one&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 HTTP Cache vs. bfcache: Two Completely Different Things
&lt;/h2&gt;

&lt;p&gt;These two caches solve different problems.&lt;/p&gt;

&lt;p&gt;Imagine your browser needs to display a page containing HTML, JavaScript, images, and stylesheets.&lt;/p&gt;

&lt;p&gt;With the HTTP cache, the browser might already have those resources stored locally.&lt;/p&gt;

&lt;p&gt;That's useful. It avoids unnecessary downloads.&lt;/p&gt;

&lt;p&gt;But the browser still needs to construct the document and execute its JavaScript.&lt;/p&gt;

&lt;p&gt;With bfcache, the browser doesn't need to reconstruct the page at all.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTTP cache&lt;/th&gt;
&lt;th&gt;Back/forward cache&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stores network resources&lt;/td&gt;
&lt;td&gt;Preserves an entire page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can avoid downloading files&lt;/td&gt;
&lt;td&gt;Can avoid rebuilding the page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JavaScript executes on a new load&lt;/td&gt;
&lt;td&gt;Existing JavaScript state resumes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used for resource requests&lt;/td&gt;
&lt;td&gt;Used for eligible history navigations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Controlled partly by HTTP caching rules&lt;/td&gt;
&lt;td&gt;Controlled by browser eligibility and lifecycle behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here's a subtle detail: &lt;strong&gt;&lt;code&gt;Cache-Control: no-cache&lt;/code&gt; doesn't mean a page cannot enter bfcache.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The HTTP cache and bfcache have different rules.&lt;/p&gt;

&lt;p&gt;Even &lt;code&gt;no-store&lt;/code&gt; isn't a universal bfcache switch across browsers; support and restrictions vary.&lt;/p&gt;

&lt;p&gt;So trying to control bfcache solely through HTTP caching headers is not a reliable strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔵 Let's See What Happens to JavaScript State
&lt;/h2&gt;

&lt;p&gt;Create a simple page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"increment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Increment&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#increment"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"click"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#result"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click the button five times.&lt;/p&gt;

&lt;p&gt;The page displays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now navigate to another page in the same tab, then press Back.&lt;/p&gt;

&lt;p&gt;There are two possible outcomes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario A: Normal navigation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The browser creates a new document and executes the script again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The counter starts at zero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario B: bfcache restoration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The browser restores the preserved document.&lt;/p&gt;

&lt;p&gt;The counter still displays &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;More importantly, the JavaScript variable &lt;code&gt;count&lt;/code&gt; is also still &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Click the button again, and the result becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing had to reconstruct that state.&lt;/p&gt;

&lt;p&gt;The original variable and event listener survived the navigation.&lt;/p&gt;

&lt;p&gt;This is what makes bfcache fundamentally different from restoring a form value or scroll position.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It preserves the JavaScript environment itself.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🟣 The Page Lifecycle Isn't Just Load and Unload
&lt;/h2&gt;

&lt;p&gt;A common mental model looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load page
    ↓
Initialize JavaScript
    ↓
User interacts
    ↓
Navigate away
    ↓
Page destroyed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But bfcache introduces another path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load page
    ↓
Initialize JavaScript
    ↓
User interacts
    ↓
Navigate away
    ↓
Page frozen in bfcache
    ↓
User presses Back
    ↓
Page resumes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice what's missing from the second path?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initialization.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a page is restored from bfcache, its scripts aren't executed from the beginning.&lt;/p&gt;

&lt;p&gt;That means events such as &lt;code&gt;DOMContentLoaded&lt;/code&gt; and &lt;code&gt;load&lt;/code&gt; don't fire again.&lt;/p&gt;

&lt;p&gt;If your application relies exclusively on those events to refresh its state, it won't necessarily do so when the user returns.&lt;/p&gt;

&lt;p&gt;Fortunately, the browser provides lifecycle events specifically useful for this situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟠 Meet &lt;code&gt;pageshow&lt;/code&gt; and &lt;code&gt;pagehide&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;pageshow&lt;/code&gt; event fires when a document is shown, including when it's restored from bfcache.&lt;/p&gt;

&lt;p&gt;Its &lt;code&gt;persisted&lt;/code&gt; property tells you whether that restoration happened.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pageshow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Restored from bfcache"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Normal page show"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's an important distinction here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pageshow&lt;/code&gt; isn't exclusive to bfcache. It also fires during normal page loading.&lt;/p&gt;

&lt;p&gt;It's the &lt;code&gt;persisted&lt;/code&gt; property that tells you whether the document was restored.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;pagehide&lt;/code&gt; fires when the page is being hidden because of navigation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pagehide"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Leaving page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Browser intends to cache this page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there's a catch.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;event.persisted === true&lt;/code&gt; on &lt;code&gt;pagehide&lt;/code&gt; means the browser intends to preserve the page.&lt;/p&gt;

&lt;p&gt;It doesn't guarantee that the page will remain cached.&lt;/p&gt;

&lt;p&gt;The browser can evict it later because of memory pressure or other conditions.&lt;/p&gt;

&lt;p&gt;That's why you should detect an actual restoration through &lt;code&gt;pageshow&lt;/code&gt;, not assume one will happen based on &lt;code&gt;pagehide&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Real Problem: Your Application Might Be Outdated
&lt;/h2&gt;

&lt;p&gt;Let's move beyond a counter.&lt;/p&gt;

&lt;p&gt;Imagine you're building a dashboard for an online store.&lt;/p&gt;

&lt;p&gt;A user opens the inventory page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Mechanical Keyboard
Available stock: 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They navigate to the orders page and process an order for three keyboards.&lt;/p&gt;

&lt;p&gt;The actual stock is now nine.&lt;/p&gt;

&lt;p&gt;Then they press Back.&lt;/p&gt;

&lt;p&gt;If the inventory page is restored from bfcache, it may still display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Available stock: 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because the browser restored the page exactly as it was.&lt;/p&gt;

&lt;p&gt;Your original API request didn't run again.&lt;/p&gt;

&lt;p&gt;Your application's initialization logic didn't run again.&lt;/p&gt;

&lt;p&gt;The browser has no idea that the data on your server changed.&lt;/p&gt;

&lt;p&gt;From its perspective, it's simply restoring an existing document.&lt;/p&gt;

&lt;p&gt;And this isn't limited to inventory dashboards.&lt;/p&gt;

&lt;p&gt;The same problem can affect authentication status, shopping carts, notification counts, account balances, permissions, and other frequently changing information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;bfcache preserves state, not truth.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the trade-off.&lt;/p&gt;

&lt;p&gt;The browser optimizes navigation speed by restoring the previous state, but your application is responsible for deciding whether that state is still valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 Refresh the Data, Not the Entire Page
&lt;/h2&gt;

&lt;p&gt;A tempting solution would be to force a reload whenever the page is restored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pageshow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Technically, that works.&lt;/p&gt;

&lt;p&gt;But it also defeats the performance benefit of bfcache.&lt;/p&gt;

&lt;p&gt;You've taken an almost instantaneous restoration and turned it into a full page load.&lt;/p&gt;

&lt;p&gt;A better approach is to refresh only the information that needs updating.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;refreshInventory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/inventory"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Failed to fetch inventory"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;renderInventory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pageshow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;refreshInventory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the page can reappear immediately while its data is updated.&lt;/p&gt;

&lt;p&gt;The user doesn't lose their scroll position or local UI state simply because the server-side data needs revalidation.&lt;/p&gt;

&lt;p&gt;You can even display the previously available information while the refresh is in progress, provided the UI makes its freshness clear when that matters.&lt;/p&gt;

&lt;p&gt;This is similar to stale-while-revalidate strategies used in data fetching.&lt;/p&gt;

&lt;p&gt;The key idea is that &lt;strong&gt;page restoration and data freshness are separate concerns&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔵 What Happens to Timers and Pending Work?
&lt;/h2&gt;

&lt;p&gt;Here's where things become more interesting.&lt;/p&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Tick"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens when the page enters bfcache?&lt;/p&gt;

&lt;p&gt;Does the interval continue executing in the background?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;The page is frozen. Its ordinary JavaScript execution is suspended while it remains cached.&lt;/p&gt;

&lt;p&gt;When the page is restored, its JavaScript environment resumes.&lt;/p&gt;

&lt;p&gt;But don't assume that every second spent away corresponds to an interval callback.&lt;/p&gt;

&lt;p&gt;If your application uses a counter to measure elapsed time, this can cause incorrect results.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;elapsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;elapsed&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This isn't a reliable way to measure real elapsed time across page suspension.&lt;/p&gt;

&lt;p&gt;Instead, calculate the difference between timestamps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;startedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getElapsedSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;startedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the calculation accounts for wall-clock time that passed while the page wasn't executing.&lt;/p&gt;

&lt;p&gt;The broader lesson applies to animation loops, polling, scheduled work, and other time-dependent behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A preserved JavaScript environment doesn't mean JavaScript was continuously running.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And some in-flight work, such as certain network operations, can affect whether a page is eligible for bfcache in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟣 Can Every Page Use bfcache?
&lt;/h2&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;The browser decides whether a page is eligible.&lt;/p&gt;

&lt;p&gt;That decision depends on browser implementation, document behavior, available memory, and other conditions.&lt;/p&gt;

&lt;p&gt;One historically important example is the &lt;code&gt;unload&lt;/code&gt; event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"unload"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Goodbye!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This event is unreliable during navigation, and its presence can prevent bfcache eligibility in some browsers.&lt;/p&gt;

&lt;p&gt;Modern browser guidance generally favors avoiding &lt;code&gt;unload&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you need to observe a page being left, &lt;code&gt;pagehide&lt;/code&gt; is usually a better choice.&lt;/p&gt;

&lt;p&gt;If you need to save application state as the user switches tabs or leaves, &lt;code&gt;visibilitychange&lt;/code&gt; is often more appropriate.&lt;/p&gt;

&lt;p&gt;These events aren't interchangeable, though.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;visibilitychange&lt;/code&gt; can happen when the user simply switches tabs, without navigating anywhere.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pagehide&lt;/code&gt; is associated with the document being hidden during navigation.&lt;/p&gt;

&lt;p&gt;And neither should be treated as a guaranteed opportunity to finish arbitrary asynchronous work.&lt;/p&gt;

&lt;p&gt;Another important point: &lt;strong&gt;you can't force the browser to cache your page&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can make your application compatible with bfcache, but the browser ultimately decides whether to preserve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟠 How to Test bfcache in Chrome
&lt;/h2&gt;

&lt;p&gt;You don't have to guess whether your page is being restored.&lt;/p&gt;

&lt;p&gt;Chrome DevTools includes a dedicated bfcache testing tool.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the page in Chrome.&lt;/li&gt;
&lt;li&gt;Open DevTools.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;Application → Background services → Back/forward cache&lt;/strong&gt; (the exact location can vary by Chrome version).&lt;/li&gt;
&lt;li&gt;Run the back/forward cache test.&lt;/li&gt;
&lt;li&gt;Inspect whether the page was restored and, if not, the reported reasons.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also inspect navigation timing information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;navigation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getEntriesByType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"navigation"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;navigation&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But be careful: a value of &lt;code&gt;"back_forward"&lt;/code&gt; indicates a history navigation, &lt;strong&gt;not necessarily a bfcache restoration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A history navigation can still involve loading a new document.&lt;/p&gt;

&lt;p&gt;For a direct restoration check, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pageshow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"From bfcache:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the distinction worth remembering when debugging.&lt;/p&gt;

&lt;p&gt;Going Back and being restored from bfcache are not synonymous.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The Takeaway
&lt;/h2&gt;

&lt;p&gt;The Back button isn't necessarily telling the browser to load a previous URL again.&lt;/p&gt;

&lt;p&gt;Sometimes it's telling the browser to &lt;strong&gt;resume an entire page that never got destroyed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's why a page can return instantly with its DOM, JavaScript variables, event listeners, and user interactions intact.&lt;/p&gt;

&lt;p&gt;But it also means developers need to think about a lifecycle that doesn't always begin with initialization.&lt;/p&gt;

&lt;p&gt;A page can become visible without running its startup code again.&lt;/p&gt;

&lt;p&gt;Its data can be stale even though its UI looks perfectly restored.&lt;/p&gt;

&lt;p&gt;And its JavaScript can resume after being frozen for an unknown amount of time.&lt;/p&gt;

&lt;p&gt;The goal isn't to fight bfcache or force every navigation to reload.&lt;/p&gt;

&lt;p&gt;It's to build pages that behave correctly whether they're loaded from scratch or resumed from memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Because sometimes the fastest page load is the one that never happens.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 See bfcache in Action
&lt;/h2&gt;

&lt;p&gt;Want to see what actually happens when your browser restores a page instead of reloading it?&lt;/p&gt;

&lt;p&gt;Try the &lt;a href="https://dev.to/demo/bfcache/"&gt;interactive bfcache demo&lt;/a&gt;. Increment the counter, navigate to Page B, wait a few seconds, and press Back.&lt;/p&gt;

&lt;p&gt;If bfcache was used, you'll see that your React state survived, JavaScript timers were suspended, and &lt;code&gt;pageshow.persisted&lt;/code&gt; reports &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then try a normal reload and compare the results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Same page. Same code. Completely different navigation behavior.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>browser</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Would You Rather Have a Dumb Component or a Smart Component?</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Sat, 31 Oct 2026 21:13:37 +0000</pubDate>
      <link>https://dev.to/bytecorner/would-you-rather-have-a-dumb-component-or-a-smart-component-3j65</link>
      <guid>https://dev.to/bytecorner/would-you-rather-have-a-dumb-component-or-a-smart-component-3j65</guid>
      <description>&lt;h1&gt;
  
  
  A Component Doesn't Have to Be Dumb to Be Reusable
&lt;/h1&gt;

&lt;p&gt;There's a long-running idea in frontend architecture:&lt;/p&gt;

&lt;p&gt;Keep components dumb.&lt;/p&gt;

&lt;p&gt;Let them receive data, render the UI, and leave everything else to something higher up the tree.&lt;/p&gt;

&lt;p&gt;It sounds clean. And often, it is.&lt;/p&gt;

&lt;p&gt;But there's a trade-off we don't always consider:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making a component simpler can make the surrounding architecture more complicated.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's look at both approaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine a component that displays a list of users.&lt;/p&gt;

&lt;p&gt;It needs to fetch the data, filter the results, and render them.&lt;/p&gt;

&lt;p&gt;One approach is to put everything inside &lt;code&gt;UserList&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UserList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUsers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setQuery&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setUsers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filteredUsers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a smart component.&lt;/p&gt;

&lt;p&gt;It knows where its data comes from, manages its own state, handles filtering, and decides how everything is displayed.&lt;/p&gt;

&lt;p&gt;Using it is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserList&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what happens when another part of the application already has the users?&lt;/p&gt;

&lt;p&gt;Or when the same UI needs to display data from a different source?&lt;/p&gt;

&lt;p&gt;Now the component's built-in behavior becomes a limitation.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 The Other Extreme
&lt;/h2&gt;

&lt;p&gt;We could separate those responsibilities.&lt;/p&gt;

&lt;p&gt;The container handles the data. The presentational component handles the UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UserListContainer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUsers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setUsers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserList&lt;/span&gt; &lt;span class="na"&gt;users&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UserList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;users&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;UserList&lt;/code&gt; doesn't care where the data comes from.&lt;/p&gt;

&lt;p&gt;It can render users fetched by a parent, loaded from a cache, or passed in by a test.&lt;/p&gt;

&lt;p&gt;That's a real benefit.&lt;/p&gt;

&lt;p&gt;But splitting components isn't free.&lt;/p&gt;

&lt;p&gt;The container needs to pass data down. Additional behavior may require additional props. And if the presentation component is only ever used in one place, the extra layer might not make the code any easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We haven't necessarily removed complexity. We've moved it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔵 Reusability Has a Cost
&lt;/h2&gt;

&lt;p&gt;A dumb component is often easier to reuse because it depends on fewer things.&lt;/p&gt;

&lt;p&gt;It doesn't need to know about an API, a router, a global store, or a particular data-fetching library.&lt;/p&gt;

&lt;p&gt;But reusability isn't the only thing that matters.&lt;/p&gt;

&lt;p&gt;Sometimes, keeping behavior close to the UI makes a component easier to understand.&lt;/p&gt;

&lt;p&gt;Consider a search box.&lt;/p&gt;

&lt;p&gt;If its value, filtering logic, and displayed results always belong together, extracting every small piece into separate components and hooks can make a simple feature harder to follow.&lt;/p&gt;

&lt;p&gt;On the other hand, if multiple screens need the same filtering behavior or visual list, separating those responsibilities may be worthwhile.&lt;/p&gt;

&lt;p&gt;The useful boundary isn't necessarily between smart and dumb components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's between responsibilities that belong together and responsibilities that benefit from being independent.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🟣 So Where Should the Logic Live?
&lt;/h2&gt;

&lt;p&gt;There isn't one rule that works for every component.&lt;/p&gt;

&lt;p&gt;A few questions can help:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Does the component need to work with different data sources?&lt;/strong&gt; Separating data access from presentation may help.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Does the behavior only make sense for this component?&lt;/strong&gt; Keeping it local may make the code clearer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Is the component coordinating several unrelated concerns?&lt;/strong&gt; That may be a sign it has too many responsibilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Are you extracting something just to make a component smaller?&lt;/strong&gt; Smaller doesn't automatically mean simpler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also mix the approaches.&lt;/p&gt;

&lt;p&gt;A component can own its local interaction state while receiving data from a parent. It can use a shared hook for behavior without becoming purely presentational.&lt;/p&gt;

&lt;p&gt;These aren't mutually exclusive architectures.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟠 What About Consistency Across a Codebase?
&lt;/h2&gt;

&lt;p&gt;So far, we've looked at individual components.&lt;/p&gt;

&lt;p&gt;But components don't always exist in isolation. They're part of a larger codebase, often maintained by multiple developers.&lt;/p&gt;

&lt;p&gt;Imagine a team decides that components should follow a consistent architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Components don't fetch data directly.&lt;/li&gt;
&lt;li&gt;Shared state is accessed through established abstractions.&lt;/li&gt;
&lt;li&gt;Business logic lives in hooks, services, or other dedicated modules.&lt;/li&gt;
&lt;li&gt;Components receive the data they need through props.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are good reasons to do this.&lt;/p&gt;

&lt;p&gt;Developers know where to look for things. Responsibilities are predictable. Moving between different parts of the application feels familiar.&lt;/p&gt;

&lt;p&gt;But there's a trade-off.&lt;/p&gt;

&lt;p&gt;Some components might genuinely be simpler if they handled their own behavior.&lt;/p&gt;

&lt;p&gt;Forcing them into the same structure could mean creating unnecessary hooks, adding extra layers, or splitting closely related logic across several files.&lt;/p&gt;

&lt;p&gt;You gain consistency, but potentially lose simplicity.&lt;/p&gt;

&lt;p&gt;And that's not necessarily a bad decision.&lt;/p&gt;

&lt;p&gt;In a large team, predictable code can be more valuable than making every individual component follow its locally simplest implementation.&lt;/p&gt;

&lt;p&gt;The important distinction is between &lt;strong&gt;consistent principles and identical implementations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A codebase can have clear architectural rules without forcing every component to have exactly the same structure.&lt;/p&gt;

&lt;p&gt;After all, you're maintaining a codebase, not just individual components.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The Takeaway
&lt;/h2&gt;

&lt;p&gt;Smart and dumb components aren't competing goals.&lt;/p&gt;

&lt;p&gt;A dumb component can be wonderfully reusable, but separating every bit of behavior can create unnecessary indirection.&lt;/p&gt;

&lt;p&gt;A smart component can be convenient and cohesive, but accumulating unrelated responsibilities can make it difficult to change.&lt;/p&gt;

&lt;p&gt;Neither approach is inherently better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't optimize for dumb components. Don't optimize for smart components.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimize for clear responsibilities.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>uiux</category>
      <category>ui</category>
      <category>webdev</category>
    </item>
    <item>
      <title>You Deleted the Array Element. Except You Didn't.</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Mon, 05 Oct 2026 19:22:16 +0000</pubDate>
      <link>https://dev.to/bytecorner/you-deleted-the-array-element-except-you-didnt-1d3g</link>
      <guid>https://dev.to/bytecorner/you-deleted-the-array-element-except-you-didnt-1d3g</guid>
      <description>&lt;h1&gt;
  
  
  The Array Still Has Three Elements
&lt;/h1&gt;

&lt;p&gt;In Javascript, &lt;code&gt;delete&lt;/code&gt; looks like a natural way to remove something from an array.&lt;/p&gt;

&lt;p&gt;You have an element at index &lt;code&gt;1&lt;/code&gt;. You delete it. Surely the array becomes shorter.&lt;/p&gt;

&lt;p&gt;Not quite.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;delete&lt;/code&gt; removes the property at that index. The array itself stays the same length, leaving a hole behind.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might expect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Alice", "Charlie"]
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, the array still has a &lt;code&gt;length&lt;/code&gt; of &lt;code&gt;3&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Alice", empty, "Charlie"]
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is that index &lt;code&gt;1&lt;/code&gt; is now missing.&lt;/p&gt;

&lt;p&gt;You can prove it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The array did not shift &lt;code&gt;"Charlie"&lt;/code&gt; from index &lt;code&gt;2&lt;/code&gt; to index &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You removed the property at index &lt;code&gt;1&lt;/code&gt; and left a hole where it used to be.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 The Solution
&lt;/h2&gt;

&lt;p&gt;If you mean "remove this element and shift everything after it," use &lt;code&gt;splice()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the result is what you probably expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Alice", "Charlie"]
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;splice()&lt;/code&gt; actually changes the array by removing the element and moving the later elements down.&lt;/p&gt;

&lt;p&gt;If you do not want to mutate the original array, modern JavaScript also gives you &lt;code&gt;toSpliced()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toSpliced&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;users&lt;/code&gt; stays unchanged, while &lt;code&gt;remaining&lt;/code&gt; contains the shorter array.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The Part That Bites
&lt;/h2&gt;

&lt;p&gt;The really interesting part is that a hole is not the same as &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compare these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading index &lt;code&gt;1&lt;/code&gt; gives &lt;code&gt;undefined&lt;/code&gt; in both cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the index exists in &lt;code&gt;a&lt;/code&gt; and does not exist in &lt;code&gt;b&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the same difference with &lt;code&gt;Object.keys()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["0", "1", "2"]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["0", "2"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That difference can also show up when you iterate.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;forEach()&lt;/code&gt; skips the hole entirely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice
Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no callback for the missing index.&lt;/p&gt;

&lt;p&gt;That is where a seemingly harmless &lt;code&gt;delete&lt;/code&gt; can become a debugging headache. The array can still have the same length, but code iterating over it may no longer see every index you think is there.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The Takeaway
&lt;/h2&gt;

&lt;p&gt;Arrays are objects with numeric properties and a &lt;code&gt;length&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;So when you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you are deleting a property. You are not asking JavaScript to remove an element and close the gap.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete      -&amp;gt; remove the property at that index
splice      -&amp;gt; remove the element and shift the array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you need an immutable version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toSpliced   -&amp;gt; create a new array without the element
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dangerous part is not that &lt;code&gt;delete&lt;/code&gt; fails.&lt;/p&gt;

&lt;p&gt;It does exactly what JavaScript asked it to do.&lt;/p&gt;

&lt;p&gt;The problem is that what JavaScript means by "delete this array element" is probably not what you meant.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>An Optional Property Isn't the Same as undefined</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Mon, 05 Oct 2026 17:20:58 +0000</pubDate>
      <link>https://dev.to/bytecorner/an-optional-property-isnt-the-same-as-undefined-3aad</link>
      <guid>https://dev.to/bytecorner/an-optional-property-isnt-the-same-as-undefined-3aad</guid>
      <description>&lt;h1&gt;
  
  
  &lt;code&gt;undefined&lt;/code&gt; Is Not the Same as Not Being There
&lt;/h1&gt;

&lt;p&gt;An optional property looks a lot like a property whose value can be &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But they are not quite the same thing.&lt;/p&gt;

&lt;p&gt;That difference becomes important when you care about whether a property exists at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;Consider these two objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At a glance, they can look equivalent.&lt;/p&gt;

&lt;p&gt;Reading &lt;code&gt;value&lt;/code&gt; gives you &lt;code&gt;undefined&lt;/code&gt; in both cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the objects are different.&lt;/p&gt;

&lt;p&gt;One does not have a &lt;code&gt;value&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;The other does.&lt;/p&gt;

&lt;p&gt;You can see that with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;false
true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That distinction is easy to miss because normal property access hides it.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 The TypeScript Part
&lt;/h2&gt;

&lt;p&gt;Now look at this type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;?&lt;/code&gt; means the property may be absent.&lt;/p&gt;

&lt;p&gt;So this is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is also valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on your TypeScript configuration, this can also be valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But those last two objects still represent different runtime states.&lt;/p&gt;

&lt;p&gt;The first has a value.&lt;/p&gt;

&lt;p&gt;The second has a property whose value is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That difference matters when the presence of the property itself has meaning.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The Part That Usually Bites
&lt;/h2&gt;

&lt;p&gt;Imagine an API where omitting a property means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Don't change this setting."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while explicitly passing &lt;code&gt;undefined&lt;/code&gt; has a different meaning.&lt;/p&gt;

&lt;p&gt;Or imagine code that checks whether a property was supplied:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// the caller supplied the property&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now these two values are no longer interchangeable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type might make them look similar.&lt;/p&gt;

&lt;p&gt;The runtime object does not.&lt;/p&gt;

&lt;p&gt;This becomes especially interesting when objects are passed between functions, merged together, serialized, or used as configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The TypeScript Detail
&lt;/h2&gt;

&lt;p&gt;There is a compiler option specifically related to this distinction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exactOptionalPropertyTypes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this enabled, an optional property means the property can be omitted.&lt;/p&gt;

&lt;p&gt;It does not automatically mean you can explicitly assign &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;allows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this becomes an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you actually want to allow the property to exist with &lt;code&gt;undefined&lt;/code&gt;, say so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the type describes both possibilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;property is absent
        OR
property exists with undefined
        OR
property exists with a string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a much more precise description of the object.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧩 When Could This Be Useful?
&lt;/h2&gt;

&lt;p&gt;This distinction is especially useful for configuration and update objects.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An update function might interpret an omitted property as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Leave the existing value alone."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while a present property could mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Change this field."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That makes property presence part of the API's meaning.&lt;/p&gt;

&lt;p&gt;The same idea appears when dealing with object merging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;defaults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The property was not missing.&lt;/p&gt;

&lt;p&gt;It was explicitly present.&lt;/p&gt;

&lt;p&gt;So the resulting object contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That can be very different from simply not providing &lt;code&gt;color&lt;/code&gt; at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The Takeaway
&lt;/h2&gt;

&lt;p&gt;An optional property is about &lt;strong&gt;presence&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;undefined&lt;/code&gt; is about &lt;strong&gt;value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Those two concepts often produce the same result when you simply read the property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But they can represent different states.&lt;/p&gt;

&lt;p&gt;Once your application cares about whether something was omitted or explicitly provided, that difference stops being a TypeScript technicality.&lt;/p&gt;

&lt;p&gt;It becomes part of the API design.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;value?: string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;does not simply mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"value can be undefined"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It starts with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"this property may not exist"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And sometimes, that is exactly the distinction your code needs.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>programming</category>
    </item>
    <item>
      <title>What Makes Something Worth Understanding?</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Mon, 05 Oct 2026 14:52:28 +0000</pubDate>
      <link>https://dev.to/bytecorner/what-makes-something-worth-understanding-351c</link>
      <guid>https://dev.to/bytecorner/what-makes-something-worth-understanding-351c</guid>
      <description>&lt;h1&gt;
  
  
  The Question That Matters
&lt;/h1&gt;

&lt;p&gt;Some things are useful because they solve a problem.&lt;/p&gt;

&lt;p&gt;Others are worth understanding even when you are not going to use them tomorrow.&lt;/p&gt;

&lt;p&gt;That sounds obvious, but it is easy to forget when learning software development. We tend to measure everything by usefulness. Will this help me write better code? Will I use it at work? Is this worth remembering?&lt;/p&gt;

&lt;p&gt;Sometimes the answer is simply: it is interesting.&lt;/p&gt;

&lt;p&gt;And I think that is a perfectly good reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not everything needs an immediate use case
&lt;/h2&gt;

&lt;p&gt;Take something like CSS margin collapsing.&lt;/p&gt;

&lt;p&gt;You can go years without deliberately using it. You might even work around it without knowing exactly what is happening.&lt;/p&gt;

&lt;p&gt;But once you understand it, a strange behavior stops being strange.&lt;/p&gt;

&lt;p&gt;That matters.&lt;/p&gt;

&lt;p&gt;Understanding something gives you a better model of how the thing works. You do not need to use that knowledge every day for it to be valuable.&lt;/p&gt;

&lt;p&gt;The same applies to things like how JavaScript evaluates expressions, how a browser calculates layout, or why a particular TypeScript type behaves the way it does.&lt;/p&gt;

&lt;p&gt;You might not need the answer today.&lt;/p&gt;

&lt;p&gt;But the next time you encounter the problem, you already have a place to start.&lt;/p&gt;

&lt;p&gt;More importantly, you start recognizing similar problems.&lt;/p&gt;

&lt;p&gt;A weird layout issue might not look exactly like the one you saw before. A TypeScript error might happen in a completely different part of the application. A JavaScript behavior might appear in an API you have never used.&lt;/p&gt;

&lt;p&gt;The details change.&lt;/p&gt;

&lt;p&gt;The mental model stays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful is not the same as valuable
&lt;/h2&gt;

&lt;p&gt;There is a difference between learning something because you need it and learning something because it makes the system make more sense.&lt;/p&gt;

&lt;p&gt;The first gets you moving.&lt;/p&gt;

&lt;p&gt;The second changes how you see things.&lt;/p&gt;

&lt;p&gt;That distinction is important because software is full of behavior that is technically easy to ignore.&lt;/p&gt;

&lt;p&gt;You can memorize that something happens.&lt;/p&gt;

&lt;p&gt;You can also understand why it happens.&lt;/p&gt;

&lt;p&gt;The second one tends to stick.&lt;/p&gt;

&lt;p&gt;Think about debugging.&lt;/p&gt;

&lt;p&gt;You can memorize a fix for a particular problem. Add this property. Move this code. Await this promise. Add this check. Restart the development server.&lt;/p&gt;

&lt;p&gt;Sometimes that is all you need.&lt;/p&gt;

&lt;p&gt;But if you understand the reason behind the fix, the next problem becomes easier.&lt;/p&gt;

&lt;p&gt;You are no longer searching for the same solution. You are looking for the same kind of problem.&lt;/p&gt;

&lt;p&gt;That is a much more useful thing to carry around.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding gives you better questions
&lt;/h2&gt;

&lt;p&gt;There is another benefit that is easy to miss.&lt;/p&gt;

&lt;p&gt;Once you understand how something works, you start asking better questions.&lt;/p&gt;

&lt;p&gt;Before you understand CSS layout, the question might be:&lt;/p&gt;

&lt;p&gt;Why is this element doing this? Why is this property doing that?&lt;/p&gt;

&lt;p&gt;After you understand the layout model, the question becomes:&lt;/p&gt;

&lt;p&gt;Which constraint is causing this result?&lt;/p&gt;

&lt;p&gt;That is a completely different debugging process.&lt;/p&gt;

&lt;p&gt;The same thing happens with asynchronous JavaScript.&lt;/p&gt;

&lt;p&gt;Instead of asking:&lt;/p&gt;

&lt;p&gt;Why did this happen after that?&lt;/p&gt;

&lt;p&gt;You start asking:&lt;/p&gt;

&lt;p&gt;When was this callback scheduled, and what state did it capture?&lt;/p&gt;

&lt;p&gt;Or with TypeScript:&lt;/p&gt;

&lt;p&gt;Why does TypeScript hate this?&lt;/p&gt;

&lt;p&gt;becomes:&lt;/p&gt;

&lt;p&gt;Where did the type information disappear?&lt;/p&gt;

&lt;p&gt;Those questions are more specific.&lt;/p&gt;

&lt;p&gt;They give you somewhere to look.&lt;/p&gt;

&lt;p&gt;Understanding does not just give you answers. It changes the questions you ask when you do not have an answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  You do not have to remember everything
&lt;/h2&gt;

&lt;p&gt;This is also why I do not think learning something requires memorizing every detail.&lt;/p&gt;

&lt;p&gt;There is a difference between knowing something by heart and understanding it well enough to find your way back to it.&lt;/p&gt;

&lt;p&gt;You will forget syntax.&lt;/p&gt;

&lt;p&gt;You will forget exact APIs.&lt;/p&gt;

&lt;p&gt;You will forget edge cases.&lt;/p&gt;

&lt;p&gt;That is normal.&lt;/p&gt;

&lt;p&gt;What tends to survive is the model.&lt;/p&gt;

&lt;p&gt;You remember that CSS has different ways of calculating sizes. You remember that promises have their own scheduling behavior. You remember that TypeScript is tracking information that may disappear at certain boundaries.&lt;/p&gt;

&lt;p&gt;The next time you need the details, you can look them up.&lt;/p&gt;

&lt;p&gt;But you know what you are looking for.&lt;/p&gt;

&lt;p&gt;That is often enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rabbit hole is sometimes the point
&lt;/h2&gt;

&lt;p&gt;This is one of the reasons I like digging into seemingly small technical details.&lt;/p&gt;

&lt;p&gt;A question starts simple.&lt;/p&gt;

&lt;p&gt;Why did this element move?&lt;/p&gt;

&lt;p&gt;Why did this promise resolve to that value?&lt;/p&gt;

&lt;p&gt;Why does this type behave differently here?&lt;/p&gt;

&lt;p&gt;Why did this request still change the UI even though it failed?&lt;/p&gt;

&lt;p&gt;The answer is often not immediately useful.&lt;/p&gt;

&lt;p&gt;But following the answer usually reveals something about the system that was hidden before.&lt;/p&gt;

&lt;p&gt;And sometimes that is enough.&lt;/p&gt;

&lt;p&gt;A small question can lead somewhere much bigger.&lt;/p&gt;

&lt;p&gt;You start with one strange behavior and end up understanding part of the larger algorithm.&lt;/p&gt;

&lt;p&gt;You start with one confusing error and end up understanding how information is lost or preserved.&lt;/p&gt;

&lt;p&gt;You start with one bug and end up understanding why the order of events in your application is not always the order the code appears in.&lt;/p&gt;

&lt;p&gt;The original problem might never happen again.&lt;/p&gt;

&lt;p&gt;The understanding still stays with you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Curiosity is a useful engineering skill
&lt;/h2&gt;

&lt;p&gt;There is a temptation to treat curiosity as something separate from practical engineering.&lt;/p&gt;

&lt;p&gt;It is not.&lt;/p&gt;

&lt;p&gt;Curiosity is often what makes you stop when something feels slightly wrong.&lt;/p&gt;

&lt;p&gt;You could accept that the code works.&lt;/p&gt;

&lt;p&gt;Instead, you ask why.&lt;/p&gt;

&lt;p&gt;You could copy the fix from a search result.&lt;/p&gt;

&lt;p&gt;Instead, you try to understand what the fix is actually changing.&lt;/p&gt;

&lt;p&gt;You could move on after finding the answer.&lt;/p&gt;

&lt;p&gt;Instead, you follow the next question.&lt;/p&gt;

&lt;p&gt;That does not mean every rabbit hole is worth following.&lt;/p&gt;

&lt;p&gt;Some are not.&lt;/p&gt;

&lt;p&gt;You can spend an afternoon learning something that will never matter to your work. That is fine too, sometimes. But there is a difference between curiosity and collecting trivia.&lt;/p&gt;

&lt;p&gt;The useful kind of curiosity changes your mental model.&lt;/p&gt;

&lt;p&gt;You come out of the rabbit hole seeing something differently than you did before.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, is it worth understanding?
&lt;/h2&gt;

&lt;p&gt;I think a good question is not only:&lt;/p&gt;

&lt;p&gt;"Will I use this?"&lt;/p&gt;

&lt;p&gt;It is also:&lt;/p&gt;

&lt;p&gt;"Will understanding this change how I think about the thing?"&lt;/p&gt;

&lt;p&gt;If the answer is yes, it probably belongs in the rabbit hole.&lt;/p&gt;

&lt;p&gt;Maybe it will help you debug something six months from now.&lt;/p&gt;

&lt;p&gt;Maybe it will make a confusing API suddenly make sense.&lt;/p&gt;

&lt;p&gt;Maybe it will help you recognize a bug before you write it.&lt;/p&gt;

&lt;p&gt;Or maybe you will never use the knowledge directly.&lt;/p&gt;

&lt;p&gt;You will just understand a little more about how the thing works.&lt;/p&gt;

&lt;p&gt;That is enough of a reason.&lt;/p&gt;

&lt;p&gt;Not everything you learn has to pay for itself immediately.&lt;/p&gt;

&lt;p&gt;Sometimes understanding is the useful part.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>There Is No Such Thing as an Edge Case. Or is there?</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Fri, 02 Oct 2026 11:42:45 +0000</pubDate>
      <link>https://dev.to/bytecorner/there-is-no-such-thing-as-an-edge-case-or-is-there-13a</link>
      <guid>https://dev.to/bytecorner/there-is-no-such-thing-as-an-edge-case-or-is-there-13a</guid>
      <description>&lt;h2&gt;
  
  
  There Is No Such Thing as an Edge Case. Or is there?
&lt;/h2&gt;

&lt;p&gt;"That's just an edge case."&lt;/p&gt;

&lt;p&gt;You've probably heard it after someone finds a weird input, an unexpected state, or a combination of events nobody thought would happen.&lt;/p&gt;

&lt;p&gt;Sometimes that's a perfectly reasonable description.&lt;/p&gt;

&lt;p&gt;Sometimes it's how a bug gets dismissed.&lt;/p&gt;

&lt;p&gt;The interesting question is not whether something is unusual.&lt;/p&gt;

&lt;p&gt;It's whether &lt;strong&gt;your software needs to handle it&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a system that accepts a username.&lt;/p&gt;

&lt;p&gt;You expect something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;matej
alex
sarah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then someone enters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is that an edge case?&lt;/p&gt;

&lt;p&gt;Probably.&lt;/p&gt;

&lt;p&gt;Now imagine someone enters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's also a normal-looking username.&lt;/p&gt;

&lt;p&gt;But if your system gives special privileges to &lt;code&gt;admin&lt;/code&gt; because of an assumption hidden somewhere else, you've got a very different problem.&lt;/p&gt;

&lt;p&gt;Neither input looks particularly strange.&lt;/p&gt;

&lt;p&gt;The important difference is the assumption your system makes about it.&lt;/p&gt;

&lt;p&gt;That's where "edge cases" get interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 The Solution
&lt;/h2&gt;

&lt;p&gt;Instead of thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How likely is this to happen?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;try asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"What does the system need to do if this happens?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Those are different questions.&lt;/p&gt;

&lt;p&gt;A user entering 500 characters might be extremely rare.&lt;/p&gt;

&lt;p&gt;But if the application accepts user input, someone eventually will.&lt;/p&gt;

&lt;p&gt;A network request taking 10 seconds might be unusual.&lt;/p&gt;

&lt;p&gt;But if your application depends on a network request, your code still needs a behavior for it.&lt;/p&gt;

&lt;p&gt;A list containing zero items might seem like an edge case.&lt;/p&gt;

&lt;p&gt;But if an empty list is a perfectly valid result, it's not really an edge case at all.&lt;/p&gt;

&lt;p&gt;It's just another valid state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;showEmptyState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The empty state isn't "special handling" if an empty result is part of the system's normal contract.&lt;/p&gt;

&lt;p&gt;It's the contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The "Why"
&lt;/h2&gt;

&lt;p&gt;The phrase "edge case" can make something sound like it belongs outside the main problem.&lt;/p&gt;

&lt;p&gt;But software doesn't care how unusual a situation sounds.&lt;/p&gt;

&lt;p&gt;It cares whether the situation is &lt;strong&gt;inside or outside the behavior you've decided to support&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Someone might say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What if &lt;code&gt;count&lt;/code&gt; is zero? That's an edge case."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But if &lt;code&gt;count&lt;/code&gt; can actually be zero, then the function has a decision to make.&lt;/p&gt;

&lt;p&gt;Should it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;throw?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;count must be greater than zero&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or produce some other defined result?&lt;/p&gt;

&lt;p&gt;The interesting part isn't which decision you make.&lt;/p&gt;

&lt;p&gt;The interesting part is that &lt;strong&gt;not making a decision is also a decision&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The bug usually appears when the code has an assumption but nobody realized it was an assumption.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧩 The Boundary Moves
&lt;/h2&gt;

&lt;p&gt;Here's the uncomfortable part.&lt;/p&gt;

&lt;p&gt;What counts as an edge case can change.&lt;/p&gt;

&lt;p&gt;A system might originally have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1–10 items
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and treat 100 items as unusual.&lt;/p&gt;

&lt;p&gt;Then the product grows.&lt;/p&gt;

&lt;p&gt;Now customers regularly import 500 items.&lt;/p&gt;

&lt;p&gt;The input didn't change.&lt;/p&gt;

&lt;p&gt;The system's expectations did.&lt;/p&gt;

&lt;p&gt;What used to be an edge case is now normal traffic.&lt;/p&gt;

&lt;p&gt;That's why good software isn't built by trying to predict every bizarre thing that could ever happen.&lt;/p&gt;

&lt;p&gt;It's built by making important assumptions explicit.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nf"&gt;renderUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you might decide that an empty collection is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;renderEmptyState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;renderUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the behavior is deliberate.&lt;/p&gt;

&lt;p&gt;The code isn't merely hoping the assumption stays true.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The Takeaway
&lt;/h2&gt;

&lt;p&gt;There isn't a universal list of edge cases.&lt;/p&gt;

&lt;p&gt;An edge case is usually a situation that sits near the boundary of what your system expects to handle.&lt;/p&gt;

&lt;p&gt;And those boundaries depend on the system.&lt;/p&gt;

&lt;p&gt;So when someone says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"That's just an edge case."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;the useful follow-up isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How likely is it?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is this a state our software is supposed to handle?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is yes, it isn't outside the problem.&lt;/p&gt;

&lt;p&gt;It's part of the problem.&lt;/p&gt;

&lt;p&gt;And if the answer is no, that's worth making explicit too.&lt;/p&gt;

&lt;p&gt;Because an edge case isn't necessarily something rare.&lt;/p&gt;

&lt;p&gt;Sometimes it's simply &lt;strong&gt;an assumption that hasn't been turned into a decision yet&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
      <category>software</category>
    </item>
    <item>
      <title>`async` Is Built on an Idea You Can See in `yield`</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Fri, 02 Oct 2026 11:42:35 +0000</pubDate>
      <link>https://dev.to/bytecorner/async-is-built-on-an-idea-you-can-see-in-yield-1mki</link>
      <guid>https://dev.to/bytecorner/async-is-built-on-an-idea-you-can-see-in-yield-1mki</guid>
      <description>&lt;h1&gt;
  
  
  async Is Built on an Idea You Can See in &lt;code&gt;yield&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; aren't the same thing.&lt;/p&gt;

&lt;p&gt;A generator isn't an async function, and an async function isn't a generator.&lt;/p&gt;

&lt;p&gt;But there is an idea hiding underneath both of them that is easy to miss:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;execution can stop here and continue later.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you see that, &lt;code&gt;async/await&lt;/code&gt; starts looking a lot less magical.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;Consider a generator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing has run yet.&lt;/p&gt;

&lt;p&gt;Now call &lt;code&gt;next()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// A&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generator runs until it reaches &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then it stops.&lt;/p&gt;

&lt;p&gt;It hasn't finished. It hasn't returned.&lt;/p&gt;

&lt;p&gt;It's &lt;strong&gt;suspended&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Call &lt;code&gt;next()&lt;/code&gt; again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution continues exactly where it stopped.&lt;/p&gt;

&lt;p&gt;That's unusual if you're used to ordinary functions.&lt;/p&gt;

&lt;p&gt;Normally, you think of a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start → run → finish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A generator gives you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start → run → suspend → resume → finish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that middle part is the interesting bit.&lt;/p&gt;

&lt;p&gt;If you want to see the whole thing together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// A&lt;/span&gt;
&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first next() runs until yield.&lt;br&gt;&lt;br&gt;
The second next() resumes the generator and continues after yield.&lt;/p&gt;
&lt;h2&gt;
  
  
  🟢 The Idea
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; makes suspension easy to see.&lt;/p&gt;

&lt;p&gt;The generator essentially says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I'm stopping here. You can have control for a while. I'll continue later."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The caller decides when that happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// runs until yield&lt;/span&gt;

&lt;span class="c1"&gt;// do something else&lt;/span&gt;

&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// continue after yield&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing about this requires the generator to block JavaScript.&lt;/p&gt;

&lt;p&gt;The generator's execution is paused while the rest of the program can continue.&lt;/p&gt;

&lt;p&gt;That idea turns out to be extremely useful for asynchronous programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔵 Promises Change Who Controls the Resume
&lt;/h2&gt;

&lt;p&gt;Promises give us another way to express "continue later."&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;async/await&lt;/code&gt;, you might write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting part isn't really &lt;code&gt;.then()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's what the code is saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start something
       ↓
   not ready yet
       ↓
   continue later
       ↓
    use(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function doing the work doesn't need to sit around waiting for the result.&lt;/p&gt;

&lt;p&gt;Instead, the continuation is registered and runs when the Promise settles.&lt;/p&gt;

&lt;p&gt;This is conceptually similar to what we saw with &lt;code&gt;yield&lt;/code&gt;, even though the mechanisms are completely different.&lt;/p&gt;

&lt;p&gt;With a generator, you explicitly resume the computation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a Promise, settlement allows the continuation to be scheduled.&lt;/p&gt;

&lt;p&gt;Different machinery.&lt;/p&gt;

&lt;p&gt;Same useful idea:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;this computation can suspend while something else happens.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🟣 Then &lt;code&gt;await&lt;/code&gt; Makes It Look Normal
&lt;/h2&gt;

&lt;p&gt;Now look at the same kind of operation using &lt;code&gt;async/await&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks almost like ordinary synchronous code.&lt;/p&gt;

&lt;p&gt;That's intentional.&lt;/p&gt;

&lt;p&gt;But the function isn't actually blocking at &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When JavaScript reaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the async function suspends.&lt;/p&gt;

&lt;p&gt;JavaScript gets control back.&lt;/p&gt;

&lt;p&gt;When the Promise eventually fulfills, the async function continues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can think of the execution like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;work()
  ↓
run
  ↓
await Promise
  ↓
suspend
  ↓
JavaScript continues elsewhere
  ↓
Promise fulfills
  ↓
resume work()
  ↓
use(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; marks a point where this particular piece of execution may need to give up control temporarily.&lt;/p&gt;

&lt;p&gt;The rest of JavaScript doesn't have to wait with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ The "Why"
&lt;/h2&gt;

&lt;p&gt;Here's where &lt;code&gt;yield&lt;/code&gt; becomes useful for understanding &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With a generator, we can see the suspension point directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;continueWork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; explicitly tells the generator:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Stop here. I'll continue later."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;await&lt;/code&gt; creates a similar boundary inside an async function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;asyncFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;continueWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting part isn't that &lt;code&gt;await&lt;/code&gt; is asynchronous.&lt;/p&gt;

&lt;p&gt;The interesting part is what happens &lt;strong&gt;to the function itself&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When execution reaches &lt;code&gt;await&lt;/code&gt;, that invocation of &lt;code&gt;asyncFunction&lt;/code&gt; cannot continue yet. Its continuation is put aside until the awaited Promise settles.&lt;/p&gt;

&lt;p&gt;JavaScript is free to do other work in the meantime.&lt;/p&gt;

&lt;p&gt;Then the async function resumes and continues with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;continueWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you can put the two side by side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generator

doSomething()
     ↓
   yield
     ↓
  suspend
     ↓
  next()
     ↓
continueWork()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function

doSomething()
     ↓
  await
     ↓
  suspend
     ↓
Promise settles
     ↓
continueWork()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The mechanisms aren't the same.&lt;/p&gt;

&lt;p&gt;A generator is explicitly resumed with &lt;code&gt;next()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An async function resumes through the Promise machinery.&lt;/p&gt;

&lt;p&gt;But both give us the same useful idea:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a computation can suspend at a specific point and continue later from that point.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the connection between &lt;code&gt;yield&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧩 The Important Difference
&lt;/h2&gt;

&lt;p&gt;It's tempting to say "&lt;code&gt;await&lt;/code&gt; is basically &lt;code&gt;yield&lt;/code&gt;."&lt;/p&gt;

&lt;p&gt;That's not quite right.&lt;/p&gt;

&lt;p&gt;Generators are &lt;strong&gt;manually resumable&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The caller controls when the generator moves forward.&lt;/p&gt;

&lt;p&gt;An async function works differently.&lt;/p&gt;

&lt;p&gt;When it reaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the function's continuation is tied to the Promise.&lt;/p&gt;

&lt;p&gt;And an async function always returns a Promise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;await&lt;/code&gt; isn't simply a nicer version of &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The mechanisms and semantics are different.&lt;/p&gt;

&lt;p&gt;The shared idea is what matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The Mental Model
&lt;/h2&gt;

&lt;p&gt;If you remember only one thing, make it this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yield
  ↓
"Pause this computation.
 Resume me when the caller decides."

await
  ↓
"Pause this computation.
 Resume me when this Promise is ready."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both let a computation give control back instead of insisting on finishing immediately.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; makes that behavior visible.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await&lt;/code&gt; hides it behind syntax that looks sequential.&lt;/p&gt;

&lt;p&gt;That's part of why &lt;code&gt;async/await&lt;/code&gt; feels so natural.&lt;/p&gt;

&lt;p&gt;It lets you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as though the function simply runs from top to bottom.&lt;/p&gt;

&lt;p&gt;But underneath that simple-looking code are suspension points.&lt;/p&gt;

&lt;p&gt;The function can stop.&lt;/p&gt;

&lt;p&gt;The rest of JavaScript can keep moving.&lt;/p&gt;

&lt;p&gt;Then the function can pick up where it left off.&lt;/p&gt;

&lt;p&gt;And once you've seen that idea in &lt;code&gt;yield&lt;/code&gt;, &lt;code&gt;await&lt;/code&gt; doesn't look quite so magical anymore.&lt;/p&gt;

&lt;p&gt;Different mechanisms.&lt;/p&gt;

&lt;p&gt;Same surprisingly useful idea:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a computation can suspend now and continue later.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>coding</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why `finally` Runs Before `return` Takes Effect</title>
      <dc:creator>Byte Corner</dc:creator>
      <pubDate>Fri, 02 Oct 2026 11:42:04 +0000</pubDate>
      <link>https://dev.to/bytecorner/why-finally-runs-before-return-takes-effect-n24</link>
      <guid>https://dev.to/bytecorner/why-finally-runs-before-return-takes-effect-n24</guid>
      <description>&lt;h1&gt;
  
  
  Why Your &lt;code&gt;finally&lt;/code&gt; Runs After &lt;code&gt;return&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;A &lt;code&gt;return&lt;/code&gt; looks like the end of a function.&lt;/p&gt;

&lt;p&gt;But put a &lt;code&gt;finally&lt;/code&gt; after it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cleanup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cleanup
42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; happened.&lt;/p&gt;

&lt;p&gt;The function just hadn't finished leaving yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔴 The Problem
&lt;/h2&gt;

&lt;p&gt;It's tempting to think of &lt;code&gt;return&lt;/code&gt; as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return
  ↓
function stops immediately
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with &lt;code&gt;finally&lt;/code&gt;, there's another step.&lt;/p&gt;

&lt;p&gt;When JavaScript reaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it evaluates &lt;code&gt;42&lt;/code&gt; and prepares to return it.&lt;/p&gt;

&lt;p&gt;Before the function actually completes, JavaScript runs the &lt;code&gt;finally&lt;/code&gt;&lt;br&gt;
block.&lt;/p&gt;

&lt;p&gt;So the flow is roughly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return 42
    ↓
remember 42
    ↓
run finally
    ↓
finish returning 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is that the return value has already been determined.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;finally&lt;/code&gt; just gets to run before the function actually exits.&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 The Solution
&lt;/h2&gt;

&lt;p&gt;This is exactly what makes &lt;code&gt;finally&lt;/code&gt; useful for cleanup.&lt;/p&gt;

&lt;p&gt;Imagine something that needs to be closed regardless of what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;file.read()&lt;/code&gt; can still determine the value returned to the caller.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;file.close()&lt;/code&gt; runs before the function actually leaves.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;finally&lt;/code&gt;, you'd have to duplicate the cleanup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;finally&lt;/code&gt;, the cleanup has one place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whether &lt;code&gt;try&lt;/code&gt; returns, throws, or finishes normally, &lt;code&gt;finally&lt;/code&gt; gets its&lt;br&gt;
chance to run.&lt;/p&gt;
&lt;h2&gt;
  
  
  ⚡ The Dangerous Part
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;finally&lt;/code&gt; isn't limited to cleanup.&lt;/p&gt;

&lt;p&gt;It can actually change how the function finishes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The original &lt;code&gt;return 42&lt;/code&gt; prepared the function to return &lt;code&gt;42&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then &lt;code&gt;finally&lt;/code&gt; executed another &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That new return becomes the function's actual result.&lt;/p&gt;

&lt;p&gt;The same thing can suppress an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Boom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of throwing, the function returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; inside &lt;code&gt;finally&lt;/code&gt; replaced the pending exception.&lt;/p&gt;

&lt;p&gt;That's why returning from &lt;code&gt;finally&lt;/code&gt; is usually a bad idea.&lt;/p&gt;

&lt;p&gt;You're no longer just cleaning up.&lt;/p&gt;

&lt;p&gt;You're changing the outcome.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 The Takeaway
&lt;/h2&gt;

&lt;p&gt;Don't think of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"I am completely done now."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of it as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"I know how I want to finish."
        ↓
     finally
        ↓
"I actually leave now."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is perfectly reasonable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this deserves suspicion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;somethingElse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;return&lt;/code&gt; decides the result.\&lt;br&gt;
&lt;code&gt;finally&lt;/code&gt; gets the last chance to change how the function exits.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
