<?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: yinsont</title>
    <description>The latest articles on Forem by yinsont (@yinsont).</description>
    <link>https://forem.com/yinsont</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%2F1059610%2Fcea1fe72-4b47-483f-baa4-cf2e9e4da31b.png</url>
      <title>Forem: yinsont</title>
      <link>https://forem.com/yinsont</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yinsont"/>
    <language>en</language>
    <item>
      <title>Declaration of Variables in JS</title>
      <dc:creator>yinsont</dc:creator>
      <pubDate>Sat, 08 Apr 2023 03:11:30 +0000</pubDate>
      <link>https://forem.com/yinsont/declaration-of-variables-in-js-pl4</link>
      <guid>https://forem.com/yinsont/declaration-of-variables-in-js-pl4</guid>
      <description>&lt;p&gt;Variables are one of the main fundamentals of programming. They are used to store data or allocate memory that can later be used to create or do bigger tasks. Such tasks can include but are not limited to: simple math, unlocking doors, etc. Within JavaScript, there are 4 ways to create working variables. They are known as &lt;strong&gt;let&lt;/strong&gt;, &lt;strong&gt;const&lt;/strong&gt;, &lt;strong&gt;var&lt;/strong&gt;, and &lt;strong&gt;none&lt;/strong&gt;.&lt;br&gt;
w3schools.com&lt;br&gt;
mozilla.org&lt;br&gt;
replit&lt;br&gt;
stackoverflow&lt;/p&gt;
&lt;h2&gt;
  
  
  =
&lt;/h2&gt;

&lt;p&gt;Before we get into the variable types, we must learn and understand how to set variables. To do this, we use the &lt;strong&gt;=&lt;/strong&gt; operator. Remember to use only one &lt;strong&gt;=&lt;/strong&gt;. If we were to use &lt;strong&gt;two =&lt;/strong&gt; operators, what would be happening is that rather than assigning/declaring a variable, you would be comparing the two values instead, which then returns a boolean, or a true/false statement. When using &lt;strong&gt;three =&lt;/strong&gt; operators, not only are you comparing the values, but you are also comparing the data types. Again, you would be returned with a boolean of either true/false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'8' == 8
//returns true when log'd

8 === 8
// returns true when log'd

8 === 9
// returns false when log'd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  let
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;let&lt;/strong&gt; is the most commonly used keyword to declare variables with due to its simplicity and ability to be moldable/changeable. This allows the user to alter the value of any variable that has been set with a &lt;strong&gt;let&lt;/strong&gt;. Remember that variables using the &lt;strong&gt;let&lt;/strong&gt; keyword can only be accessible when within the right scope.&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 //assigning variable a to store the value of 5
// returns 5 when log'd

let b = 8 //assigning variable b to store the value of 8
// returns 8 when log'd

b = b + a //change b so that b is equal to b(8) + a(5)
// returns 13 when log'd

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  const
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt; is the second most used keyword when it comes to declaring variables. Unlike the keyword &lt;strong&gt;let&lt;/strong&gt;, &lt;strong&gt;const&lt;/strong&gt; cannot be changed or altered in anyway. Once you set it to an initial value, it cannot be become another value. Although it cannot be mutable like &lt;strong&gt;let&lt;/strong&gt;, it also can only be accessed when within the correct scope, just like &lt;strong&gt;let&lt;/strong&gt;.&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 = 5 //assigning variable a to store the value of 5
// returns 5 when log'd

a + 3 //make a(5) add 3
// returns 5 when log'd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  var
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt; used to be the only keyword used to declare variables, after an update in 2015 which brought &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt; into JavaScript. If used in present day JavaScript, it can lead to bugs and errors so it is best to only use it with old code. Like &lt;strong&gt;let&lt;/strong&gt;, &lt;strong&gt;var&lt;/strong&gt; can also be altered later on in the code you make. &lt;strong&gt;var&lt;/strong&gt; can also be used globally in your code. This means the you can access the variable no matter which scope you are in.&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 = 5//assigning variable a to store the value of 5
// returns 5 when log'd

a + 3 //make a(5) add 3
// returns 8 when log'd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;None&lt;/strong&gt;&lt;br&gt;
Yes, that's right, &lt;strong&gt;nothing&lt;/strong&gt;. Although you can create variables, it is better to declare and assign variables using the previous keywords due to it's common practice and their common usage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 5
// when log'd, a will produce 5
b = 3
// when log'd b will produce 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There you have it, a small rundown on declaring variables in JavaScript. If you're still wondering which keyword to use or how you should be declaring variables, almost always use &lt;strong&gt;let&lt;/strong&gt;. Altering variables within code is common, thus &lt;strong&gt;const&lt;/strong&gt; is not always the best option due to it's ability to not be changeable. Remember to always declare your variables.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
