<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Vivek Pandey's Blog]]></title><description><![CDATA[Web developer who loves to build apps which'll help millions, if not billions. When l am not working, am usually with my three year Labrador, who loves to run a]]></description><link>https://blog.vivekpandey.in</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1625395460244/3ff0ZVZ2K.png</url><title>Vivek Pandey&apos;s Blog</title><link>https://blog.vivekpandey.in</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 09:10:25 GMT</lastBuildDate><atom:link href="https://blog.vivekpandey.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Bundlers Like Webpack and Vite: What Developers Miss]]></title><description><![CDATA[Bundlers don't "combine" your files. They map dependencies and output optimized chunks.
Most developers think bundlers work like this: "I run webpack, it reads all my files, merges them together, and outputs one big bundle."
It doesn't work that way....]]></description><link>https://blog.vivekpandey.in/bundlers-like-webpack-and-vite-what-developers-miss</link><guid isPermaLink="true">https://blog.vivekpandey.in/bundlers-like-webpack-and-vite-what-developers-miss</guid><category><![CDATA[webpack]]></category><category><![CDATA[vite]]></category><category><![CDATA[Bundler]]></category><dc:creator><![CDATA[Vivek]]></dc:creator><pubDate>Mon, 03 Nov 2025 13:55:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762178036278/50ab42c7-7c8d-41a8-84ae-09886c55bf1b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Bundlers don't "combine" your files. They map dependencies and output optimized chunks.</strong></p>
<p>Most developers think bundlers work like this: "I run webpack, it reads all my files, merges them together, and outputs one big bundle."</p>
<p>It doesn't work that way. Bundlers don't merge. They analyze relationships, split intelligently, and create multiple output files designed for modern browsers and caching strategies.</p>
<h2 id="heading-1-the-core-idea-the-module-graph">1. The Core Idea: The Module Graph</h2>
<p>At the heart of webpack and Vite is a <strong>module graph</strong>—a complete map of every file and how it depends on others.</p>
<p>When you tell webpack to build, it doesn't immediately process files. Instead:</p>
<ul>
<li><p>It starts at your <strong>entry point</strong> (the main file you specify).</p>
</li>
<li><p>It parses that file and finds every <code>import</code> and <code>require</code> statement.</p>
</li>
<li><p>It recursively follows each dependency to build a complete graph.</p>
</li>
<li><p>Each file becomes a <strong>node</strong>. Each <code>import</code> relationship becomes an <strong>edge</strong>.</p>
</li>
</ul>
<p>Webpack's official documentation calls this "recursively building a dependency graph that includes every module your application needs."</p>
<p>Why?<br />Because everything else—splitting, tree-shaking, optimization—depends on understanding this map first.</p>
<p>The module graph is the <strong>source of truth</strong>. Everything else is just processing this graph intelligently.</p>
<h2 id="heading-2-the-build-isnt-magicits-four-distinct-steps">2. The Build Isn't Magic—It's Four Distinct Steps</h2>
<p>Developers imagine bundlers work in one mysterious phase: "running webpack."</p>
<p>Actually, bundlers work in four clear, isolated stages:</p>
<p><strong>Parse:</strong> Read and understand your JavaScript. Webpack parses the syntax and extracts import/require statements.</p>
<p><strong>Resolve:</strong> Convert partial paths like <code>./utils</code> or <code>lodash</code> into actual file locations on disk. Check if files exist. This is why webpack can find <code>node_modules</code> packages.</p>
<p><strong>Transform:</strong> Use loaders and plugins to convert non-JavaScript assets. TypeScript becomes JavaScript. SCSS becomes CSS. JSX becomes regular functions. Each file gets individually transformed.</p>
<p><strong>Bundle:</strong> Take the transformed module graph and generate output files (called "chunks"). Decide which modules go in which chunk based on your configuration.</p>
<p>These stages are sequential but independent. Each stage receives input and produces output that feeds the next stage. This is why bundlers are so extendable—plugins and loaders hook into specific stages and modify behavior.</p>
<h2 id="heading-3-bundlers-never-execute-your-codethey-only-analyze-it">3. Bundlers Never Execute Your Code—They Only Analyze It</h2>
<p>This is crucial and misunderstood: <strong>Bundlers do not run your code.</strong></p>
<p>When webpack processes <code>const result = expensiveCalculation()</code>, it doesn't calculate anything. It just sees:</p>
<ul>
<li><p>There's a variable named <code>result</code>.</p>
</li>
<li><p>It's assigned the return value of a function call.</p>
</li>
</ul>
<p>Bundlers use <strong>static analysis</strong>—understanding code structure without running it. This is possible because webpack looks at the static structure of ES2015 <code>import</code> and <code>export</code> statements.</p>
<p>Tree-shaking (removing unused code) works <em>because</em> bundlers can statically analyze which exports are used and which aren't, without executing anything.</p>
<p>Dynamic things break this:</p>
<ul>
<li><p><code>require(someVariable)</code> is opaque—bundlers can't know what file path the variable contains, so they conservatively include possibilities.</p>
</li>
<li><p>This is why bundlers warn about dynamic requires and why developers avoid them in production code.</p>
</li>
</ul>
<p>Static analysis makes bundlers predictable, fast, and capable of optimization. But it also explains their limitations.</p>
<h2 id="heading-4-output-multiple-chunks-not-one-file">4. Output: Multiple Chunks, Not One File</h2>
<p>Here's the misconception: "Webpack outputs a bundle"—singular.</p>
<p>Actually, webpack outputs <strong>one or more chunks</strong>. The relationship between your code and output can be 1:1, 1:many, or many:1.</p>
<p>By default, webpack creates:</p>
<ul>
<li><p><strong>One main chunk</strong> containing your application code.</p>
</li>
<li><p><strong>Separate chunks</strong> for code you dynamically import using <code>import()</code>.</p>
</li>
<li><p><strong>Vendor chunks</strong> (if configured) containing <code>node_modules</code> code.</p>
</li>
</ul>
<p>Why multiple chunks?<br />Because <strong>code splitting</strong> allows the browser to:</p>
<ul>
<li><p>Load only the code needed for the current page.</p>
</li>
<li><p>Reuse cached chunks across pages.</p>
</li>
<li><p>Parallelize downloads.</p>
</li>
</ul>
<p>Each chunk gets a <strong>unique name with a hash</strong>, like <code>main.a3b4c5d6.js</code>. This hash is content-based—if the chunk changes, webpack generates a new filename.</p>
<p>The browser never needs to refetch <code>main.a3b4c5d6.js</code> because the filename itself guarantees its content never changes. You can set <code>Cache-Control: max-age=31536000</code> (one year), and the browser will cache it forever. If code changes, webpack generates <code>main.e7f8g9h0.js</code>, and the browser fetches the new file.</p>
<p>This chunking strategy is why webpack can scale to massive applications—unchanged code stays cached, and only changed chunks need redownloading.</p>
<h2 id="heading-5-vites-speed-secret-native-esm-on-demand-transformation">5. Vite's Speed Secret: Native ESM + On-Demand Transformation</h2>
<p>Webpack builds everything upfront. In large projects, this can mean waiting 5-30 seconds even during development.</p>
<p><strong>Vite is different because it exploits native ES modules in browsers.</strong></p>
<p>Here's how:</p>
<p>When you run <code>vite dev</code>:</p>
<ul>
<li><p>Vite <strong>does not</strong> build your entire project.</p>
</li>
<li><p>Instead, it starts a dev server that serves files as ES modules.</p>
</li>
<li><p>When your browser requests a file, Vite transforms <em>only that file</em> and returns it.</p>
</li>
<li><p>Because browsers support native <code>&lt;script type="module"&gt;</code>, the browser understands ES module syntax directly.</p>
</li>
</ul>
<p>For dependencies in <code>node_modules</code> (which are often CommonJS), Vite uses <strong>esbuild to pre-bundle them once</strong> into ESM format. This pre-bundling happens only on first startup or when dependencies change, not on every reload.</p>
<p>When you save a file:</p>
<ul>
<li><p>Vite detects the change.</p>
</li>
<li><p>It transforms only the changed file and its immediate dependents.</p>
</li>
<li><p>It sends an update via WebSocket to the browser.</p>
</li>
<li><p>The browser replaces the old module without a full reload—<strong>Hot Module Replacement (HMR)</strong>.</p>
</li>
</ul>
<p>This on-demand transformation is why Vite feels "instant" during development. No full rebuild on every keystroke.</p>
<p>However, <strong>for production, Vite uses Rollup</strong> (not esbuild). Rollup is optimized for generating efficient, optimized bundles with advanced code-splitting strategies—things that matter in production but are unnecessary during development.</p>
<h2 id="heading-6-loaders-transform-plugins-extend">6. Loaders Transform, Plugins Extend</h2>
<p>Both webpack and Vite rely on an ecosystem of <strong>loaders</strong> and <strong>plugins</strong> to handle everything outside vanilla JavaScript.</p>
<p><strong>Loaders</strong> transform individual files:</p>
<ul>
<li><p><code>babel-loader</code> converts ES6+ to ES5.</p>
</li>
<li><p><code>ts-loader</code> converts TypeScript to JavaScript.</p>
</li>
<li><p><code>sass-loader</code> converts SCSS to CSS.</p>
</li>
<li><p>Loaders work at the file level—each file is independently transformed.</p>
</li>
</ul>
<p><strong>Plugins</strong> operate at the bundle and compilation level:</p>
<ul>
<li><p>They hook into webpack's lifecycle events (like <code>compilation</code>, <code>optimize</code>, <code>emit</code>).</p>
</li>
<li><p>They can modify the final output, generate new files, or inspect the entire module graph.</p>
</li>
<li><p>A plugin might minify code, extract CSS, generate an HTML file, or analyze bundle size.</p>
</li>
</ul>
<p>This plugin system is why bundlers have lasted so long despite JavaScript ecosystem churn. New frameworks (React, Vue, Svelte) simply write loaders/plugins, and bundlers automatically support them.</p>
<h2 id="heading-7-why-bundlers-scale-so-insanely-well">7. Why Bundlers Scale So Insanely Well</h2>
<p>Bundlers handle modern applications because:</p>
<ul>
<li><p><strong>Module graphs are deterministic:</strong> Given the same source code, the graph is always the same. No surprises.</p>
</li>
<li><p><strong>Parsing is fast:</strong> Modern parsers like esbuild can parse thousands of files per second.</p>
</li>
<li><p><strong>Static analysis enables optimization:</strong> Without running code, bundlers can eliminate dead code, split intelligently, and cache efficiently.</p>
</li>
<li><p><strong>Caching strategies are built-in:</strong> Hash-based filenames + browser caching + chunking = only changed code is re-downloaded.</p>
</li>
<li><p><strong>On-demand transformation (Vite):</strong> Don't transform what the browser won't request.</p>
</li>
<li><p><strong>Parallel processing:</strong> Modern bundlers parallelize parsing, transforming, and bundling across CPU cores.</p>
</li>
</ul>
<p>The result?</p>
<p>Webpack can process projects with tens of thousands of files. Vite can handle the same projects with sub-second dev server feedback.</p>
<h2 id="heading-conclusion-bundlers-are-deterministic-build-engines">Conclusion: Bundlers Are Deterministic Build Engines</h2>
<p>Bundlers aren't magic. They're consistent, powerful processors that:</p>
<ul>
<li><p>Map dependencies into a module graph.</p>
</li>
<li><p>Transform code through predictable stages.</p>
</li>
<li><p>Analyze statically, never executing your code.</p>
</li>
<li><p>Generate optimized, cacheable output chunks.</p>
</li>
</ul>
<p>Understanding these fundamentals—the graph, the stages, the static analysis, the chunking strategy—means you can:</p>
<ul>
<li><p>Debug configuration issues.</p>
</li>
<li><p>Optimize bundle size.</p>
</li>
<li><p>Choose the right tool for your project.</p>
</li>
<li><p>Extend bundlers for custom needs.</p>
</li>
</ul>
<p>The reason bundlers work at scale is simple: they're not doing anything fancier than <strong>deterministic graph mapping + clever caching + parallel processing</strong>. But that simplicity, combined with extensibility, makes them powerful enough to handle the entire modern JavaScript ecosystem.</p>
]]></content:encoded></item><item><title><![CDATA[Covert your React app into native app (Android & iOS)]]></title><description><![CDATA[For this tutorial, we'll be using  capacitorjs  to convert our react app into Android and iOS build. Let me start by why do we need it?
Why do we need capacitorjs?
CapacitorJS is generally used to convert our existing web apps built with Vue.js or Re...]]></description><link>https://blog.vivekpandey.in/covert-your-react-app-into-native-app-android-and-ios</link><guid isPermaLink="true">https://blog.vivekpandey.in/covert-your-react-app-into-native-app-android-and-ios</guid><category><![CDATA[React]]></category><category><![CDATA[Android]]></category><category><![CDATA[iOS]]></category><dc:creator><![CDATA[Vivek]]></dc:creator><pubDate>Mon, 15 Nov 2021 17:44:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1636998167975/SwceXj-1v.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For this tutorial, we'll be using  <a target="_blank" href="https://capacitorjs.com/">capacitorjs </a> to convert our react app into Android and iOS build. Let me start by why do we need it?</p>
<h1 id="why-do-we-need-capacitorjs">Why do we need capacitorjs?</h1>
<p>CapacitorJS is generally used to convert our existing web apps built with Vue.js or React to mobile apps, this tool has been designed to drop into any existing modern JavaScript web application. If you have this kind of app already or you want to start from scratch to the design a native mobile app on iOS and Android, this framework will help you to use familiar web languages to access native features like the Camera, Location or storage access without dealing with the complexity of the native code.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1636997686947/q8nke2URt.png" alt="sample.png" /></p>
<h2 id="steps-to-create-native-apps">Steps to create native apps</h2>
<ol>
<li><code>npm install @capacitor/cli @capacitor/core</code> </li>
<li><code>npx cap init</code></li>
<li><code>npm install @capacitor/ios @capacitor/android</code></li>
<li><code>npx cap add ios</code> </li>
<li><code>npx cap add android</code></li>
</ol>
<p>That's it, you're done creating native apps.</p>
<h2 id="what-next">What next?</h2>
<h3 id="environment-setup">Environment setup</h3>
<p>You'll notice that you have two folders in your project directory named <code>ios</code> and <code>android</code>. These folders contain the native code for the respective platforms. Now you must be wondering how can I run these apps? If you've some exposure to android and iOS development, you might have the environment already setup. If not, it's simple just follow these  <a target="_blank" href="https://capacitorjs.com/docs/getting-started/environment-setup">steps</a>. </p>
<h3 id="running-the-apps">Running the apps</h3>
<ul>
<li>Android: <code>npx cap run android</code></li>
<li>iOS: <code>npx cap run ios</code></li>
</ul>
<p>Voila you've built the native apps in no time.</p>
<h3 id="solving-issues-related-to-android-and-ios-app-not-running">Solving issues related to android and iOS app not running</h3>
<p>Open the apps in native code editor, it'll fix the build automatically for you. Here's the command for the same, <code>npx cap open android</code> or <code>npx cap open ios</code>.</p>
<h2 id="what-else-can-be-done">What else can be done?</h2>
<p>You can request for permission without dealing with native code like the example given below for notifications:</p>
<pre><code><span class="hljs-string">import</span> { <span class="hljs-string">LocalNotifications</span> } <span class="hljs-string">from</span> <span class="hljs-string">'@capacitor/local-notifications'</span><span class="hljs-string">;</span>

<span class="hljs-string">LocalNotifications.schedule({</span>
  <span class="hljs-attr">notifications:</span> [
    {
      <span class="hljs-attr">title:</span> <span class="hljs-string">"On sale"</span>,
      <span class="hljs-attr">body:</span> <span class="hljs-string">"Widgets are 10% off. Act fast!"</span>,
      <span class="hljs-attr">id:</span> <span class="hljs-number">1</span>,
      <span class="hljs-attr">schedule:</span> { <span class="hljs-attr">at:</span> <span class="hljs-string">new</span> <span class="hljs-string">Date(Date.now()</span> <span class="hljs-string">+</span> <span class="hljs-number">1000</span> <span class="hljs-string">*</span> <span class="hljs-number">5</span><span class="hljs-string">)</span> },
      <span class="hljs-attr">sound:</span> <span class="hljs-literal">null</span>,
      <span class="hljs-attr">attachments:</span> <span class="hljs-literal">null</span>,
      <span class="hljs-attr">actionTypeId:</span> <span class="hljs-string">""</span>,
      <span class="hljs-attr">extra:</span> <span class="hljs-literal">null</span>
    }
  ]
<span class="hljs-string">});</span>
</code></pre><p>Camera:</p>
<pre><code><span class="hljs-keyword">import</span> { Camera, CameraResultType } from <span class="hljs-string">'@capacitor/camera'</span>;

<span class="hljs-comment">// Take a picture or video, or load from the library</span>
<span class="hljs-keyword">const</span> picture = <span class="hljs-keyword">await</span> Camera.getPicture({
  resultType: CameraResultType.<span class="hljs-built_in">Uri</span>
});
</code></pre><h2 id="closing-thoughts">Closing thoughts</h2>
<p>Your imagination is the only limit, you can also inject native code using capacitorjs. It's swiss knife for quick builds. At last, I hope this helps someone. Cheers! Feel free to ask any questions.</p>
]]></content:encoded></item><item><title><![CDATA[Should you follow TDD?]]></title><description><![CDATA[Let’s make sense
Test-driven development (aka TDD) is a three-step process. It’s often referred to as the “red, green, refactor cycle”

Let me explain the process:

Red: It involves writing a function to test a feature which you’re going to add in th...]]></description><link>https://blog.vivekpandey.in/should-you-follow-tdd</link><guid isPermaLink="true">https://blog.vivekpandey.in/should-you-follow-tdd</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Testing]]></category><category><![CDATA[TDD (Test-driven development)]]></category><dc:creator><![CDATA[Vivek]]></dc:creator><pubDate>Tue, 18 May 2021 10:37:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625394696472/Qf4AGRKvj.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="lets-make-sense">Let’s make sense</h2>
<p>Test-driven development (aka TDD) is a three-step process. It’s often referred to as the “red, green, refactor cycle”</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1625394743586/DghXniQNu.png" alt="TDD.png" /></p>
<p>Let me explain the process:</p>
<ul>
<li><strong>Red</strong>: It involves writing a function to test a feature which you’re going to add in the project/app. It’ll fail obviously and you get a red error message.</li>
</ul>
<ul>
<li><strong>Green</strong>: Next up, we add the functionality/feature to the app in-order to pass the test and you get a green success message.</li>
</ul>
<ul>
<li><strong>Refactor</strong>: Now we need to look at the code and refactor it to ensure it’s well-written, and easy to read/understand.</li>
</ul>
<blockquote>
<p>You repeat the same process over and over again until you have added all the required functionality.</p>
</blockquote>
<p><strong>Bonus</strong>: Using this cycle saves you if you break something while implementing other functionalities.</p>
<h2 id="when-does-it-make-sense">When does it make sense?</h2>
<ul>
<li><p>Utility Functions: If I’ve a function which has a certain set of inputs or ouputs, and of a certain complexity. I prefer setting up a test for it.</p>
</li>
<li><p>Well defined UI: If I’m trying to achieve certain UI goals in my app, I prefer setting up a test for it.</p>
</li>
</ul>
<ul>
<li>Fixing a bug: This also helps me in replicating the issue so that I can understand the problem better.</li>
</ul>
<h3 id="but-when-it-doesnt">But when it doesn’t?</h3>
<ul>
<li>When you’re learning about new features and implementation (aka exploratory coding), I wouldn’t follow this cycle.</li>
</ul>
<ul>
<li>It is waste of your precious time if you’re testing the implementation details.</li>
</ul>
<h2 id="my-conclusion">My conclusion</h2>
<p>Go follow TDD if you believe you’ll be working on the project in the long run. Because I believe Time is an important resource, and you should utilize it to the fullest. Good luck!</p>
]]></content:encoded></item><item><title><![CDATA[What's better to wrap a SVG - Object, or Img?]]></title><description><![CDATA[To be exact I wrapped the SVG in Img tag and went with it. It worked obviously, but it had its own shortcomings. This is what I’m going to discuss today.
Let’s get started with discussing the cons of wrapping a img tag:

Limited CSS based styling.


...]]></description><link>https://blog.vivekpandey.in/whats-better-to-wrap-a-svg-object-or-img</link><guid isPermaLink="true">https://blog.vivekpandey.in/whats-better-to-wrap-a-svg-object-or-img</guid><category><![CDATA[HTML5]]></category><dc:creator><![CDATA[Vivek]]></dc:creator><pubDate>Sat, 15 May 2021 10:20:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625394301556/5DOlfuQvZ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>To be exact I wrapped the SVG in <strong>Img</strong> tag and went with it. It worked obviously, but it had its own shortcomings. This is what I’m going to discuss today.</p>
<p>Let’s get started with discussing the cons of wrapping a <strong>img</strong> tag:</p>
<ul>
<li>Limited CSS based styling.</li>
</ul>
<ul>
<li><p>In-line styling is required for some of them to work.</p>
</li>
</ul>
<ul>
<li><p>External Stylesheets have no effect on them except for few basic modifications.</p>
</li>
</ul>
<h2 id="what-should-i-use-then">What should I use then?</h2>
<p>Let’s take a look at the syntax for both of of them:</p>
<p>-
Image Tag</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./sample.svg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Sample SVG"</span> /&gt;</span>
</code></pre><p>-
 Object Tag</p>
<pre><code>&lt;<span class="hljs-keyword">object</span> data="sample.svg" <span class="hljs-keyword">type</span>="image/svg+xml"&gt; &lt;img src="fallback.jpg" /&gt; &lt;/<span class="hljs-keyword">object</span>&gt;
</code></pre><blockquote>
<p>If you just need the SVG use the img tag, otherwise for an interactive SVG use object tag.</p>
</blockquote>
<p><strong>Why I used img inside the object tag?</strong></p>
<p>It’s called a fallback image incase there’s an issue loading the SVG. The fallback png or jpg is used to replace it. Sounds amazing right. That’s one of the cool benefit of using a object tag.</p>
<p><strong>My Conclusion</strong></p>
<p>If I were to use a SVG in a everyday scenario, I would use object tag over other available tags like img, embed, or iframe.</p>
]]></content:encoded></item><item><title><![CDATA[What to use Let or Const?]]></title><description><![CDATA[It seems that netizens believe that one should use const wherever possible, only falling back to let where necessary, as can be enforced with the prefer-const ESLint rule.

Let vs Const vs Var: We generally want to use let. If we’re not using the sam...]]></description><link>https://blog.vivekpandey.in/what-to-use-let-or-const</link><guid isPermaLink="true">https://blog.vivekpandey.in/what-to-use-let-or-const</guid><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Vivek]]></dc:creator><pubDate>Wed, 12 May 2021 09:39:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625393060502/7tiGLkO5l.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It seems that netizens believe that one should use const wherever possible, only falling back to let where necessary, as can be enforced with the prefer-const ESLint rule.</p>
<blockquote>
<p>Let vs Const vs Var: We generally want to use let. If we’re not using the same, we use const. (I’ve have seen codebases which force you to use const when there is only one assignment, same goes for some of the devs out there.)</p>
</blockquote>
<h2 id="why-prefer-const">Why prefer const?</h2>
<ul>
<li><p>Performance Benefits: There are some claims out there which say using const makes code faster because it knows there won’t be any re-assignment.</p>
</li>
<li><p>One Way to Do It: Sometimes it’s a bias in our head. A rule like “always use const where it works” stop us from thinking about it and can be enforced by a linter nowadays.</p>
</li>
<li><p>Reassignments May Cause Bugs: In the long-run, there can be instances where we may forget about reassignment of the variables. Therefore, const gives you confidence you’ll always “see” the same value and there will be no bug.</p>
</li>
<li><p>Learning About Mutation: When I got started with JavaScript, I got consfused with the const &amp; immutability. It’s important to learn the difference between variable mutation and assignment anyway, and preferring const forces you to confront this distinction early on.</p>
</li>
</ul>
<h2 id="why-not-prefer-const">Why Not prefer const?</h2>
<ul>
<li><p>Confusion with Immutability: In every conversation over const, you’ll find someone who confuses with immutability. This happens because assignment and mutation use the same '=' operator.</p>
</li>
<li><p>Pressure to Avoid Redeclaring: A const based codebase creates a pressure to not use let for conditionally assigned variables. For example, you might write const a = condition ? b : c instead of an if condition, even if both b and c branches are convoluted and giving them explicit names is awkward.</p>
</li>
<li><p>Loss of Intent: If we use const everywhere, we lose the ability to clarify when something is important to not be reassigned.</p>
</li>
</ul>
<h2 id="what-is-my-conclusion">What is my Conclusion?</h2>
<p>Well I don’t have a opinion on this.</p>
<p>I would use whatever I need as per my situation. If you care, use a linter that automates checking and fixing this issue so that changing let to const doesn’t become a delay in code review.</p>
<p>Finally, remember that linters exist to serve you. Learn from your own mistakes.</p>
]]></content:encoded></item></channel></rss>