<?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: Damith Samarasighe</title>
    <description>The latest articles on DEV Community by Damith Samarasighe (@damith08).</description>
    <link>https://dev.to/damith08</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%2F1102013%2F48d38de7-13ca-416e-988d-6918c049afa2.jpeg</url>
      <title>DEV Community: Damith Samarasighe</title>
      <link>https://dev.to/damith08</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damith08"/>
    <language>en</language>
    <item>
      <title>JavaScript var, let &amp; const</title>
      <dc:creator>Damith Samarasighe</dc:creator>
      <pubDate>Wed, 03 Jan 2024 04:44:07 +0000</pubDate>
      <link>https://dev.to/damith08/javascript-var-let-const-ibn</link>
      <guid>https://dev.to/damith08/javascript-var-let-const-ibn</guid>
      <description>&lt;h2&gt;
  
  
  JavaScript Variables
&lt;/h2&gt;

&lt;p&gt;In programming we use variables for store data. Basically variables are like containers which we can store something init.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "John";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;name&lt;/code&gt; is a variable which storing John as it's value&lt;/p&gt;

&lt;h2&gt;
  
  
  Declare Variables in JavaScript
&lt;/h2&gt;

&lt;p&gt;We can declare variables with &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt; keywords. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a;
let b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In here &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;var&lt;/code&gt; Vs &lt;code&gt;let&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; are use to declare variables. However there are some differences.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Scope:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;var&lt;/code&gt; is only available within the function where it is declared. which means &lt;code&gt;var&lt;/code&gt; is "function-scoped". if declare outside any function, it becomes a global variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt; is limited to the block where it declared. which means &lt;code&gt;let&lt;/code&gt; is "block-scoped".&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Hoisting:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; are hoisted to the top of their scope during the compilation phase. This means you can use the variable before it is declared in the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; are also hoisted, but they are not initialized until the interpreter reaches the line where the variable is declared. Accessing a let variable before its declaration results in a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Re-declaration:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;With &lt;code&gt;var&lt;/code&gt; you can re-declare a variable within the same scope without any errors. The new declarations overrides the previous one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With &lt;code&gt;let&lt;/code&gt; when you re-declared a variable in the same scope, it will gives you &lt;code&gt;SyntaxEror&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, it is recommended we use &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt; because &lt;code&gt;var&lt;/code&gt; is used in the older versions of JavaScript and &lt;code&gt;let&lt;/code&gt; is the new way of declaring variables starting &lt;strong&gt;ES6 (ES2015)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialize Variables
&lt;/h2&gt;

&lt;p&gt;we can use the assignment operator &lt;code&gt;=&lt;/code&gt; to assign a value to a variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a;
a = 5; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now variable &lt;code&gt;a&lt;/code&gt; has the value of &lt;strong&gt;5&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can also initialize variables during its declaration. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 5;
let b = 6;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also declare variables in a single statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 5, b = 6, c = 7;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript &lt;code&gt;const&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; keyword was also introduce in the &lt;strong&gt;ES6(ES2015)&lt;/strong&gt; version to create constants. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = 6;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a constant initialized, we cannot change its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = 6;
// a = 8; // Error: Assignment to constant variable (reassigning is not allowed)

console.log(a); // Output: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically constant is a type of variable whose values cannot be change.&lt;br&gt;
Also we cannot declare constant without initializing it.&lt;/p&gt;

&lt;p&gt;In summary, understanding the nuances of &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; allows developers to use the right tool for the job, developing cleaner and more maintainable JavaScript code. &lt;/p&gt;

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