<?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>Forem: Matsu</title>
    <description>The latest articles on Forem by Matsu (@devmatsu).</description>
    <link>https://forem.com/devmatsu</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1252052%2Faad6ebe9-eaa8-4f3c-9f0c-091b75ddd03d.png</url>
      <title>Forem: Matsu</title>
      <link>https://forem.com/devmatsu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/devmatsu"/>
    <language>en</language>
    <item>
      <title>How Relational Databases Find Database: Understanding Table and Index Scans</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Thu, 12 Mar 2026 10:08:04 +0000</pubDate>
      <link>https://forem.com/devmatsu/how-relational-databases-find-database-understanding-table-and-index-scans-49d9</link>
      <guid>https://forem.com/devmatsu/how-relational-databases-find-database-understanding-table-and-index-scans-49d9</guid>
      <description>&lt;p&gt;When creating indexes it's important to understand how they work. &lt;a href="https://devmatsu.com/blog/post/2541326" rel="noopener noreferrer"&gt;In my previous post&lt;/a&gt;, I've explained the basics of indexes and the &lt;code&gt;EXPLAIN&lt;/code&gt; command. Now we can go a step further and look at the different types of reads a database can perform.&lt;/p&gt;

&lt;p&gt;Understanding these access patterns is crucial because it helps us evaluate the trade-offs of creating (or not creating) an indexes. It also gives us insight into what happens under the hood when a query is executed.&lt;/p&gt;

&lt;p&gt;At a high level, databases retrieve data in two main ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scanning the table directly (reading rows sequentially);&lt;/li&gt;
&lt;li&gt;Using an index to locate rows more efficiently;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The query planner decides which strategy to use based on statistics, table size, and how selective the query conditions are.&lt;/p&gt;

&lt;p&gt;Since different databases use slightly different terminology, I've included the names used by each database engine for reference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Full table scan (FTS)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oracle DB: Full table scan&lt;br&gt;
SQL Server: Table Scan&lt;br&gt;
MySQL: Table Scan&lt;br&gt;
PostgreSQL: Sequential scan (Seq Scan)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is generally the least efficient scan, with O(n) complexity. The database reads the entire table sequentially, checking each row to see if it matches the query conditions.&lt;/p&gt;

&lt;p&gt;When is it used?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For small tables, the query optimizer may decide that a full table scan is actually more efficient than using an index;&lt;/li&gt;
&lt;li&gt;For queries that need most of the rows in the table;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sequential scans are not always bad. Modern databases can read tables very efficiently using sequential disk access, which can sometimes outperform index lookups when a large percentage of rows must be returned.&lt;/p&gt;

&lt;p&gt;Full table scans can lead to high I/O, increased CPU usage, and slower response times, especially on large datasets. Monitoring queries and minimizing unnecessary full table scans is a key part of database performance tuning.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Index Scan&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oracle DB: Index Range Scan&lt;br&gt;
SQL Server: Index Seek&lt;br&gt;
MySQL: Index Lookup / Range Scan&lt;br&gt;
PostgreSQL: Index Scan&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An Index Scan uses an index to locate relevant rows and then fetches the full row from the table (often called the heap in PostgreSQL).&lt;/p&gt;

&lt;p&gt;This means the database performs two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find matching entries in the index&lt;/li&gt;
&lt;li&gt;Follow pointers to retrieve the actual rows in the table&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When is it used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries filtering on columns with an index;&lt;/li&gt;
&lt;li&gt;When only a small subset of rows matches the criteria;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because the database must jump between the index and the table, index scans can involve random I/O. For queries that return many rows, this can become slower than a sequential scan.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Covering Index Scan&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oracle DB: Index Fast Full Scan&lt;br&gt;
SQL Server: Index Seek (covering)&lt;br&gt;
MySQL: Covering Index&lt;br&gt;
PostgreSQL: Index Only Scan&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A covering index scan can satisfy the query using only the index, without reading the main table. This avoids additional I/O and can be extremely fast.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, this appears as an Index Only Scan, although the database may still access the table if it needs to check row visibility.&lt;/p&gt;

&lt;p&gt;When is it used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries that only request columns included in the index;&lt;/li&gt;
&lt;li&gt;Read-heavy queries where performance is critical;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Indexes require additional storage. They are most effective when queries are frequent and selective, and less useful if the query needs columns that are not part of the index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Bitmap Index Scan&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oracle DB: Bitmap Index Scan&lt;br&gt;
SQL Server: Bitmap Filter&lt;br&gt;
MySQL: Not implemented the same way&lt;br&gt;
PostgreSQL: Bitmap Index Scan &amp;amp; Bitmap Heap Scan&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The database first scans one or more indexes and builds a bitmap representing the locations of matching rows. It then reads the table pages in a more optimized order.&lt;/p&gt;

&lt;p&gt;When is it used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries with multiple indexed conditions (e.g., WHERE status='active' AND type='premium');&lt;/li&gt;
&lt;li&gt;Medium to large tables where reading rows individually would be slow;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More memory usage to build the bitmap. Very efficient for combining multiple indexes, but not ideal for very small or highly selective queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the right scan
&lt;/h2&gt;

&lt;p&gt;The query planner decides which scan to use based on several factors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Table size&lt;/li&gt;
&lt;li&gt;Index availability&lt;/li&gt;
&lt;li&gt;Selectivity of the query&lt;/li&gt;
&lt;li&gt;Table statistics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this, adding an index does not guarantee it will be used. If the planner estimates that most rows will be returned, it may choose a sequential scan instead.&lt;/p&gt;

&lt;p&gt;Understanding these access methods helps interpret &lt;code&gt;EXPLAIN&lt;/code&gt; output and design indexes that actually improve performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>relationaldb</category>
      <category>index</category>
      <category>scan</category>
      <category>performance</category>
    </item>
    <item>
      <title>"The API Is Slow, Fix It!" – What I Learned About `EXPLAIN` and Indexing</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Thu, 29 May 2025 13:52:40 +0000</pubDate>
      <link>https://forem.com/devmatsu/the-api-is-slow-fix-it-what-i-learned-about-explain-and-indexing-99k</link>
      <guid>https://forem.com/devmatsu/the-api-is-slow-fix-it-what-i-learned-about-explain-and-indexing-99k</guid>
      <description>&lt;p&gt;During a technical interview, the situation was something like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Your manager tells you: the API is too slow. Fix it!”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I started walking through what I’d do — checking logs, measuring response time, analyzing the stack. Eventually, we narrowed it down to the database, suspecting a slow query.&lt;/p&gt;

&lt;p&gt;I mentioned I’d try to optimize it based on “feeling” and maybe throw in some indexes. Then the interviewer dropped a simple but powerful tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Have you tried running it with EXPLAIN?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s when I learned something new and honestly, it changed the way I look at query performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Does &lt;code&gt;EXPLAIN&lt;/code&gt; Actually Show?
&lt;/h2&gt;

&lt;p&gt;When you write a query, the database doesn’t just run it blindly, it builds an &lt;strong&gt;execution plan&lt;/strong&gt;. And &lt;code&gt;EXPLAIN&lt;/code&gt; reveals exactly how it plans to execute that query.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like saying: “Show me how you're going to fetch these results before you actually do it.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With it, you can discover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it’s doing a full table scan;&lt;/li&gt;
&lt;li&gt;If your indexes are being used or totally ignored;&lt;/li&gt;
&lt;li&gt;What kind of join is being applied (Nested Loop, Hash Join, etc.);&lt;/li&gt;
&lt;li&gt;How many rows it &lt;strong&gt;thinks&lt;/strong&gt; it will read;&lt;/li&gt;
&lt;li&gt;And how “expensive” the query is, cost-wise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s simple to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'hi@devmatsu.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why Not Add Indexes Everywhere?
&lt;/h2&gt;

&lt;p&gt;Another key part of the interview was when we discussed indexing. I first said, "maybe because of storage cost?" — but the better answer was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Indexes speed up reads, but make writes more expensive."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every time you run an &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt;, the database also has to update every related index. So the trade-off is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too many indexes → fast reads, but slow writes.&lt;/li&gt;
&lt;li&gt;Too few indexes → fast writes, but slow reads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why indexing is a design choice, not a default solution. And again, &lt;code&gt;EXPLAIN&lt;/code&gt; helps you check if your index is actually being used or if it’s just sitting there eating up space.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Took from That Interview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;EXPLAIN&lt;/code&gt; isn’t optional, it’s your ally for understanding performance;&lt;/li&gt;
&lt;li&gt;Indexing is powerful, but it comes at a cost. Use it with intention;&lt;/li&gt;
&lt;li&gt;And most of all: interviews aren’t just tests. Sometimes, they’re great learning moments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>index</category>
      <category>performance</category>
    </item>
    <item>
      <title>Setting a Default Node Version for Your Project with .nvmrc</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Thu, 24 Apr 2025 20:12:08 +0000</pubDate>
      <link>https://forem.com/devmatsu/setting-a-default-node-version-for-your-project-with-nvmrc-4j81</link>
      <guid>https://forem.com/devmatsu/setting-a-default-node-version-for-your-project-with-nvmrc-4j81</guid>
      <description>&lt;p&gt;I used to switch between projects and wonder why some things just... broke.&lt;/p&gt;

&lt;p&gt;Then I realized the issue: each project was using a different Node.js version.&lt;br&gt;&lt;br&gt;
Sometimes one needed Node 18 for a modern API, while another was still running on 16.&lt;/p&gt;

&lt;p&gt;That’s when I found out about &lt;code&gt;.nvmrc&lt;/code&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  What is &lt;code&gt;.nvmrc&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;It’s a simple file that you add to the root of your project to define the &lt;strong&gt;Node.js version&lt;/strong&gt; that should be used.&lt;/p&gt;

&lt;p&gt;No JSON. No config object. Just the version number.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;When using &lt;a href="https://github.com/nvm-sh/nvm" rel="noopener noreferrer"&gt;NVM (Node Version Manager)&lt;/a&gt;, you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm use
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And NVM will automatically switch your Node.js version to the one specified in &lt;code&gt;.nvmrc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is super helpful when you clone a repo and want to make sure you're using the right environment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why it matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keeps your local version consistent with the project&lt;/li&gt;
&lt;li&gt;Prevents bugs caused by incompatible Node versions&lt;/li&gt;
&lt;li&gt;Makes onboarding easier for teams&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're working with multiple Node projects, &lt;strong&gt;this tiny file can save you hours&lt;/strong&gt; of debugging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>node</category>
    </item>
    <item>
      <title>How JavaScript Gets Updated: ECMAScript, V8 and Your Runtime Explained</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Sun, 13 Apr 2025 19:37:10 +0000</pubDate>
      <link>https://forem.com/devmatsu/how-javascript-gets-updated-ecmascript-v8-and-your-runtime-explained-2hmd</link>
      <guid>https://forem.com/devmatsu/how-javascript-gets-updated-ecmascript-v8-and-your-runtime-explained-2hmd</guid>
      <description>&lt;p&gt;I remember when I started coding in JavaScript, I used to think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Okay... so JavaScript gets updated like any language... just update and that's it."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But that's not exactly how things work here. JavaScript runs in different environments like browser, Node.js, Deno — and what actually decides if you can use that new fancy feature you saw on X (the old Twitter) is your runtime.&lt;/p&gt;

&lt;p&gt;In this post, I’ll explain how new JavaScript features are released, how ECMAScript fits into this process and why your runtime decides what features you can use (or not).&lt;/p&gt;




&lt;h2&gt;
  
  
  What is ECMAScript?
&lt;/h2&gt;

&lt;p&gt;ECMAScript is the official specification of the language and JavaScript is just an implementation of that spec.&lt;/p&gt;

&lt;p&gt;Every year, a group called &lt;strong&gt;TC39&lt;/strong&gt; (the committee that defines the language) releases a new ECMAScript version — adding new features, syntax, and behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of features introduced by ECMAScript versions:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Year&lt;/th&gt;
&lt;th&gt;Features example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ES6&lt;/td&gt;
&lt;td&gt;2015&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, arrow functions, classes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ES2020&lt;/td&gt;
&lt;td&gt;2020&lt;/td&gt;
&lt;td&gt;Optional chaining &lt;code&gt;?.&lt;/code&gt;, Nullish coalescing &lt;code&gt;??&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ES2022&lt;/td&gt;
&lt;td&gt;2022&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;#privateFields&lt;/code&gt; in classes, top-level &lt;code&gt;await&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  But How Do We Get Those Features?
&lt;/h2&gt;

&lt;p&gt;That's the point. It's not about updating JavaScript. It depends on the JavaScript Engine you’re running.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a JavaScript Engine?
&lt;/h2&gt;

&lt;p&gt;A JavaScript Engine is what reads, parses, and executes your JavaScript code. Each environment (browser, Node.js) uses an engine.&lt;/p&gt;

&lt;p&gt;Popular engines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;V8&lt;/strong&gt; → used by Chrome and Node.js
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SpiderMonkey&lt;/strong&gt; → used by Firefox
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScriptCore (Nitro)&lt;/strong&gt; → used by Safari
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  When Do You Get New JavaScript Features?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Environment&lt;/th&gt;
&lt;th&gt;How do you get new features?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Browser&lt;/td&gt;
&lt;td&gt;By updating the browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node.js&lt;/td&gt;
&lt;td&gt;By updating your Node.js version&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It's simple as that. If you want to use new features just need to update the browser or the Node.js version&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Is This Important?
&lt;/h2&gt;

&lt;p&gt;Because when you’re writing code locally — in your machine — you can probably use everything that your Node.js or browser supports. But when you're writing code for production (especially frontend), you need to care about your users' environment.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;If your users still use old browsers like IE11 (hope not) or very old Android versions — you can’t use optional chaining &lt;code&gt;?.&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;The browser wouldn’t even understand that syntax.&lt;/p&gt;

&lt;p&gt;That’s why tools like: Babel, TypeScript, Webpack, Vite and others exist — to transpile your code and convert modern JavaScript to old compatible syntax.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;JavaScript is updated every year through ECMAScript but you only get those new features when your environment supports them — either by updating the engine (browser, Node.js) or transpiling your code for compatibility.&lt;/p&gt;

&lt;p&gt;Now every time you see a new JavaScript feature, remember:&lt;br&gt;&lt;br&gt;
It’s not about "updating JavaScript" — it’s about your engine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>concept</category>
      <category>fundamentals</category>
    </item>
    <item>
      <title>Closures vs Classes in JavaScript: What I Learned Solving LeetCode #2622</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Tue, 08 Apr 2025 13:56:07 +0000</pubDate>
      <link>https://forem.com/devmatsu/closures-vs-classes-in-javascript-what-i-learned-solving-leetcode-2622-2mkb</link>
      <guid>https://forem.com/devmatsu/closures-vs-classes-in-javascript-what-i-learned-solving-leetcode-2622-2mkb</guid>
      <description>&lt;p&gt;I was solving &lt;a href="https://leetcode.com/problems/cache-with-time-limit/" rel="noopener noreferrer"&gt;LeetCode #2622 – Cache With Time Limit&lt;/a&gt; and the goal was to create a function that returns a caching system with TTL (time-to-live).&lt;/p&gt;

&lt;p&gt;My first instinct? Use a &lt;strong&gt;closure&lt;/strong&gt;. It's a common pattern — just define a variable outside the function and the inner function can reference it.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔁 Closure-style cache:
&lt;/h3&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;createCache&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&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="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;ttl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&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="na"&gt;expiry&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ttl&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;cached&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;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;Easy. It works, it's scoped and no one can touch &lt;code&gt;store&lt;/code&gt; from the outside — classic closure encapsulation. But the LeetCode problem expected us to extend a function via its prototype — and that threw me off. I was like: "How the heck do I create a private variable across prototype methods?"&lt;br&gt;
That’s when the light bulb went on.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; + Classes = Encapsulation
&lt;/h2&gt;

&lt;p&gt;I realized I needed to treat it like a class and that’s when I remembered: when using prototypes (or classes), you can store shared data inside this.&lt;br&gt;
So I did:&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;var&lt;/span&gt; &lt;span class="nx"&gt;TimeLimitedCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;TimeLimitedCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;duration&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;expiredAt&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;duration&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;existed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&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="nx"&gt;expiredAt&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;existed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;TimeLimitedCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expiredAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;entry&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;Now everything lives inside &lt;code&gt;this.cache&lt;/code&gt;. No need for an external closure and it works great across all methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Private Fields in Classes
&lt;/h3&gt;

&lt;p&gt;At that point I thought: "Wait, so this whole thing is basically behaving like a class!". And if it’s a class, can we go one step further? What about private fields?&lt;br&gt;
In the past, we'd use a naming convention like &lt;code&gt;_cache&lt;/code&gt;, but that was just a signal, not actual protection.&lt;br&gt;
But now with modern JavaScript (since ES2022), we can use native private fields with the &lt;code&gt;#&lt;/code&gt; symbol:&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;class&lt;/span&gt; &lt;span class="nc"&gt;TimeLimitedCache&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;duration&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;expiredAt&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;duration&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;existed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&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="nx"&gt;expiredAt&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;existed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expiredAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;entry&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s real encapsulation!&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Closures are a great way to encapsulate state, especially for factory functions;&lt;/li&gt;
&lt;li&gt;Classes with &lt;code&gt;this&lt;/code&gt; are a powerful alternative when you want to work with prototypes or build something that feels more like an object;&lt;/li&gt;
&lt;li&gt;Modern JavaScript gives us native private fields (&lt;code&gt;#&lt;/code&gt;);&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No matter what pattern you choose, closures or classes, the key is to understand the trade-offs and when each tool fits better.&lt;/p&gt;

&lt;p&gt;Solving this problem gave me a new perspective on how classes and closures can often solve the same thing in different ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What happens when both try and finally return in JavaScript?</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Mon, 07 Apr 2025 03:05:26 +0000</pubDate>
      <link>https://forem.com/devmatsu/what-happens-when-both-try-and-finally-return-in-javascript-49i6</link>
      <guid>https://forem.com/devmatsu/what-happens-when-both-try-and-finally-return-in-javascript-49i6</guid>
      <description>&lt;p&gt;What do you think will happen if we run this code?&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;test&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;oops&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;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catch&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;finally&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;test&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first you might think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Okay, it throws an error, so it goes into the catch block and returns 'catch'... right?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But not quite! Actually this code will return &lt;code&gt;finally&lt;/code&gt;! But why? Let me explain. &lt;/p&gt;

&lt;h2&gt;
  
  
  How &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; Work
&lt;/h2&gt;

&lt;p&gt;We usually think of &lt;code&gt;return&lt;/code&gt; as an immediate exit, but in JavaScript, that’s not exactly how it works under the hood. When the JS engine hits a &lt;code&gt;return&lt;/code&gt;, it creates something called a Completion Record.&lt;/p&gt;

&lt;p&gt;Think of it as a sticky note that says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Hey, we're done here. The reason is return and here's the value to give back"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This record is kept internally by the JavaScript engine until the function truly finishes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The flow with &lt;code&gt;finally&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here's a real-world metaphor:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You go to the market (that's your function)&lt;/p&gt;

&lt;p&gt;You try to buy apples (try), but if they’re out of stock, you get bananas instead (catch).&lt;/p&gt;

&lt;p&gt;As you're about to check out, your mom calls and says:&lt;br&gt;
“Forget that — just buy milk!” (finally).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So no matter what you were planning to bring home (apples or bananas), you end up bringing milk.&lt;/p&gt;

&lt;p&gt;Here's the code version:&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;goToMarket&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="s1"&gt;No apples today!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Apples&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;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bananas&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Milk&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;goToMarket&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 'Milk'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Does This Happen?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;try&lt;/code&gt; block tries to return &lt;code&gt;'Apples'&lt;/code&gt;, but an error is thrown.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;catch&lt;/code&gt; block handles it and returns &lt;code&gt;'Bananas'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Before the function can return &lt;code&gt;'Bananas'&lt;/code&gt;, the &lt;code&gt;finally&lt;/code&gt; block runs and its &lt;code&gt;return 'Milk'&lt;/code&gt; overrides everything.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So &lt;code&gt;'Milk'&lt;/code&gt; is the final return value.&lt;/p&gt;

&lt;h2&gt;
  
  
  ECMAScript Spec (in simple terms)
&lt;/h2&gt;

&lt;p&gt;According to the ECMAScript spec:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;return&lt;/code&gt; in &lt;code&gt;try&lt;/code&gt; or &lt;code&gt;catch&lt;/code&gt; doesn't immediately return — it creates a Completion Record;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;finally&lt;/code&gt; block is always executed, regardless of what happened in &lt;code&gt;try&lt;/code&gt; or &lt;code&gt;catch&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;finally&lt;/code&gt; block has its own &lt;code&gt;return&lt;/code&gt;, it overwrites any previous return;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why &lt;code&gt;finally&lt;/code&gt; becomes the actual return value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;This behavior follows the ECMAScript specification, which details how CompletionRecords are handled during function execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block isn't just a "cleanup" section — it can take control of your function's return value if you’re not careful. Even if &lt;code&gt;try&lt;/code&gt; or &lt;code&gt;catch&lt;/code&gt; has a &lt;code&gt;return&lt;/code&gt;, a &lt;code&gt;return&lt;/code&gt; inside &lt;code&gt;finally&lt;/code&gt; will override it.&lt;/p&gt;

&lt;p&gt;So next time you're writing error handling, think twice before returning inside a &lt;code&gt;finally&lt;/code&gt; block, it might silently change your function’s behavior.&lt;/p&gt;

&lt;p&gt;Understanding this little quirk helps you avoid unexpected bugs and gives you more control over your code flow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>fundamentals</category>
    </item>
    <item>
      <title>What is Immutability in JavaScript?</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Sun, 06 Apr 2025 03:43:58 +0000</pubDate>
      <link>https://forem.com/devmatsu/what-is-immutability-in-javascript-fnm</link>
      <guid>https://forem.com/devmatsu/what-is-immutability-in-javascript-fnm</guid>
      <description>&lt;p&gt;Immutability is a powerful concept in programming and especially important in JavaScript, even if we don't always think about it. It affects how we manage data, state and side effects in our applications.&lt;/p&gt;

&lt;p&gt;In this post, we'll break down what immutability means, why it matters and how you can work with it in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Immutability?
&lt;/h2&gt;

&lt;p&gt;Basically &lt;strong&gt;Immutability&lt;/strong&gt; means something cannot be changed after it's created. For example in JavaScript, &lt;strong&gt;primitive values&lt;/strong&gt; like numbers, strings and booleans are immutable. This doesn't mean the variable can't be reassigned, it means the value itself can't be changed in place.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example with a string:
&lt;/h3&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="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matsu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Trying to change just one character (this won't work)&lt;/span&gt;
&lt;span class="nx"&gt;name&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;B&lt;/span&gt;&lt;span class="dl"&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'Matsu' → Still the same&lt;/span&gt;

&lt;span class="c1"&gt;// But we can reassign the variable&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'John'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the string itself is immutable but the variable name can be reassigned to a new string. In contrast, objects and arrays are mutable by default, which means you can change their contents directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example with an object:
&lt;/h3&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matsu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying the object&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can lead to unintended side effects, especially when multiple parts of your code are referencing and modifying the same object or array, such as shared data or state.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Can Immutability Do?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Avoid side effects (accidentally changing shared data);&lt;/li&gt;
&lt;li&gt;Make debugging easier (state doesn't change in hidden ways);&lt;/li&gt;
&lt;li&gt;Improve performance in some cases (e.g. with reference checks);&lt;/li&gt;
&lt;li&gt;Work better with frameworks like React, which rely on state changes via new objects;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Working with Immutability in JavaScript
&lt;/h2&gt;

&lt;p&gt;To keep data immutable, we usually create new copies instead of modifying the original.&lt;/p&gt;

&lt;h3&gt;
  
  
  Copying Arrays
&lt;/h3&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Instead of: numbers.push(4)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// [1, 2, 3]&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [1, 2, 3, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Copying Objects
&lt;/h3&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matsu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Instead of: user.age = 29&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedUser&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;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;29&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// { name: 'Matsu', age: 28 }&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;updatedUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'Matsu', age: 29 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Attention with Deep Copy vs Shallow Copy
&lt;/h3&gt;

&lt;p&gt;Most techniques (like spread (&lt;code&gt;...&lt;/code&gt;) and &lt;code&gt;Object.assign&lt;/code&gt;) create shallow copies. This means only top-level properties are copied. If the object contains nested objects, those nested references remain shared between the original and the copy.&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;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matsu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tokyo&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;clone&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;person&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Osaka&lt;/span&gt;&lt;span class="dl"&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 😱 'Osaka'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we used the spread operator, &lt;code&gt;person.address&lt;/code&gt; and &lt;code&gt;clone.address&lt;/code&gt; still point to the same object in memory, so changing one affects the other.&lt;/p&gt;

&lt;p&gt;To truly copy all levels of an object, you need a deep clone. One common approach is using &lt;code&gt;cloneDeep&lt;/code&gt; from Lodash:&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;import&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&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;deepClone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cloneDeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new object where even the nested structures are fully cloned, breaking the reference between the original and the copy.&lt;/p&gt;

&lt;p&gt;You can check my post about &lt;a href="https://dev.to/devmatsu/exploring-object-cloning-techniques-in-javascript-380c"&gt;Exploring Object Cloning Techniques in JavaScript&lt;/a&gt; to dive deeper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enforcing Immutability in JavaScript
&lt;/h2&gt;

&lt;p&gt;You might think using &lt;code&gt;const&lt;/code&gt; would make an object or array immutable, but that's not the case. &lt;code&gt;const&lt;/code&gt; only prevents reassignment, not mutation.&lt;br&gt;
By default, JavaScript objects and arrays are mutable. But there are ways to partially enforce immutability at runtime using built-in methods like:&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;Object.freeze()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Prevents adding, removing or modifying properties of an object making the object shallowly immutable.&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;config&lt;/span&gt; &lt;span class="o"&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;freeze&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&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;retries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3 — change is ignored or throws in strict mode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ Note: This only works on the first level. If the object has nested properties, they’re still mutable unless you recursively freeze them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Freezing an Array
&lt;/h3&gt;

&lt;p&gt;You can also use &lt;code&gt;Object.freeze()&lt;/code&gt; to prevent changes to an array, for example, if you want to enforce a fixed set of values (e.g. length, content):&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;roles&lt;/span&gt; &lt;span class="o"&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;freeze&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin&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="s1"&gt;editor&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="s1"&gt;viewer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nx"&gt;roles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;guest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// ❌ won't work&lt;/span&gt;
&lt;span class="nx"&gt;roles&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;superadmin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// ❌ won't work&lt;/span&gt;
&lt;span class="nx"&gt;roles&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="c1"&gt;// ❌ won't work&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;roles&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 'admin', 'editor', 'viewer' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful when you want to protect reference lists, roles, or static options in your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Object.seal()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Prevents adding or removing properties, but you can still modify existing ones.&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;settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&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;seal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// ✅ allowed&lt;/span&gt;
&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;language&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// ❌ can't add new property&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// ❌ can't delete property&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;settings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { theme: 'light' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These methods are great for protecting config objects, shared constants or preventing accidental mutations especially in larger codebases.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use Immutability?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When managing application state (especially in React or Redux);&lt;/li&gt;
&lt;li&gt;When writing pure functions;&lt;/li&gt;
&lt;li&gt;When you want predictable and reliable behavior in your code;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Immutability is not about never changing anything, it's about changing things safely by creating new versions instead of modifying existing ones. You can also "lock" objects and arrays using &lt;code&gt;Object.freeze()&lt;/code&gt; or &lt;code&gt;Object.seal()&lt;/code&gt; to help prevent accidental changes.&lt;/p&gt;

&lt;p&gt;It leads to better, cleaner and more bug-resistant code, especially as your project grows. So embrace immutability and you'll avoid a lot of hidden side effects and unexpected behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>immutability</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Programming Paradigms Explained with Code Examples</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Fri, 04 Apr 2025 14:12:18 +0000</pubDate>
      <link>https://forem.com/devmatsu/programming-paradigms-explained-with-code-examples-1ib0</link>
      <guid>https://forem.com/devmatsu/programming-paradigms-explained-with-code-examples-1ib0</guid>
      <description>&lt;p&gt;When you're learning to code or expanding your knowledge, you'll often hear the term &lt;strong&gt;"programming paradigms"&lt;/strong&gt; but what does that actually mean?&lt;/p&gt;

&lt;p&gt;A programming paradigm is a way to approach problem-solving using code. It's about how you structure your logic and instructions. Different paradigms shape the way you think and organize your solutions.&lt;/p&gt;

&lt;p&gt;Let's explore some of the main ones with simple examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Imperative Programming
&lt;/h2&gt;

&lt;p&gt;This is one of the most common and intuitive paradigms. You tell the computer how to do something, step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (in JavaScript):
&lt;/h3&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="nx"&gt;sum&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Declarative Programming
&lt;/h2&gt;

&lt;p&gt;You focus on what you want instead of how to do it. SQL and even parts of React are declarative.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (SQL):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example (React - .jsx file):
&lt;/h3&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;Greeting&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may not look declarative at first, but the point is: React uses JavaScript under the hood, the way you define UI with JSX is declarative because you're describing what should be rendered based on the current state or props, not how to manually build or update the DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Object-Oriented Programming (OOP)
&lt;/h2&gt;

&lt;p&gt;This paradigm organizes code that encapsulate both data and behavior. It's great for modeling real-world entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (TypeScript):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&gt; makes a sound.`&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;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&gt; barks.`&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Rex barks.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Functional Programming
&lt;/h2&gt;

&lt;p&gt;In functional programming, you use pure functions and avoid mutating data. It's about composition, immutability and statelessness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (JavaScript):
&lt;/h3&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;double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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;double&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Modern languages like JavaScript, Python and TypeScript are multi-paradigm so you can combine functional, object-oriented and imperative styles depending on what fits best.&lt;/p&gt;

&lt;p&gt;Here are some languages commonly associated with each paradigm:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Paradigm&lt;/th&gt;
&lt;th&gt;Languages&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Imperative&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;JavaScript, TypeScript, Go, Python, Java, Rust&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Declarative&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;SQL, JavaScript (JSX), Elixir&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Object-Oriented&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;TypeScript, Python, C#, Java, Kotlin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Functional&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;JavaScript, TypeScript, Python, Kotlin, Elixir&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you're aiming to grow as a developer, understanding paradigms is a powerful step. Knowing the fundamentals helps you understand what you're doing and why you're doing it that way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>fundamentals</category>
    </item>
    <item>
      <title>Memoization and Caching: Are they the same in JavaScript?</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Tue, 01 Apr 2025 01:55:47 +0000</pubDate>
      <link>https://forem.com/devmatsu/memoization-and-caching-are-they-the-same-in-javascript-3ann</link>
      <guid>https://forem.com/devmatsu/memoization-and-caching-are-they-the-same-in-javascript-3ann</guid>
      <description>&lt;p&gt;At first glance, &lt;strong&gt;Memoization&lt;/strong&gt; and &lt;strong&gt;Caching&lt;/strong&gt; might seen like the same thing. After all, both techniques involve saving results to avoid doing the same work again. But while they are related, they're not exactly the same and understanding the difference between them will make you have a strong fundamentals.&lt;/p&gt;

&lt;p&gt;In this post, we'll break down what memoization and caching really are, compare them side by side and walk through practical examples so you'll never confuse them again.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Memoization?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Memoization&lt;/strong&gt; is a specific form of caching that's focused on &lt;strong&gt;functions&lt;/strong&gt;. It stores the result of a function &lt;strong&gt;based on its inputs arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the function is called again with the same arguments, the stores result is returned instead of running the function again.&lt;br&gt;
It's typically used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pure Functions (functions that always return the same output for the same input);&lt;/li&gt;
&lt;li&gt;Expensive calculations that are repeated often;&lt;/li&gt;
&lt;li&gt;Variables in outer functions that are preserved through closures (e.g., for storing cached values);&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It usually stores values &lt;strong&gt;in memory&lt;/strong&gt;, like in a plain object or closure.&lt;/p&gt;


&lt;h2&gt;
  
  
  What is Caching?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Caching&lt;/strong&gt; is a broader concept. It referes to storing any kind of data so it can be accessed faster in the future, not just functions results.&lt;/p&gt;

&lt;p&gt;It's used everywhere&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API responses;&lt;/li&gt;
&lt;li&gt;Database query results;&lt;/li&gt;
&lt;li&gt;Images, files, computed reports;&lt;/li&gt;
&lt;li&gt;Session data, user info, etc;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it can be stored anywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In memory;&lt;/li&gt;
&lt;li&gt;In a Redis store;&lt;/li&gt;
&lt;li&gt;In the browser (localStorage, indexedDB);&lt;/li&gt;
&lt;li&gt;On disk&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Key differences
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Memoization&lt;/th&gt;
&lt;th&gt;Caching&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Function-level&lt;/td&gt;
&lt;td&gt;Global / application-level&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Store function results&lt;/td&gt;
&lt;td&gt;Store any reusable data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Based On&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Function arguments&lt;/td&gt;
&lt;td&gt;Keys like URL, ID, custom cache key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Typical Storage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;In-memory (object or closure)&lt;/td&gt;
&lt;td&gt;Memory, Redis, disk, browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Recursive calculations, pure logic&lt;/td&gt;
&lt;td&gt;API calls, DB queries, static files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Invalidation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Rare (input-based)&lt;/td&gt;
&lt;td&gt;TTL, manual purge, cache strategy needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;factorial(n)&lt;/code&gt;, &lt;code&gt;fibonacci(n)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Cached API response by user ID&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  Memoization in code
&lt;/h2&gt;

&lt;p&gt;Let's look at a simple example using the factorial function, which is a great case for memoization because each result depends on the previous one.&lt;/p&gt;

&lt;p&gt;Without Memoization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function factorial(n) {
  if (n &amp;lt;= 1) return 1;
  return n * factorial(n - 1);
}

console.log(factorial(5)); // 120
console.log(factorial(5)); // Recalculates everything again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Memoization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoizedFactorial() {
  const cache = {};

  return function factorial(n) {
    if (n in cache) {
      console.log('From cache:', n);
      return cache[n];
    }

    if (n &amp;lt;= 1) return 1;

    const result = n * factorial(n - 1);
    cache[n] = result;
    return result;
  };
}

const factorial = memoizedFactorial();

console.log(factorial(5)); // Calculated
console.log(factorial(5)); // Cached!
console.log(factorial(6)); // Uses cached factorial(5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows how we avoid redundant work by reusing previous results.&lt;/p&gt;




&lt;h2&gt;
  
  
  Caching in code (Example with Redis)
&lt;/h2&gt;

&lt;p&gt;Now let's say we have a function that fetches user data, possibly expensive if it hits a DB or API.&lt;br&gt;
Instead of recalculating or re-fetching it every time we cache it using Redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getUser(userId) {
  const key = `user:${userId}`;
  const cached = await redis.get(key);

  if (cached) {
    console.log('Returned from Redis cache');
    return JSON.parse(cached);
  }

  const user = await fetchUserFromDB(userId);
  await redis.set(key, JSON.stringify(user), 'EX', 60); // Cache for 60s
  return user;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;not memoization&lt;/strong&gt;, but it is caching. It's not tied directly to a function's input/output relationship in memory. It's stored in an external cache layer and used across multiple calls.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Each
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use memoization when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You're working with pure functions;&lt;/li&gt;
&lt;li&gt;You want to optimize repeated calculations;&lt;/li&gt;
&lt;li&gt;You don't want to introduce external storage (just use in-memory);&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use caching when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You want to store data fetched or computed from external sources;&lt;/li&gt;
&lt;li&gt;You need to share results between processes or requests;&lt;/li&gt;
&lt;li&gt;You need expiration, invalidation or disk/network-level persistence;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Memoization and caching are related but they solve different problems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Memoization is about functions calls: if I call this again with the same input, don't recompute it.&lt;/p&gt;

&lt;p&gt;Caching is about data reuse: if I already fetched this, let's avoid doing it again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Quick recap:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Memoization is a type of caching;&lt;/li&gt;
&lt;li&gt;All memoization is caching, but not all caching is memoization;&lt;/li&gt;
&lt;li&gt;Both can drastically improve performance if used in the right context;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>fundamentals</category>
    </item>
    <item>
      <title>What is an IIFE in JavaScript and Why Would You Use It?</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Fri, 28 Mar 2025 21:32:28 +0000</pubDate>
      <link>https://forem.com/devmatsu/what-is-an-iife-in-javascript-and-why-would-you-use-it-17al</link>
      <guid>https://forem.com/devmatsu/what-is-an-iife-in-javascript-and-why-would-you-use-it-17al</guid>
      <description>&lt;p&gt;In JavaScript, you might have seen a function that seems to call itself right after it's defined. That’s called an IIFE, short for Immediately Invoked Function Expression. It might look weird at first, but it has some interesting use cases. Let’s explore what it is, how it works, and when to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an IIFE?
&lt;/h2&gt;

&lt;p&gt;An IIFE is a function that runs immediately after it’s defined. The main idea is to create a new scope without polluting the global one.&lt;/p&gt;

&lt;p&gt;Here’s how it looks:&lt;br&gt;
&lt;code&gt;(function () { console.log('This runs immediately!'); })();&lt;/code&gt;&lt;br&gt;
Or, with arrow function syntax:&lt;br&gt;
&lt;code&gt;(() =&amp;gt; { console.log('Arrow IIFE runs immediately!'); })();&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Does This Work?
&lt;/h2&gt;

&lt;p&gt;In JavaScript, when you define a function like:&lt;br&gt;
&lt;code&gt;function sayHi() {}&lt;/code&gt;&lt;br&gt;
…it’s just a declaration, and it won’t run until you call it.&lt;/p&gt;

&lt;p&gt;But to make the function run immediately, you need to turn it into an expression. That’s why we wrap it in parentheses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first () turns the function into a function expression.&lt;/li&gt;
&lt;li&gt;The second () calls it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Avoiding Global Scope Pollution
IIFEs help keep variables inside their own scope so they don’t interfere with other parts of your code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function () { const secret = 'I stay local'; console.log(secret); // Works here })();

console.log(secret); // Error: secret is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Running Setup Code Once
If you want to run some initialization logic right away (like setting up configs), IIFE can be a clean solution.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = (() =&amp;gt; { const config = { env: 'production' }; console.log('App is starting...'); return { getConfig: () =&amp;gt; config }; })();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using await in Non-Async Environments (Async IIFE)
In environments where you can't use top-level await (like older Node.js or CommonJS modules), an async IIFE allows you to run asynchronous code with await.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(async () =&amp;gt; { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log(data); })();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is super useful for running async logic without needing to define and then manually call a separate async function.&lt;/p&gt;

&lt;p&gt;IIFEs are a handy JavaScript pattern that allow you to execute functions immediately while keeping variables scoped locally. While not as common in ES6+ codebases thanks to modules and let/const, they're still worth knowing — especially when you stumble across them in older code or want a quick isolated block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>fundamentals</category>
    </item>
    <item>
      <title>Comparing Promise Chaining vs. Async/Await in JavaScript</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Wed, 22 Jan 2025 14:21:57 +0000</pubDate>
      <link>https://forem.com/devmatsu/comparing-promise-chaining-vs-asyncawait-in-javascript-2omh</link>
      <guid>https://forem.com/devmatsu/comparing-promise-chaining-vs-asyncawait-in-javascript-2omh</guid>
      <description>&lt;p&gt;In modern JavaScript (Node.js included), asynchronous operations can be managed using either &lt;strong&gt;Promise chaining&lt;/strong&gt; or the &lt;strong&gt;Async/Await&lt;/strong&gt; syntax. Both approaches help avoid deeply nested callbacks (the infamous “callback hell”) and ensure cleaner, more readable code. However, there are some nuanced differences in style and usage between the two.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise Chaining
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Promise chaining&lt;/strong&gt; involves returning a new Promise from each &lt;code&gt;.then()&lt;/code&gt; in a sequence. It can look 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;fetchUserData()
  .then((userData) =&amp;gt; processData(userData))
  .then((processedData) =&amp;gt; displayResults(processedData))
  .catch((error) =&amp;gt; handleError(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clear Step-by-Step Flow&lt;/strong&gt;: Each &lt;code&gt;.then()&lt;/code&gt; block details a subsequent step, making the logic flow relatively clear.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Error Handling&lt;/strong&gt;: A single &lt;code&gt;.catch()&lt;/code&gt; at the end can handle errors thrown at any step in the chain.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Widely Supported&lt;/strong&gt;: Promises have been part of JS for quite some time, so this style is compatible with most JavaScript environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nested Chaining&lt;/strong&gt;: When multiple asynchronous tasks need branching logic, chaining can become more complex or deeply nested.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: While much better than callbacks, a series of &lt;code&gt;.then()&lt;/code&gt; calls can still feel a bit verbose or less synchronous-like compared to Async/Await.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Tip: Using &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; directly in Promise chaining
&lt;/h3&gt;

&lt;p&gt;When creating custom Promises, you might come across this pattern:&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;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tasks&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;resolve&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;reject&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 works because &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; are functions and &lt;code&gt;then()&lt;/code&gt;/&lt;code&gt;catch()&lt;/code&gt; pass their result as arguments. So writing &lt;code&gt;.then(resolve)&lt;/code&gt; is equivalent to &lt;code&gt;.then((result) =&amp;gt; resolve(result))&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Avoid calling them like resolve() immediately or you'll trigger them at the wrong time.&lt;/p&gt;

&lt;p&gt;I came across this pattern while solving LeetCode 2721, where we implement a custom version of Promise.all. Passing resolve directly into .then() made the solution cleaner and avoided unnecessary async/await usage.”&lt;/p&gt;




&lt;h2&gt;
  
  
  Async/Await
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Async/Await&lt;/strong&gt; is syntactic sugar on top of Promises that provides a more synchronous feel to asynchronous code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function run() {
  try {
    const userData = await fetchUserData();
    const processedData = await processData(userData);
    displayResults(processedData);
  } catch (error) {
    handleError(error);
  }
}

run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Synchronous-Like Syntax&lt;/strong&gt;: The code reads in a top-down manner, making asynchronous flows easier to follow.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inline Error Handling&lt;/strong&gt;: Using &lt;code&gt;try...catch&lt;/code&gt; blocks within an &lt;code&gt;async&lt;/code&gt; function allows more granular handling of errors where they occur.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cleaner Logic&lt;/strong&gt;: Multiple &lt;code&gt;await&lt;/code&gt; can be used without creating a “chain”, potentially making the code easier to refactor or maintain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling Complexity&lt;/strong&gt;: While &lt;code&gt;try...catch&lt;/code&gt; is powerful, ensuring you handle all errors properly across multiple awaits can require careful structuring.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requires Modern JS&lt;/strong&gt;: Async/Await is supported in modern JavaScript environments (Node.js &amp;gt;= 8, modern browsers), so very old environments would need transpiling.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Which One Should You Use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Personal / Team Preference&lt;/strong&gt;: Some teams prefer the explicit &lt;code&gt;.then()&lt;/code&gt; chaining, especially if they’re used to it. Others love the synchronous style of &lt;code&gt;async/await&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complexity&lt;/strong&gt;: For more complex, branching async flows, &lt;code&gt;async/await&lt;/code&gt; can make the logic significantly easier to read and maintain.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: If you need fine-grained error handling at specific points, &lt;code&gt;try...catch&lt;/code&gt; in &lt;code&gt;async/await&lt;/code&gt; can be more intuitive. If a single &lt;code&gt;.catch()&lt;/code&gt; is enough, Promise chaining might be sufficient.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Both Are Promises&lt;/strong&gt;: Async/Await is syntactic sugar over Promises, so under the hood, they function the same way.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: &lt;code&gt;async/await&lt;/code&gt; often results in code that looks and behaves more like synchronous code, improving readability.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Promise chaining uses &lt;code&gt;.catch()&lt;/code&gt;, whereas &lt;code&gt;async/await&lt;/code&gt; can use multiple &lt;code&gt;try...catch&lt;/code&gt; blocks, providing more control.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment&lt;/strong&gt;: Make sure your environment (or your build setup) supports &lt;code&gt;async/await&lt;/code&gt; if you choose it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ultimately, choosing between Promise chaining and async/await often comes down to preference and code style. Both approaches handle asynchronous operations effectively, so pick the method that best suits your workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Why [4, 11, 2, 23].sort() in JavaScript Isn’t What You Expect</title>
      <dc:creator>Matsu</dc:creator>
      <pubDate>Sun, 05 Jan 2025 00:25:24 +0000</pubDate>
      <link>https://forem.com/devmatsu/why-4-11-2-23sort-in-javascript-isnt-what-you-expect-4njl</link>
      <guid>https://forem.com/devmatsu/why-4-11-2-23sort-in-javascript-isnt-what-you-expect-4njl</guid>
      <description>&lt;p&gt;When sorting arrays in JavaScript, you might expect &lt;code&gt;[4, 11, 2, 23].sort()&lt;/code&gt; to return &lt;code&gt;[2, 4, 11, 23]&lt;/code&gt;. Surprisingly, the result is &lt;code&gt;[11, 2, 23, 4]&lt;/code&gt;. Let’s explore why this happens, and how Unicode code points factor into the comparison.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default Sort Behavior&lt;/strong&gt;&lt;br&gt;
By default, JavaScript’s &lt;code&gt;Array.prototype.sort()&lt;/code&gt; method sorts elements as &lt;strong&gt;strings&lt;/strong&gt; in &lt;strong&gt;ascending Unicode order&lt;/strong&gt;. This means each array element is converted to a string, and the comparison is made based on these string values, character by character.&lt;/p&gt;

&lt;p&gt;Let’s see the array as strings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"4"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"11"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"2"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"23"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When comparing these strings, JavaScript looks at the &lt;strong&gt;first character&lt;/strong&gt; of each string and compares their Unicode code points:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;String&lt;/th&gt;
&lt;th&gt;First Character&lt;/th&gt;
&lt;th&gt;Unicode Code Point&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;"4"&lt;/td&gt;
&lt;td&gt;'4'&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"11"&lt;/td&gt;
&lt;td&gt;'1'&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"2"&lt;/td&gt;
&lt;td&gt;'2'&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"23"&lt;/td&gt;
&lt;td&gt;'2'&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;"11" vs. "2"&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'1' (code 49) vs. '2' (code 50) ⇒ 49 &amp;lt; 50 ⇒ &lt;code&gt;"11"&lt;/code&gt; comes before &lt;code&gt;"2"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;"2" vs. "23"&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both start with '2' (code 50). Then &lt;code&gt;"2"&lt;/code&gt; ends, while &lt;code&gt;"23"&lt;/code&gt; continues with another '3'.&lt;/li&gt;
&lt;li&gt;By string comparison rules, a shorter string that is a prefix of a longer one is considered smaller ⇒ &lt;code&gt;"2"&lt;/code&gt; comes before &lt;code&gt;"23"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;"23" vs. "4"&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'2' (code 50) vs. '4' (code 52) ⇒ 50 &amp;lt; 52 ⇒ &lt;code&gt;"23"&lt;/code&gt; comes before &lt;code&gt;"4"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ultimately, &lt;strong&gt;as strings&lt;/strong&gt;, the sorted order is &lt;code&gt;"11" &amp;lt; "2" &amp;lt; "23" &amp;lt; "4"&lt;/code&gt;, which corresponds to &lt;code&gt;[11, 2, 23, 4]&lt;/code&gt; when converted back to numbers in the array.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Fix It (Sort Numerically)
&lt;/h2&gt;

&lt;p&gt;If you want to sort numbers in &lt;strong&gt;numeric&lt;/strong&gt; order, you need to provide a &lt;strong&gt;compare function&lt;/strong&gt; to the &lt;code&gt;sort()&lt;/code&gt; method. This compare function tells JavaScript how to compare two elements numerically instead of as strings:&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;sort&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;b&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;a&lt;/span&gt; &lt;span class="o"&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;// [2, 4, 11, 23]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;a - b&lt;/code&gt; is negative, a comes before b.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;a - b&lt;/code&gt; is zero, the order of a and b stays as is.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;a - b&lt;/code&gt; is positive, a comes after b.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default String Comparison: JavaScript’s default sort() converts elements to strings and sorts them in ascending Unicode order.&lt;/li&gt;
&lt;li&gt;Numeric Sorting: To sort numbers, provide a compare function—(a, b) =&amp;gt; a - b.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Armed with this knowledge, you can confidently sort arrays in JavaScript, whether you need to sort them as strings or numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console You Later!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>interesting</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
