<?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: Lovoo</title>
    <description>The latest articles on Forem by Lovoo (@lubbaa).</description>
    <link>https://forem.com/lubbaa</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%2F439288%2F227ba209-8d2d-4f96-981e-b3d37e71e472.jpg</url>
      <title>Forem: Lovoo</title>
      <link>https://forem.com/lubbaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lubbaa"/>
    <language>en</language>
    <item>
      <title>Why const Doesn’t Freeze Your JavaScript Arrays</title>
      <dc:creator>Lovoo</dc:creator>
      <pubDate>Thu, 11 Dec 2025 03:12:51 +0000</pubDate>
      <link>https://forem.com/lubbaa/why-const-doesnt-freeze-your-javascript-arrays-3j93</link>
      <guid>https://forem.com/lubbaa/why-const-doesnt-freeze-your-javascript-arrays-3j93</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Intro - The Common Confusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using const with an array feels like it should lock the whole thing in place. Then you try push(), pop(), or update a value, and it still changes. If it’s constant, why is it still changing?&lt;/p&gt;

&lt;p&gt;The answer lies in how JavaScript handles variables and references. Once you see that distinction, this behavior becomes far less mysterious and a lot more logical.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Binding vs Value — What const Actually Does&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A const variable is like a signpost planted firmly in the ground. You can’t move the signpost or point it somewhere else, but whatever it’s pointing at can shift around freely.&lt;/p&gt;

&lt;p&gt;Primitives behave like solid stones once placed; they don’t change shape. Arrays and objects are more like workbenches: the signpost stays fixed, but the tools and pieces on the bench can be rearranged, added, or removed.&lt;/p&gt;

&lt;p&gt;That’s the heart of it. const protects the pointer, not the thing it points to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Mutable vs Immutable Data Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primitives in JavaScript act like sealed envelopes; you can read what’s inside, but you can’t change the contents. If you need something different, you create a whole new envelope.&lt;/p&gt;

&lt;p&gt;Objects and arrays work more like open notebooks. You can write, erase, add notes, or shuffle things around on the same pages.&lt;/p&gt;

&lt;p&gt;This difference explains why a const variable can point to data that still changes in place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Concrete Examples — What You Can and Can’t Do&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;const locks the variable binding, not the value itself. That means you can change the contents of arrays or objects, but you cannot reassign the variable to something new.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];

// ✅ Allowed: mutating the array
numbers.push(4);
numbers[0] = 10;
console.log(numbers); // [10, 2, 3, 4]

// ❌ Not allowed: reassigning the variable
numbers = [5, 6]; // TypeError: Assignment to constant variable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: "Alice", age: 25 };

// ✅ Allowed: modifying properties
person.age = 26;
person.city = "Toronto";
console.log(person); // { name: "Alice", age: 26, city: "Toronto" }

// ❌ Not allowed: reassigning the variable
person = { name: "Bob" }; // TypeError
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These examples show the key difference: the variable itself is constant, but the data it points to can be updated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. When Immutability Is Needed&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you really don’t want data to change, like shared state in apps or functional programming. const alone won’t stop mutations.&lt;/p&gt;

&lt;p&gt;Use Object.freeze() to lock an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Freeze an object so it can’t be changed
const person = Object.freeze({ name: "Alice", age: 25 });
person.age = 26; // ❌ ignored in strict mode

// Use libraries for deeper immutability
// e.g., Immer or Immutable.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locking data helps prevent bugs and keeps your code predictable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Conclusion &amp;amp; Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use const by default; it locks the variable, not the data. Arrays and objects can still change unless you freeze them. Clear naming and immutable patterns make your code predictable and easier to maintain. Keep these simple rules, and JavaScript stops feeling so tricky.&lt;/p&gt;

&lt;p&gt;References / Further Reading&lt;br&gt;
    • &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const" rel="noopener noreferrer"&gt;MDN Web Docs — ￼const&lt;/a&gt;￼ — Official docs explaining variable bindings in JavaScript.&lt;br&gt;
    • &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Immutable" rel="noopener noreferrer"&gt;MDN Web Docs — Immutable / Mutable&lt;/a&gt;￼ — Glossary and explanation of mutable vs immutable data.&lt;br&gt;
    • &lt;a href="https://www.geeksforgeeks.org/javascript/javascript-const/" rel="noopener noreferrer"&gt;GeeksforGeeks — JavaScript ￼const&lt;/a&gt;￼ — Beginner-friendly explanation with examples.&lt;br&gt;
    • &lt;a href="https://www.freecodecamp.org/news/differences-between-var-let-const-javascript/" rel="noopener noreferrer"&gt;freeCodeCamp — Differences between ￼var￼, ￼let￼, and ￼const&lt;/a&gt;￼ — Practical guide and code examples.&lt;br&gt;
    • &lt;a href="https://stackoverflow.com/questions/42833540/keyword-const-does-not-make-the-value-immutable-what-does-it-mean" rel="noopener noreferrer"&gt;Stack Overflow — Keyword ￼const￼ does not make the value immutable&lt;/a&gt;￼ — Community discussion clarifying common confusion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>web3</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
