<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Parth</title>
    <description>The latest articles on DEV Community by Parth (@parthctrl).</description>
    <link>https://dev.to/parthctrl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4026306%2F046cb9f7-8fba-40a0-a536-b37f02853120.jpg</url>
      <title>DEV Community: Parth</title>
      <link>https://dev.to/parthctrl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parthctrl"/>
    <language>en</language>
    <item>
      <title>var, let, const — and why JavaScript changed its mind</title>
      <dc:creator>Parth</dc:creator>
      <pubDate>Sat, 01 Aug 2026 19:16:19 +0000</pubDate>
      <link>https://dev.to/parthctrl/var-let-const-and-why-javascript-changed-its-mind-5f2o</link>
      <guid>https://dev.to/parthctrl/var-let-const-and-why-javascript-changed-its-mind-5f2o</guid>
      <description>&lt;p&gt;When I started learning JavaScript, I kept seeing the same advice: &lt;em&gt;use &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, don't use &lt;code&gt;var&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Usually it came as a rule, not a reason. And a rule you don't understand is a rule you forget — so I went looking for the &lt;em&gt;why&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It turns out &lt;code&gt;var&lt;/code&gt; isn't just old-fashioned. It's broken in three specific ways, and &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; exist to fix exactly those three things.&lt;/p&gt;

&lt;p&gt;Here they are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: var lets you redeclare the same variable
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&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="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&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="s2"&gt;Saini&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;// "Saini"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error. No warning. JavaScript just quietly lets you declare the same variable twice.&lt;/p&gt;

&lt;p&gt;That sounds harmless in a five-line example. Now imagine a 500-line file where you declare &lt;code&gt;var count&lt;/code&gt; at the top, and 300 lines later — having forgotten — you declare &lt;code&gt;var count&lt;/code&gt; again. Your original value is gone, and nothing tells you.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; refuses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="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="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&gt;"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Saini&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// SyntaxError: Identifier 'name' has already been declared&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error &lt;em&gt;is the feature&lt;/em&gt;. It catches the mistake at the moment you make it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: var ignores blocks
&lt;/h2&gt;

&lt;p&gt;A block is anything inside curly braces — an &lt;code&gt;if&lt;/code&gt;, a &lt;code&gt;for&lt;/code&gt;, a bare &lt;code&gt;{ }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You'd expect a variable declared inside a block to stay inside it. &lt;code&gt;var&lt;/code&gt; doesn't care:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&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;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 21 — it escaped!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That variable leaked out of the &lt;code&gt;if&lt;/code&gt; and into the surrounding code. &lt;code&gt;var&lt;/code&gt; is &lt;strong&gt;function-scoped&lt;/strong&gt;, not block-scoped: it only respects the boundaries of a function, and treats &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; blocks as if the braces weren't there.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are &lt;strong&gt;block-scoped&lt;/strong&gt;. They stay where you put them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&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;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Delhi&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;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Delhi" — fine&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;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: city is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That error is the whole point. The variable did its job inside the block and then stopped existing, which is exactly what you wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: var gets quietly moved to the top
&lt;/h2&gt;

&lt;p&gt;This one broke my brain for a while:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined — not an error!&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used a variable &lt;em&gt;before I declared it&lt;/em&gt;, and JavaScript didn't complain. It printed &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason is &lt;strong&gt;hoisting&lt;/strong&gt;. Before running your code, JavaScript scans it and moves all &lt;code&gt;var&lt;/code&gt; declarations to the top of their scope. So what actually runs is closer to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// moved up, no value yet&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// the assignment stays put&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;declaration&lt;/em&gt; moves. The &lt;em&gt;value&lt;/em&gt; doesn't. So you get a variable that exists but is empty — and &lt;code&gt;undefined&lt;/code&gt; instead of a helpful error.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted too, but they refuse to be touched before their declaration line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: Cannot access 'b' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again: &lt;strong&gt;the error is the improvement.&lt;/strong&gt; "Cannot access before initialization" tells you exactly what you did wrong. &lt;code&gt;undefined&lt;/code&gt; tells you nothing, and lets a broken program keep running.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what's the difference between let and const?
&lt;/h2&gt;

&lt;p&gt;Simple version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;let&lt;/code&gt;&lt;/strong&gt; — the value can be changed later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;const&lt;/code&gt;&lt;/strong&gt; — the value cannot be reassigned after you create it.
&lt;/li&gt;
&lt;/ul&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;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// fine&lt;/span&gt;

&lt;span class="kd"&gt;const&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="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&gt;"&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="s2"&gt;Vatsal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// TypeError: Assignment to constant variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But here's the part that trips people up. &lt;code&gt;const&lt;/code&gt; locks the &lt;strong&gt;variable&lt;/strong&gt;, not the &lt;strong&gt;contents&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;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="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vatsal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// this works!&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="c1"&gt;// { name: "Vatsal" }&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="s2"&gt;Aryan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// this doesn't — TypeError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can change what's &lt;em&gt;inside&lt;/em&gt; the object. You just can't point the variable at a &lt;em&gt;different&lt;/em&gt; object.&lt;/p&gt;

&lt;p&gt;Think of &lt;code&gt;const&lt;/code&gt; as gluing a label to a box. You can't move the label to another box — but you can still open the box and rearrange what's inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bonus problem: variables you never meant to create
&lt;/h2&gt;

&lt;p&gt;There's a fourth way to make a variable, and it's an accident:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// no let, no const, no var&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works. JavaScript silently creates a &lt;strong&gt;global&lt;/strong&gt; variable — one that's visible everywhere in your program. Misspell a variable name during an assignment and you don't get an error; you get a brand new global you didn't ask for, while the one you meant to update sits there unchanged.&lt;/p&gt;

&lt;p&gt;This is exactly what &lt;code&gt;"use strict"&lt;/code&gt; was made for:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&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="s2"&gt;Parth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: name is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you still need to write it? Usually not — you're probably already in strict mode without knowing it. ES modules (anything with &lt;code&gt;import&lt;/code&gt;/&lt;code&gt;export&lt;/code&gt;) run in strict mode automatically, and so does most code in React, Next.js, Angular, and modern Node projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I actually took away
&lt;/h2&gt;

&lt;p&gt;I came in expecting "&lt;code&gt;var&lt;/code&gt; is old, &lt;code&gt;let&lt;/code&gt; is new." That's not the story.&lt;/p&gt;

&lt;p&gt;The real story is that &lt;strong&gt;&lt;code&gt;var&lt;/code&gt; fails silently, and &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; fail loudly.&lt;/strong&gt; Redeclaring a variable, leaking one out of a block, using one before it exists — &lt;code&gt;var&lt;/code&gt; allows all three without a word. &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; throw an error at every one.&lt;/p&gt;

&lt;p&gt;That felt backwards to me at first. More errors sounds like a worse language.&lt;/p&gt;

&lt;p&gt;But an error at the moment you make a mistake is a gift. The alternative is &lt;code&gt;undefined&lt;/code&gt; quietly flowing through your program until it finally breaks somewhere far away — with no clue where it came from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;const&lt;/code&gt; by default. Use &lt;code&gt;let&lt;/code&gt; when the value genuinely needs to change. Reach for &lt;code&gt;var&lt;/code&gt; basically never.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And now you know why — not just that.&lt;/p&gt;




&lt;p&gt;What tripped you up most when you were learning scope? For me it was hoisting. I'd love to hear about it in the comments.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>typeof null is "object" — a bug from 1995 nobody can fix</title>
      <dc:creator>Parth</dc:creator>
      <pubDate>Sun, 12 Jul 2026 19:28:22 +0000</pubDate>
      <link>https://dev.to/parthctrl/typeof-null-is-object-a-bug-from-1995-nobody-can-fix-268g</link>
      <guid>https://dev.to/parthctrl/typeof-null-is-object-a-bug-from-1995-nobody-can-fix-268g</guid>
      <description>&lt;p&gt;I'm learning JavaScript. A few weeks in, I ran this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I stared at it for a while.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;null&lt;/code&gt; is not an object. It's the value you use to say &lt;em&gt;there is deliberately nothing here&lt;/em&gt;. It's a primitive — same family as &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;. So why does JavaScript insist on calling it an object?&lt;/p&gt;

&lt;p&gt;I assumed I'd misunderstood something. I hadn't. It's a bug. It's been in the language since 1995, everybody knows about it, and it is never getting fixed.&lt;/p&gt;

&lt;p&gt;Here's the story, because it turned out to be the most interesting thing I've learned so far.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it came from
&lt;/h2&gt;

&lt;p&gt;JavaScript was created by Brendan Eich in about 10 days in 1995. That's not a joke — Netscape wanted a scripting language for their browser and they wanted it immediately.&lt;/p&gt;

&lt;p&gt;When you build a language that fast, you take shortcuts. One of them was how values got stored in memory. Every value carried a small &lt;strong&gt;type tag&lt;/strong&gt; — a few bits at the front saying "this is a number," "this is a string," and so on. To find out a value's type, the engine just looked at the tag.&lt;/p&gt;

&lt;p&gt;The tag for an &lt;strong&gt;object&lt;/strong&gt; was all zeros.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;null&lt;/code&gt;? In that original implementation, &lt;code&gt;null&lt;/code&gt; was represented as a null pointer — which, on the machine, is also all zeros.&lt;/p&gt;

&lt;p&gt;So when &lt;code&gt;typeof&lt;/code&gt; looked at &lt;code&gt;null&lt;/code&gt;, it saw all zeros, checked its table, and confidently reported: &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's it. That's the whole bug. Two different things happened to look identical to the one piece of code that was supposed to tell them apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it was never fixed
&lt;/h2&gt;

&lt;p&gt;This is the part I find genuinely interesting.&lt;/p&gt;

&lt;p&gt;Everyone figured this out pretty quickly. It's not a mystery, and it's not hard to fix — you'd just special-case &lt;code&gt;null&lt;/code&gt; in &lt;code&gt;typeof&lt;/code&gt; and be done in an afternoon.&lt;/p&gt;

&lt;p&gt;But by the time anyone got around to it, the web already existed. Thousands of sites had shipped code that ran &lt;code&gt;typeof x === "object"&lt;/code&gt; and depended on getting &lt;code&gt;true&lt;/code&gt; back for &lt;code&gt;null&lt;/code&gt;. Some of that code was checking whether a value was "object-ish." Some of it was doing something weirder. Nobody knew exactly how much of it was out there, and nobody could go and edit it.&lt;/p&gt;

&lt;p&gt;So fixing the bug would mean breaking working websites. Real ones. Pages that people were using.&lt;/p&gt;

&lt;p&gt;There was actually a proposal, years later, to make &lt;code&gt;typeof null&lt;/code&gt; return &lt;code&gt;"null"&lt;/code&gt;. It was rejected. The reasoning was blunt: it would break too much.&lt;/p&gt;

&lt;p&gt;The bug stayed. Not because it was hard to fix, but because &lt;strong&gt;the cost of fixing it was higher than the cost of living with it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I actually took away from this
&lt;/h2&gt;

&lt;p&gt;I started out thinking this was a piece of JavaScript trivia — the kind of thing that shows up in interview questions and nowhere else.&lt;/p&gt;

&lt;p&gt;But it taught me something bigger, and it's not really about JavaScript:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once people depend on your mistake, it stops being a mistake and becomes a feature you have to support.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's a slightly uncomfortable thought when you're a beginner writing code nobody else will ever run. But it reframed how I read old, weird, "why is it like this" corners of a language. Usually the answer isn't that the designers were careless. It's that they were fast, or early, or working with constraints that no longer exist — and then the world built on top of them before anyone could go back.&lt;/p&gt;

&lt;p&gt;Ten days in 1995. One shortcut. And &lt;code&gt;typeof null === "object"&lt;/code&gt; ever since.&lt;/p&gt;

&lt;h2&gt;
  
  
  The practical bit
&lt;/h2&gt;

&lt;p&gt;If you actually need to check for &lt;code&gt;null&lt;/code&gt;, don't use &lt;code&gt;typeof&lt;/code&gt;. Just compare directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// definitely null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you want to know whether something is a real object, you have to rule &lt;code&gt;null&lt;/code&gt; out yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// an actual object&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That extra &lt;code&gt;value !== null&lt;/code&gt; is in a lot of real codebases. Now you know why it's there.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Have you ever come across a JavaScript behavior that made you stop and think, "Why does it work like that?" I'd love to hear about it in the comments.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
