<?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: Mansoor Ahmad</title>
    <description>The latest articles on DEV Community by Mansoor Ahmad (@manahmad).</description>
    <link>https://dev.to/manahmad</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%2F131026%2F6ef77bb3-4969-45b2-b094-8c9974744ef3.png</url>
      <title>DEV Community: Mansoor Ahmad</title>
      <link>https://dev.to/manahmad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manahmad"/>
    <language>en</language>
    <item>
      <title>How To Use Variable Keywords In JavaScript Properly</title>
      <dc:creator>Mansoor Ahmad</dc:creator>
      <pubDate>Fri, 11 Oct 2019 20:17:09 +0000</pubDate>
      <link>https://dev.to/manahmad/how-to-use-variable-keywords-in-javascript-properly-l8f</link>
      <guid>https://dev.to/manahmad/how-to-use-variable-keywords-in-javascript-properly-l8f</guid>
      <description>&lt;p&gt;JavaScript can do &lt;em&gt;lots&lt;/em&gt; of things; make desktop apps, run web servers, build world-class enterprise applications, make freakin' full-fledged games, and so much more. However, at each of these applications' core, there is &lt;strong&gt;data&lt;/strong&gt;. And data is handled by &lt;strong&gt;variables&lt;/strong&gt;. In this article, we'll be exploring JavaScript variables and how to use them properly.&lt;/p&gt;

&lt;p&gt;Let's get right into it. &lt;/p&gt;

&lt;h1&gt;
  
  
  Variable Scope
&lt;/h1&gt;

&lt;p&gt;Let's take a look at two programs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// VS&lt;/span&gt;

&lt;span class="c1"&gt;// Example 2&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;blockquote&gt;
&lt;p&gt;If you're wondering why each of those variables are enclosed in curly brackets &lt;code&gt;{}&lt;/code&gt;, just know that those curly brackets essentially act as a "container", restricting access to whatever's inside of them. However, as you'll soon see, that isn't always the case...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's say there's an evil programmer who got a copy of this code and wanted to publish it to the web for all of his criminal friends to see. He might do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;publishSecretPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What'll happen next depends almost entirely on what variable keyword you used to declare &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt;. You see, in one scenario, the code executed by the evil programmer will work as planned, but in the other case, he'll get this error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ReferenceError: superImportantGovernmentPassword is not defined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why is this happening?&lt;/p&gt;

&lt;p&gt;It all has to do with &lt;strong&gt;variable scope&lt;/strong&gt;, and how each keyword treats the data in the sense of that scope. &lt;/p&gt;

&lt;h2&gt;
  
  
  Scope Is A Container
&lt;/h2&gt;

&lt;p&gt;Remember those curly brackets?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, in JavaScript, curly brackets are a way of blocking outside access to anything inside of these curly brackets. That's why, in one of the scenarios, the evil programmer gets a &lt;code&gt;ReferenceError&lt;/code&gt; back; because he , quite literally, couldn't touch &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;That's because of the fact that the &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt; variable &lt;em&gt;wasn't in the &lt;strong&gt;global scope&lt;/strong&gt;&lt;/em&gt;. The &lt;strong&gt;global scope&lt;/strong&gt; can be thought of as the place &lt;em&gt;outside of the container&lt;/em&gt;. Anything outside of the container can be accessed by anybody! However, when you put something inside curly brackets, then you're adding a container to the field and putting something into that container. In our case, we're trying to put &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt; in a safe &lt;em&gt;scope&lt;/em&gt; so that people from the outside world can't access it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;But, what if I want to login to my secret FBI server?! I need my password!&lt;/em&gt; - A secret FBI agent&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, we can fix that...&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;var&lt;/code&gt; Negates Scope
&lt;/h2&gt;

&lt;p&gt;As you've seen, we can block outside access by adding another &lt;em&gt;scope&lt;/em&gt; to the program. However, things can get tricky when you use &lt;code&gt;var&lt;/code&gt; to declare your variables. Remember Example 1?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, if you were wondering which scenario lets the evil programmer complete his evil plan, this is it. That's because using &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;completely negates any scope&lt;/strong&gt;! Anything that is declared with the &lt;code&gt;var&lt;/code&gt; keyword gets put into the global scope, irrespective of where it was declared. You could put 100 nested curly brackets, and the evil programmer would still succeed. &lt;/p&gt;

&lt;p&gt;It's for this reason that, as a general rule, JavaScript programmers...&lt;/p&gt;

&lt;h2&gt;
  
  
  NEVER USE &lt;code&gt;var&lt;/code&gt; TO DECLARE VARIABLES
&lt;/h2&gt;

&lt;p&gt;I'll say it once more: &lt;strong&gt;NEVER USE &lt;code&gt;var&lt;/code&gt; TO DECLARE YOUR VARIABLES!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;But, why?&lt;/em&gt; - You&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because it puts the functionality of your entire program, along with its readability, semantic meaning, and organization into a state of chaos. Imagine a program where &lt;em&gt;every single function&lt;/em&gt; has access to &lt;em&gt;every bit of data&lt;/em&gt; in your program; it would cause a lot of issues! For this reason, it is best practice to use the other two keywords we'll be talking about today: &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to NOT Negate Scope
&lt;/h2&gt;

&lt;p&gt;Let's go back to Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 2&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we use &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt;. Now, if that evil programmer tries his dirty trick again, then he'll be met with this error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ReferenceError: superImportantGovernmentPassword is not defined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is because &lt;code&gt;let&lt;/code&gt; respects the scope it currently is working inside of, so it doesn't expose our &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt; variable to the outside world. With that, we've beat the evil programmer and sent him back to his terminal shell. Neat.&lt;/p&gt;

&lt;h2&gt;
  
  
  Locking Values In
&lt;/h2&gt;

&lt;p&gt;Let's say one of your top FBI officials is looking to login using that password. He executes his &lt;code&gt;loginToSecretServer(password)&lt;/code&gt; function, but comes up with an error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Sorry! Wrong password. You have 2 more tries, then you're locked out. [This message was written by console.log()]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since he knows the password is 42, he goes back to the file where the password is. Lo and behold, it is still 42:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What's causing the problem?&lt;/p&gt;

&lt;p&gt;Well, he runs a find-and-replace through the repository and finds that &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt; is referenced in another file. However, the code over there is a little fishy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2408oifsd8fu08sdg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;He frowns. Somebody has changed the reference value of the variable, which meant that he was logging in with the WRONG password. He deletes the line of code, but also wants to prevent any future programmers from making the same mistake; how does he do that?&lt;/p&gt;

&lt;p&gt;Well, observe what he does next:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// Original password file&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;superImportantGovernmentPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// NOTICE THE 'const'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Wait, what's &lt;code&gt;const&lt;/code&gt;? You see, &lt;code&gt;const&lt;/code&gt; is short for "constant", and when you declare a variable with it, it is essentially saying "The value of this variable will remain unchanged (or 'immutable') for as long as it's used". Thanks to this small change, the FBI officer can rest in peace, knowing that any foolish programmers who mistakenly try to change &lt;code&gt;superImportantGovernmentPassword&lt;/code&gt;'s value indirectly will be met with this message:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TypeError: Assignment to constant variable.&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;So, let's summarize what we've learned:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Never use &lt;code&gt;var&lt;/code&gt; to declare variables, because it will let EVERY single part of your program access its value. Instead, use &lt;code&gt;let&lt;/code&gt;, which respects the scope and prevents access to its value from outside the scope.&lt;/li&gt;
&lt;li&gt;Scope is like a container; every program starts with a "global scope", where every variable lives by default as if it were in a "container". You can then create "sub-containers" to protect the values of some variables, which lets you prevent fatal errors in your program's logic!&lt;/li&gt;
&lt;li&gt;Sometimes, you don't want external sources changing the value of your variables. When you want that, use &lt;code&gt;const&lt;/code&gt;, which locks in the value of your variable completely. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading. I hope you enjoyed! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>node</category>
    </item>
  </channel>
</rss>
