<?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[Obi franklin's Blog]]></title><description><![CDATA[Obi franklin's Blog]]></description><link>https://obifrankie.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 25 Nov 2026 09:24:36 GMT</lastBuildDate><atom:link href="https://obifrankie.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Variable assignment in JavaScript]]></title><description><![CDATA[JavaScript is definitely a favorite amongst many software developer, it has been around for a while and has seen considerable changes. One those changes…]]></description><link>https://obifrankie.hashnode.dev/variable-assignment-in-javascript</link><guid isPermaLink="true">https://obifrankie.hashnode.dev/variable-assignment-in-javascript</guid><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Obi Franklin]]></dc:creator><pubDate>Thu, 03 Feb 2022 12:50:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1643891467264/XArEH-FvF.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript is definitely a favorite amongst many software developer, it has been around for a while and has seen considerable changes. One those changes being the variable declarations.</p>
<p>Previously the <strong>var</strong> keyword was used, it was the main and only way to declare variables in JavaScript, in recent times the <strong>let</strong> and <strong>const</strong> keywords where also introduced as  keywords for variable declaration in JavaScript. In a perfect world we would call the <strong>var</strong> keyword obsolete but we are not in a perfect world, are we ?. The truth is that some developers still use the <strong>var </strong> keyword for variable declaration till date. What is the difference between this keywords you may ask, well let's find out together shall we.</p>
<p>The <strong>var</strong> keyword is said to be function scope defined which mean that variables defined using the <strong>var</strong> key is accessible outside it's scope while variables defined using the <strong>let</strong> and the <strong>const</strong> keyword are said to be block scoped. Let me explain further</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHi</span>(<span class="hljs-params"></span>)</span>{
       <span class="hljs-keyword">var</span> word <span class="hljs-operator">=</span> <span class="hljs-string">"hi"</span>
       console.log(word)
     }
    sayHi()
</code></pre><p>This will work perfectly without no errors why because the <strong>var</strong> keyword used to declare the variable word makes it accessible outside it's scope. Although the <strong>var</strong> is still very much usable in modern programming, this might lead to an issue later on if you unknowingly reassign  the value of the variable <em>word</em> because you used the <strong>var</strong> keyword you might never find out, till maybe say compile time or after you have launched the project, then you might notice you reassigned the value without knowing.</p>
<p>But with the <strong>let</strong> and <strong>const</strong> keyword the above code snippet will through an error because they are said to be block scoped defined. The <strong>let</strong> and <strong>const</strong> won't allow you access variables outside it's scope. To access the variable you have to access it inside it's scope for example. </p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHi</span>(<span class="hljs-params"></span>)</span>{
       let word <span class="hljs-operator">=</span> <span class="hljs-string">"hi"</span>
       console.log(word)

      sayHi()
     }
</code></pre><p>OR</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHi</span>(<span class="hljs-params"></span>)</span>{
       const word <span class="hljs-operator">=</span> <span class="hljs-string">"hi"</span>
       console.log(word)

      sayHi()
     }
</code></pre><p>So this is the major difference between the <strong>var</strong>, the <strong>let</strong>, and the <strong>const</strong> keywords. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1643891467264/XArEH-FvF.png" alt="Screenshot (19).png" /></p>
]]></content:encoded></item></channel></rss>