<?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: Chukwudi Nweze</title>
    <description>The latest articles on DEV Community by Chukwudi Nweze (@onearmycode).</description>
    <link>https://dev.to/onearmycode</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%2F153259%2F7847afb8-8296-434f-8f8f-8dd13536425a.jpg</url>
      <title>DEV Community: Chukwudi Nweze</title>
      <link>https://dev.to/onearmycode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onearmycode"/>
    <language>en</language>
    <item>
      <title>what I didn't know about const variable declaration in JavaScript.</title>
      <dc:creator>Chukwudi Nweze</dc:creator>
      <pubDate>Sun, 10 May 2020 21:20:17 +0000</pubDate>
      <link>https://dev.to/onearmycode/what-i-didn-t-know-about-const-variable-declaration-in-javascript-1cph</link>
      <guid>https://dev.to/onearmycode/what-i-didn-t-know-about-const-variable-declaration-in-javascript-1cph</guid>
      <description>&lt;p&gt;I'm proudly teaching myself coding, on the journey so far, here is what I think I didn't know about const variable declaration&lt;/p&gt;

&lt;p&gt;(1) Const declaration in JavaScript is that  type of variable declaration in which its ''variable assignment'' is set to read-only. This means, value of variable can't be changed once it has  been declared at the declaration time.&lt;/p&gt;

&lt;p&gt;Using the code below to drive home my explanation&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const a = 2;&lt;br&gt;
console.log(a) //2&lt;br&gt;
a = 2;//typeError&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;In line 3 of the snippet above, assigning the variable to another value didn't work. With const, you can't change the value of variable once it's declared.&lt;/p&gt;

&lt;p&gt;(2) Unlike var and let, to get a const declaration with an "undefined" value, you have to declare it manually, const a = undefined &lt;/p&gt;

&lt;p&gt;Consider the code below:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var a; //undefined&lt;br&gt;
let b; //undefined&lt;br&gt;
const c; //missing initialisation in const declaration&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;now we declare it manually&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const c = undefined;&lt;br&gt;
console.log(c)  //undefined&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now from the above example shown so far, should I think declaring a variable with  const  restricts the value of the variable itself?  Well, this could have been my assumption, if I didn't read further. The answer, however, is simply No!&lt;/p&gt;

&lt;p&gt;Const is not a restriction on the value itself but on the variable assignment of the value. This means that the value of const can be modified. Only the variable assignment of the value is frozen, and can't be modified.&lt;br&gt;
Consider the code below&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const a = [1,2,3];&lt;br&gt;
a.push(4);&lt;br&gt;
console.log(a)//[1,2,3,4]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Wow, the value could be modified, and not read-only. Now what exactly do I mean by variable assignment of a const is immutable/frozen. Let's revisit our snippet above&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const a = [1,2,3];&lt;br&gt;
a.push(4)&lt;br&gt;
console.log(a)//[1,2,3,4] as expected&lt;br&gt;
//now assigning a variable to a new value&lt;br&gt;
a = 2; //typeError!&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Reassigning a to 42 didn't work simply because the variable assignment of const is frozen. Finally, we can say that the value of const itself is mutable but the variable, a reference to the value is immutable.&lt;/p&gt;

&lt;p&gt;I'm excited about my journey so far and this is my first publication on this platform&lt;/p&gt;

</description>
      <category>javascriptwebdev</category>
    </item>
  </channel>
</rss>
