<?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: Coding Sam</title>
    <description>The latest articles on DEV Community by Coding Sam (@codingsam).</description>
    <link>https://dev.to/codingsam</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%2F141598%2F7122f5bc-3eb3-4c9e-9d0d-dd7553b6c93c.jpg</url>
      <title>DEV Community: Coding Sam</title>
      <link>https://dev.to/codingsam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingsam"/>
    <language>en</language>
    <item>
      <title>What really is a constant in Javascript and what it is not</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Mon, 16 Nov 2020 17:29:19 +0000</pubDate>
      <link>https://dev.to/codingsam/what-really-is-a-constant-in-javascript-and-what-it-is-not-2c2i</link>
      <guid>https://dev.to/codingsam/what-really-is-a-constant-in-javascript-and-what-it-is-not-2c2i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I have been coding in Javascript in the last 2 years of my full time job, using the most recent &lt;a href="https://www.ecma-international.org/publications/standards/Ecma-262.htm"&gt;ECMAScript&lt;/a&gt; standard, which has all the cool features. One of them, is to be able to define constants. Other programming languages already had this feature. In Javascript this was not possible to do until ES6 came out and introduced the &lt;code&gt;const&lt;/code&gt; keyword.&lt;br&gt;
Sometimes, while I am talking to other developers, I can see some confusion around constants. Some people think that constants are more than they really are. I decided to write this post to make it clear what really is a constant in Javascript once and for all. If you are a developer who writes Javascript code, do you think you really know what constants are? For instance, if you have an object declared as a constant, can you still do the same things as if it was just a normal variable?&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a constant in Javascript?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; keyword allows you to assign a name to a value, just like &lt;code&gt;let&lt;/code&gt; or even the &lt;code&gt;var&lt;/code&gt; keyword (remember the old Javascript days?). However, once you assign a value, you cannot reassign another value. For instance, if you do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you cannot do &lt;code&gt;a = 3;&lt;/code&gt; . When you run your code, it will return an error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---G6kOK5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w36162yexx1h801txykv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---G6kOK5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w36162yexx1h801txykv.png" alt="Trying to reassign a value to a constant"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not true for the &lt;code&gt;let&lt;/code&gt; keyword. &lt;br&gt;
If you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty simple, right? If you have experience with other programming languages that have this constant feature, once you jump to Javascript ES6, you get the idea pretty quickly. For instance, in C++ there is also &lt;code&gt;const&lt;/code&gt;. In Java there is &lt;code&gt;final&lt;/code&gt;. They all do the same!&lt;br&gt;
Even though, this &lt;code&gt;const&lt;/code&gt; keyword implements a pretty simple concept, sometimes there is some confusion around it. If you come from a programming language that does not have constants or if you still code in ES5 Javascript, you only have the &lt;code&gt;var&lt;/code&gt; keyword to declare variables. It might be the first time you have to deal with constants in programming languages. Once you get it, you realise how awesome such a simple feature is. It allows you to write less error-prone code. If you have some Javascript extensions for you text editor, you can even get errors about trying to change constants right on your text editor before you try to run the code!&lt;/p&gt;
&lt;h2&gt;
  
  
  What is not a constant?
&lt;/h2&gt;

&lt;p&gt;At this point, this constant feature should be clear to you, right? It should be if you only think about numbers, booleans and strings. What happens when you start working with objects and arrays? What it means to do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can I do &lt;code&gt;person.age = 30&lt;/code&gt;? Usually, we think about something constant as something that never changes. If I declare the person variable as a constant, should I be able to change it? If you try to run that code in the Javascript console of your browser, no errors are thrown!&lt;/p&gt;

&lt;p&gt;Declaring an object as a constant and adding a property to that object&lt;br&gt;
Why this happens? Because…&lt;br&gt;
The &lt;code&gt;const&lt;/code&gt; keyword means you cannot assign other values to the same name. It doesn’t mean that the value is immutable!&lt;br&gt;
Basically, the &lt;code&gt;const&lt;/code&gt; keyword only defines one rule. You can assign a value only once. Unlike &lt;code&gt;var&lt;/code&gt;, both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords use block scope. If you want to know more about the difference between these keywords and their scopes, check my post about &lt;a href="https://dev.to/codingsam/why-don-t-we-use-var-anymore-c71"&gt;Why don’t we use var anymore?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In an assignment, if the left side of it (the variable’s name) holds a mutable value, that value is still mutable.&lt;br&gt;
You can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper };
person.age = 30;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You cannot do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper };
person = { firstName: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Rajesh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, lastName: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Koothrappali&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Is it possible to achieve immutability?&lt;/em&gt;&lt;br&gt;
Just because you use the &lt;code&gt;const&lt;/code&gt; keyword, doesn’t mean you can make sure that a value cannot change. You can always change elements in an array and properties in an object.&lt;br&gt;
Is it possible to create an object or an array and prevent changes to it? Yes! &lt;br&gt;
But, there is one small detail. Let me show you an example. Let’s use the same person constant with a slightly different structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper&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;Note that the person object has a name property, which is also an object with two properties, first and last.&lt;br&gt;
You can make this person immutable using the &lt;code&gt;Object.freeze&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper&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;Then, if you try to change the name property, nothing will happen and the person object will hold the same values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ce_6yt4b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p1b9y3uprrgzj6shobru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ce_6yt4b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p1b9y3uprrgzj6shobru.png" alt="Using Object.freeze"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, this might not be enough protection. The name property is also an object with two properties. What happens if you try to change one property of the person’s name?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u_MEiRat--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ikmfzyc11sdmpyr2vb0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u_MEiRat--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ikmfzyc11sdmpyr2vb0i.png" alt="Changing values in objects inside a frozen object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, you can still change some properties in a frozen object.&lt;br&gt;
If you want true immutability, you have to call &lt;code&gt;Object.freeze&lt;/code&gt; on all properties of the object recursively.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;After a lot of time coding in Javascript, I could see some confusion around constants. The feature to be able to declare a variable that cannot be reassigned is something that wasn’t part of the first version of the language. If you have experience coding in programming languages that have constants, you easily get familiar with it once you start coding in Javascript. However, it can get a bit unclear if you don’t have such experience.&lt;br&gt;
You can declare constants in Javascript by using the const keyword, for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sheldon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cooper&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you do that, you are not allowing to assign some other value to the same name in the same block. However, if the value is an object or an array, you can still change some properties inside, because a constant is not immutable. If you need immutability, you can use &lt;code&gt;Object.freeze&lt;/code&gt;. But, you need to have in mind that, if some of the properties inside the frozen object are also objects, you still can change those values.&lt;/p&gt;

&lt;p&gt;I hope you liked it and learned something!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
    </item>
    <item>
      <title>Hoisting - One of the trickiest Javascript features</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Sun, 18 Aug 2019 18:46:43 +0000</pubDate>
      <link>https://dev.to/codingsam/hoisting-in-javascript-get-it-once-and-for-all-5f48</link>
      <guid>https://dev.to/codingsam/hoisting-in-javascript-get-it-once-and-for-all-5f48</guid>
      <description>&lt;p&gt;Javascript is a programming language that can be tricky for beginners. Once you understand how it works, it becomes clear, but in the beginning, you end up debugging problems that you created yourself because you didn't know something about the language.&lt;br&gt;
We all have been there. &lt;br&gt;
Maybe you are learning Javascript and you are in this process of dealing with problems you don't see happening in most programming languages.&lt;/p&gt;

&lt;p&gt;In this post, I will show you some code snippets and for some, an embedded codepen. However, you will see &lt;code&gt;document.write&lt;/code&gt; calls instead of &lt;code&gt;console.log&lt;/code&gt;. This is just to allow you to see the output on the codepen's app. Otherwise, you would have to open your browser's developer tools.&lt;/p&gt;

&lt;p&gt;Let's look at the following JS code snippet:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, my name is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/codingsam01/embed/BXNGqO?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;What do you think that is going to happen? &lt;br&gt;
Here, you are using a variable before declaring it and assigning a value. This kind of code, in most programming languages, would throw an error because you are trying to use something that is not known yet.&lt;br&gt;
You will see the following output in the console:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Hello my name is undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There was not a single error, but you get an unexpected output.&lt;br&gt;
&lt;code&gt;undefined&lt;/code&gt; is the default value of any variable.&lt;/p&gt;

&lt;p&gt;If you don't know what is going on, you start to ask yourself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Why didn't the code crash when I tried to use an unknown variable?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is there some magic that allows me to use a variable declared below? If so, why didn't I get the right variable's value and got &lt;code&gt;undefined&lt;/code&gt; instead?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the previous code example, try to replace &lt;code&gt;name&lt;/code&gt; by something else:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, my name is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;somethingElse&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/codingsam01/embed/NQGJxa?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You will get this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReferenceError: somethingElse is not defined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This error is something that any Javascript beginner would expect.&lt;br&gt;
But it didn't happen in the previous example. It seems that you can use variables before they are declared.&lt;/p&gt;

&lt;p&gt;Actually, something else happens behind the scenes to allow developers to do that. It's called &lt;em&gt;Hoisting&lt;/em&gt;. Let me help you to understand one of the most tricky features of Javascript, specially for beginners. I will also show you how you can avoid it and when it can be useful.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is &lt;em&gt;Hoisting&lt;/em&gt;?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Hoisting&lt;/em&gt; is a process that moves all the declarations to the top of their scope. This is handled by the Javascript engine before running your code.&lt;/p&gt;

&lt;p&gt;But... What is the scope of a declaration?&lt;/p&gt;

&lt;p&gt;In Javascript, you have function based scope, which means, any variable declared inside a function, will belong to that function's scope, no matter where in the function it happens. For instance, if you declare a variable inside a for loop, that variable will be known anywhere in the function and not just in the loop.&lt;br&gt;
But, any function or variable declared outside a function, will belong to the global scope.&lt;/p&gt;

&lt;p&gt;Only declarations are &lt;em&gt;hoisted&lt;/em&gt; (moved to the top).&lt;br&gt;
But, which types of declarations?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;variable declarations using the &lt;code&gt;var&lt;/code&gt; keyword;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;functions using the &lt;code&gt;function&lt;/code&gt; keyword;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be careful about &lt;code&gt;var&lt;/code&gt; declarations. It is really easy to look at an assignment and think that the variable's name and value will be moved to the top.&lt;/p&gt;

&lt;p&gt;Let's look at an example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In this line of code, there are two things going on, a declaration and an assignment. This line could be translated to:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Only the declaration &lt;code&gt;var a&lt;/code&gt; will be hoisted. Like what happened in the first code snippet, if you try to use the variable &lt;code&gt;a&lt;/code&gt; before the assignment, it will be &lt;code&gt;undefined&lt;/code&gt; and not &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, when you declare a function, you also write its body. You can't create a function with just a name and specify its behavior later on. That's why, when you declare a function (using the &lt;code&gt;function&lt;/code&gt; keyword) the whole function gets hoisted. This means that, you can declare it in a line after the first call.&lt;br&gt;
For instance, the following example will not throw an error:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello my name is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&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;&lt;iframe height="600" src="https://codepen.io/codingsam01/embed/PoYzdgr?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You will see "Hello my name is Sam" on the console. The &lt;code&gt;getMessage&lt;/code&gt; function got hoisted, which means, moved to the top, by the Javascript engine before running the code.&lt;/p&gt;
&lt;h1&gt;
  
  
  Let's see it in action!
&lt;/h1&gt;

&lt;p&gt;Look at the following code snippet:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;completeMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intro&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&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;intro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello my name is&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;completeMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/codingsam01/embed/JjPKaqv?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The output will be "Message: undefined Sam".&lt;br&gt;
Before running the code, declarations will be hoisted. Let's break down &lt;em&gt;Hoisting&lt;/em&gt; and follow through the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, collect all declarations (&lt;code&gt;var&lt;/code&gt; and &lt;code&gt;function&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tfKUsTnN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vwbc44wrxedyiqhcr32s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tfKUsTnN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vwbc44wrxedyiqhcr32s.png" alt="Highlighting all declarations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move those declarations to the top of their scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7jlklfxi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/74dpwcfri9qo7y9mgshr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7jlklfxi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/74dpwcfri9qo7y9mgshr.png" alt="Moving all declarations to the top of their scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this process, the Javascript engine will look at your code 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;completeMessage&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;intro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;completeMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intro&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;intro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello my name is&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;completeMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;message&lt;/code&gt; variable and &lt;code&gt;getMessage&lt;/code&gt; function are global, that's why they were moved to the top of the file. The &lt;code&gt;completeMessage&lt;/code&gt; and &lt;code&gt;intro&lt;/code&gt; variables were moved to the top of the &lt;code&gt;getMessage&lt;/code&gt; body, because they are attached to the function's scope.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;completeMessage&lt;/code&gt; variable will be &lt;code&gt;undefined Sam&lt;/code&gt;, because the assignment to the variable &lt;code&gt;intro&lt;/code&gt; happens one line below.&lt;/p&gt;
&lt;h1&gt;
  
  
  Can you run away from it?
&lt;/h1&gt;

&lt;p&gt;How many programming languages do you know that have this &lt;em&gt;Hoisting&lt;/em&gt; feature? Most programming languages don't have it, which means, if you try to use a variable or a function that is not declared yet, you will get an error. For me, it makes a lot of sense. Why would you use a variable before you declare it in the first place?&lt;br&gt;
I don't really know why Javascript has &lt;em&gt;Hoisting&lt;/em&gt;. But, I know that &lt;a href="http://es6-features.org"&gt;ES6 (ECMAScript version 6)&lt;/a&gt; added ways of declaring variables that are not hoisted.&lt;br&gt;
ECMAScript is a standardized specification, that Javascript is based on.&lt;/p&gt;

&lt;p&gt;If, at this point, you think that &lt;em&gt;Hoisting&lt;/em&gt; was a bad idea, maybe you are not the only one and the people that work on the ES specification think like you. Maybe that's why they have created the &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords, that allow you to define constants and variables, respectively. They are not hoisted.&lt;/p&gt;

&lt;p&gt;Nowadays, a lot of Javascript developers are not using &lt;code&gt;var&lt;/code&gt; anymore, because of &lt;em&gt;Hoisting&lt;/em&gt; and the function based scope.&lt;br&gt;
See my post on "Why don't we use var anymore?"&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/codingsam" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZRB28yR4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--cTh-12PO--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/141598/7122f5bc-3eb3-4c9e-9d0d-dd7553b6c93c.jpg" alt="codingsam image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/codingsam/why-don-t-we-use-var-anymore-c71" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Why don’t we use var anymore?&lt;/h2&gt;
      &lt;h3&gt;Coding Sam ・ Apr 27 '19 ・ 7 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#es5vses6&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#es6&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#softwareengineering&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;In the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;completeMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intro&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&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;intro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello my name is&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;completeMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/codingsam01/embed/wvwWYwB?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You will get an error inside the &lt;code&gt;getMessage&lt;/code&gt; function, because you are trying to use the constant &lt;code&gt;intro&lt;/code&gt; before you declare it and assign it a value.&lt;br&gt;
Calling &lt;code&gt;getMessage&lt;/code&gt; before defining it does not throw an error because, remember, functions are hoisted. But there is also a solution to create non hoisted ones. Just use &lt;em&gt;arrow functions&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello my name is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&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;They are not hoisted because they don't have a name (you are not using the &lt;code&gt;function&lt;/code&gt; keyword). When you create an arrow function, you are creating an anonymous function. If you want to keep a reference to it, you have to assign that anonymous function to a constant or a variable.&lt;/p&gt;

&lt;h1&gt;
  
  
  Is it all bad?
&lt;/h1&gt;

&lt;p&gt;So far, I have shown you what &lt;em&gt;Hoisting&lt;/em&gt; is and how you can avoid it. However, I think that it is not all that bad. There is one good use case for this feature. &lt;/p&gt;

&lt;p&gt;Let's say you have a &lt;code&gt;location.js&lt;/code&gt; file, that exports a function that any developer can use to get the user's location. Before getting the location, you need to ask permission to get that data. Your file would look 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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUserLocation&lt;/span&gt; &lt;span class="p"&gt;()&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;userAllowedToGetLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;askUserPermission&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userAllowedToGetLocation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;userLocation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User refused to provide location&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;askUserPermission&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Logic to ask user permission to get his/her location&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;userLocation&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Logic to get the user's location&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The exported function is a constant with an arrow function as its value. But, that function uses two other functions, which are defined after the exported &lt;code&gt;getUserLocation&lt;/code&gt;.&lt;br&gt;
You can do this because, remember, functions in Javascript are hoisted, but not arrow functions. If these auxiliary functions were arrow functions, you would have to put them before the exported one. This is a simple example but sometimes, you can have more than two auxiliary functions. Other developers that need to work on that file would have to scroll until they find what is being exported.&lt;/p&gt;

&lt;p&gt;If you put all the auxiliary functions on the bottom of the file, you make it easier to read, because other developers will see the main logic as soon as they open the file, instead of having to scroll through a lot of functions.&lt;/p&gt;

&lt;p&gt;I use this pattern a lot... But you might disagree ;)&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Hoisting&lt;/em&gt; is a feature that can be tricky to a lot of developers. Fortunately, we have ways to avoid that process of moving all the declarations to the top of their scope. But, you can put this feature to good use. When you have a file that exports something, you can move all auxiliary functions to the bottom and make your code easier to read.&lt;/p&gt;

&lt;p&gt;Do you like &lt;em&gt;Hoisting&lt;/em&gt;? Do you think that it is useful? Do you know other ways of taking advantage of it? Let me know what you think in the comments!&lt;/p&gt;

&lt;p&gt;Happy coding! :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/u/58cf94a36c97"&gt;Coding Sam&lt;/a&gt; @ Medium&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/codingsam01"&gt;codingsam @ Twitter&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>dev</category>
    </item>
    <item>
      <title>My Top 5 VS Code Extensions</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Wed, 03 Jul 2019 21:59:08 +0000</pubDate>
      <link>https://dev.to/codingsam/my-top-5-vs-code-extensions-5hl7</link>
      <guid>https://dev.to/codingsam/my-top-5-vs-code-extensions-5hl7</guid>
      <description>&lt;p&gt;Since I started coding I have used many text editors, such as Sublime, Atom and even vim (I know this is a bit different but it can be considered an editor anyway). I have been using VS Code for a couple of years and I really really love it.&lt;/p&gt;

&lt;p&gt;I love this editor because it has a really good performance (at least I don’t have any issues so far), it is free and you have a lot of extensions available to install. These extensions can make a difference in your everyday work and of course, make your editor look awesome!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AQD7srEXJUpS8KSuRu8WJhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AQD7srEXJUpS8KSuRu8WJhg.png"&gt;&lt;/a&gt;Just a screenshot of my favorite text editor&lt;/p&gt;

&lt;p&gt;I will share with you what is my top 5 extensions for VS Code. You will find two that are only useful if you write Javascript code, which I do... And I love to!&lt;/p&gt;

&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/eamodio/vscode-gitlens" rel="noopener noreferrer"&gt;&lt;strong&gt;GitLens&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are working on a project that uses git as the VCS (Version Control System) you will love this one. This basically tells you who made a given change in the line your cursor is on. It shows you the commit message and the user who made the change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F858%2F1%2Af46Iy-ZRSL22GdaAjYepDQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F858%2F1%2Af46Iy-ZRSL22GdaAjYepDQ.gif"&gt;&lt;/a&gt;Using gitlens to see commit messages line by line&lt;/p&gt;

&lt;p&gt;This extension also adds a new option in the left side menu where you can see in great detail, the commit logs, compare files, see branches, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AyFYqfp_lPA4zO-vtUisJpg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AyFYqfp_lPA4zO-vtUisJpg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=robertohuertasm.vscode-icons" rel="noopener noreferrer"&gt;&lt;strong&gt;vscode-icons&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one is also for pretty much any kind of software developer. It is nothing more than a icon package. Let me show you an example…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F406%2F1%2AcSTfrWStf9cmGh_nzlNj3Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F406%2F1%2AcSTfrWStf9cmGh_nzlNj3Q.png"&gt;&lt;/a&gt;Example of how icons appear using vscode-icons extension&lt;/p&gt;

&lt;p&gt;As you can see, it does a lot more than just looking at each file’s extension and use the right icon. Some files don’t even have an extension in the file name, for instance, the “LICENSE” file. But somehow, the extension uses a different icon for this file, even though it is just a text file. The “Dockerfile” got its own icon too! It really is more than just an icon theme for your text editor!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer" rel="noopener noreferrer"&gt;&lt;strong&gt;Bracket Pair Colorizer&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you have a lot of “&lt;em&gt;(&lt;/em&gt;“ and “&lt;em&gt;{&lt;/em&gt;“ and it’s hard to find the corresponding “&lt;em&gt;)&lt;/em&gt;” and “&lt;em&gt;}&lt;/em&gt;” closing characters. I use this extension to solve this first world problem. It applies a different color for each pair of “()” and “{}”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F922%2F1%2A6p_Zzisd3glWVHBy1rAx4Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F922%2F1%2A6p_Zzisd3glWVHBy1rAx4Q.png"&gt;&lt;/a&gt;Each pair of “()” and “{}” gets a different color&lt;/p&gt;

&lt;p&gt;Awesome, right?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint" rel="noopener noreferrer"&gt;&lt;strong&gt;ESLint&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://eslint.org" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt; is a tool to specify code standards. Some questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Should I put a space around the “=” in an assignment?
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Should I leave a white space between the &lt;em&gt;if&lt;/em&gt; keyword and the condition in an &lt;em&gt;if&lt;/em&gt; statement?
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And much more, can be answered by this tool…&lt;/p&gt;

&lt;p&gt;All of these code standards can be defined and enforced by ESLint. You just need to integrate this tool in your project and set your rules in a &lt;code&gt;eslintrc&lt;/code&gt; file. Your build tool will raise an error if some rule is being broken. But you can do better. You can have all those errors popping up in your VS Code. There’s an extension that does just that. It looks for a configuration file and as you type, your text editor highlights errors that break the rules. Some of these errors can be automatically fixed. Just press Ctrl + Shift + P (on a PC) or Cmd + Shift + P on a Mac and type “fix” until you find an option that says “&lt;em&gt;Fix all auto-fixable problems&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F940%2F1%2A6MQHAAbiDLkV84sqHezBeA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F940%2F1%2A6MQHAAbiDLkV84sqHezBeA.gif"&gt;&lt;/a&gt;Using auto fix to fix code standard issues&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=wix.vscode-import-cost" rel="noopener noreferrer"&gt;&lt;strong&gt;Import Cost&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you import something from a package, it means you are including some external code. If you have a build process in place that generates a bundle file (all code in just one JS file), each library you use will increase final bundle’s size. With this extension, each import statement is evaluated and it tells you what is the cost of that import, in terms of file size. Because here… The size matters!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F730%2F1%2An6WqZM8glgh4xHCY-YOLSQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F730%2F1%2An6WqZM8glgh4xHCY-YOLSQ.png"&gt;&lt;/a&gt;Just an example of Import Cost extension in action&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;It’s not obvious the benefits of the extensions I showed you in this post… Until you start using them! Even if some of them just solve some first world problems, I think all of them added value to my text editor and also to my everyday work. I hope you can get something awesome from this post.&lt;/p&gt;

&lt;p&gt;Do you use VS Code? Any other editor, like &lt;code&gt;vim&lt;/code&gt;? What extensions do you use? Share in the comments your top 5 extensions for your editor of choice! &lt;/p&gt;

</description>
      <category>editor</category>
      <category>vscode</category>
      <category>tools</category>
      <category>extensions</category>
    </item>
    <item>
      <title>Bug detective — When returning JSON goes wrong</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Sun, 02 Jun 2019 21:32:32 +0000</pubDate>
      <link>https://dev.to/codingsam/bug-detective-when-returning-json-goes-wrong-283g</link>
      <guid>https://dev.to/codingsam/bug-detective-when-returning-json-goes-wrong-283g</guid>
      <description>&lt;p&gt;As a developer, I spend a lot of time on fixing bugs. If you are a software developer, you can relate to this, right? I love to create new things and develop new features. However, there are a lot of days that most of my time is spent around debugging. Some days are really hard and you have to act like a detective to fix a bug. Those are your detective days. I think most developers have such days from time to time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WzQrb9Na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0raphcvm8o6zuwfrm0ra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WzQrb9Na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0raphcvm8o6zuwfrm0ra.png" alt="The code detective"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, I will introduce a bug case and I will explain the process of fixing it. The case is based on a real problem that a coworker and I had to debug in the past, while working on a PHP codebase. I changed some variables and functions names to avoid sharing private code. This programming language has a lot of bad reputation because a lot of people wrote really bad code in it. But you can do that in any programming language, right? The focus of this post is not in PHP. You can keep reading it even if you never coded in that programing language before. The main focus is on the case itself and in the process of finding the cause. In the end, I will give you some tips to help you on those detective days.&lt;/p&gt;

&lt;p&gt;Let’s just get started with the case!&lt;/p&gt;

&lt;h2&gt;
  
  
  The case
&lt;/h2&gt;

&lt;p&gt;You work on a web application and one of the pages has a table, which is really important for your users to allow them to make use of your app. One day, you receive a lot of bug reports about this table not showing any data at all. Not a single row was there. Like any bug that is blocking your users, you have to fix it right away.&lt;/p&gt;

&lt;p&gt;The first thing you try to do is to reproduce the behavior that your users described. You just do that and of course, they were right. There was no data at all. You check the browser’s developer tools and there is not a single error being thrown. You check your server’s logs and there are no errors there either.&lt;/p&gt;

&lt;p&gt;You start investigating…&lt;/p&gt;

&lt;h2&gt;
  
  
  The investigation
&lt;/h2&gt;

&lt;p&gt;Let’s ask the first question… &lt;em&gt;How the table works and where its data comes from?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An HTTP request is triggered by some code in Javascript to an endpoint written in PHP, which is supposed to return a JSON array. That array contains several objects that will be used to “feed” the table’s content on the frontend.&lt;/p&gt;

&lt;p&gt;Pretty simple, right?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What is the endpoint returning?&lt;/em&gt; Let’s check the response, maybe it is returning an empty array… But it’s not. &lt;em&gt;The response is completely empty&lt;/em&gt;. Not an empty array. There is nothing in the response body but its status code is a &lt;em&gt;200 OK&lt;/em&gt;. Now it starts to become really weird. Not a single error and not an empty array. How is it possible that no error is thrown and you get an empty response body? Something really weird is happening on the server.&lt;/p&gt;

&lt;p&gt;How the server endpoint’s code looks like?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDataFromDatabase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For those of you who never saw any PHP code before, the way you initialize a variable is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$variableName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;getDataFromDatabase&lt;/em&gt; is a function in the project’s code base that sends a query to the database and returns an &lt;a href="https://www.w3schools.com/php/php_arrays.asp"&gt;array&lt;/a&gt; of associative arrays (key-value structures, something like Javascript objects, in PHP).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.json-encode.php"&gt;json_encode&lt;/a&gt;&lt;/em&gt; is a built-in function, provided by the programming language, that converts an associative array or an array of associative arrays to JSON.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.echo.php"&gt;echo&lt;/a&gt;&lt;/em&gt; is a very well known PHP construct that just outputs one or more strings. In this case, it is outputting the &lt;em&gt;$data&lt;/em&gt; array in a JSON format, because of the &lt;em&gt;json_encode&lt;/em&gt; call. If everything goes well, you get a JSON array out of the &lt;em&gt;$data&lt;/em&gt; variable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, I challenge you to take a small break and think of what can be causing this endpoint to return nothing.&lt;/p&gt;

&lt;p&gt;Did you think about it?&lt;/p&gt;

&lt;p&gt;You need to ask yourself… Is there any issue with the getDataFromDatabase function? Is there any error connecting to the database that is being caught and not being thrown to the caller?&lt;/p&gt;

&lt;p&gt;Let’s try a top down approach. Which means, let’s start in the top level, which is the endpoint’s code in this case, if something is wrong here, you need to take a look inside the &lt;em&gt;getDataFromDatabase&lt;/em&gt; function. Because this is the only function you can look at. &lt;em&gt;json_encode&lt;/em&gt; is a PHP built-in function. It should be working properly. Let’s take a look at the &lt;em&gt;$data&lt;/em&gt; variable’s content. To do that, you just need to call &lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.var-dump.php"&gt;var_dump&lt;/a&gt;&lt;/em&gt;, which is a function that dumps the content of a given variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDataFromDatabase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Let's output the $data variable's content&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this change, now your response will look weird because you are dumping the variable’s content, which is expected. When the &lt;em&gt;var_dump&lt;/em&gt; executes, you can see an actual array with a lot of associative arrays inside. This &lt;em&gt;getDataFromDatabase&lt;/em&gt; function seems to be working just fine, right?&lt;/p&gt;

&lt;p&gt;Right now, the only explanation is that, something is wrong with calling the &lt;em&gt;json_encode&lt;/em&gt; function. But it is a built-in function. It should behave as expected.&lt;/p&gt;

&lt;p&gt;At this point you are thinking that I lied and this is all fake, which I really understand. But, as I told you in the beginning of this post, this case is based on a real problem that I had to debug. Something like this actually happened to me. I ended up asking myself…&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Is it possible that a built-in function like json_encode is not working properly?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And also… &lt;em&gt;WTF&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;This is one of those problems that you spend many hours on it, trying to fix it and the more you dig into it, the more you say “&lt;em&gt;WTF&lt;/em&gt;?”. You start to become really desperate and you start googling about your problem. Before doing that, let’s just look at the &lt;a href="PHP%20website"&gt;PHP website&lt;/a&gt; and search “&lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.json-encode.php"&gt;json_encode&lt;/a&gt;&lt;/em&gt;”. There is something really interesting in the “Return values” section:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Returns a JSON encoded string on success or &lt;code&gt;FALSE&lt;/code&gt; on failure.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since the endpoint is returning nothing, the &lt;em&gt;json_encode&lt;/em&gt; function must be failing and returning &lt;code&gt;false&lt;/code&gt;. Let’s change the code to call &lt;em&gt;var_dump&lt;/em&gt; to see what &lt;em&gt;json_encode&lt;/em&gt; is returning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDataFromDatabase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Let's output the $data variable's content&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Let's output the result of calling json_encode&lt;/span&gt;
&lt;span class="nv"&gt;$encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$encoded&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$encoded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You will see in the endpoint’s response the output of dumping the &lt;em&gt;$data&lt;/em&gt; variable and another line with &lt;code&gt;FALSE&lt;/code&gt;. That’s right, your HTTP endpoint code is returning nothing because &lt;em&gt;json_encode&lt;/em&gt; function is failing and when you pass &lt;code&gt;FALSE&lt;/code&gt; to echo, it doesn’t output anything . There are a lot of reasons why this could be failing. When this happened to me, some of my associative arrays had some &lt;code&gt;NaN&lt;/code&gt; (Not a Number) values. Looks like it cannot be encoded to JSON.&lt;/p&gt;

&lt;p&gt;How do we get the error when &lt;em&gt;json_encode&lt;/em&gt; function fails?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pass &lt;code&gt;JSON_THROW_ON_ERROR&lt;/code&gt; option to the function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call &lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.json-last-error.php"&gt;json_last_error&lt;/a&gt;&lt;/em&gt; function to get the actual error from the last json_encode call&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call &lt;em&gt;&lt;a href="https://www.php.net/manual/en/function.json-last-error-msg.php"&gt;json_last_error_msg&lt;/a&gt;&lt;/em&gt; function to get the error message from the error occurred in the last json_encode call&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What was the cause of this bug? You will have to look at the code that actually creates the data that you get from the &lt;em&gt;getDataFromDatabase&lt;/em&gt; function. All these hours spent just trying to understand why the endpoint’s response was empty could be avoided if an error was being thrown. I think most times, developers don’t think too much much about errors or they try to write code that fails silently. This is a good example why, in most cases, the best solution is to just throw an error. Your users will get it but you can fix it much faster and start looking at the right place.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the takeaways from this?
&lt;/h2&gt;

&lt;p&gt;Some days, I spend long hours debugging. I think most developers can relate to this. The story I told you in this post is based on a problem, that I actually had to fix, that was blocking users. You can get some takeaways that can help you in most of your detective days:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Use a top-down approach&lt;/em&gt;. This means, you start looking at the most high level code that seems to be the problem. In this case, you can avoid looking inside the &lt;em&gt;getDataFromDatabase&lt;/em&gt; function because you can start looking at the endpoint code itself. If you look inside at that function first, you would spend much more time in this entire investigation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Look at the documentation&lt;/em&gt;. If you are using some built-in functions you can’t just assume they always work the right way and there will never be errors. Try to understand how they work and how to do the proper error handling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Avoid to write code that fails silently&lt;/em&gt;. If something is not supposed to fail don’t’ just think that it will never happen and you don’t need to do anything. Write code that gets the error and if you don’t know what do with it, just throw it. If something goes wrong your users will be affected but you will be able to fix the problem much faster&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Most times, the bug is not where you think it is&lt;/em&gt;. This goes in line with previous bullet. If you throw errors instead of thinking they will never happen, you might start looking at the right place in the beginning of your investigation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Take some breaks and drink water or eat something&lt;/em&gt;. You will need extra energy. This can be hard while you have users and your boss yelling at you because something is not working. But really, try to do it. Otherwise your brain will just “explode” and you will take much more time that is necessary to fix the problem.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you enjoyed this story and took away something that will help you in your life as a software developer. Specially in those days when you have to fix really weird bugs.&lt;/p&gt;

&lt;p&gt;Happy coding! :)&lt;/p&gt;

&lt;p&gt;Let me know what you think and follow me for more cool content about dev stuff :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/codingsam01"&gt;codingsam @ Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>bugs</category>
      <category>softwaredevelopment</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Unboxing Redux</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Sun, 12 May 2019 17:52:04 +0000</pubDate>
      <link>https://dev.to/codingsam/unboxing-redux-4g6o</link>
      <guid>https://dev.to/codingsam/unboxing-redux-4g6o</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/@codingsam/unboxing-redux-5510bce4178d?source=rss-58cf94a36c97------2" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1200%2F1%2A4wwbSLGNbh1m6E-z6y9_Wg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redux is just flux with only one store&lt;/p&gt;

&lt;p&gt;Redux is a Javascript library that helps developers manage the application state, for instance, it can be the logged in user or the followers of a given user in a social network app.&lt;/p&gt;

&lt;p&gt;I am working with Redux for a couple of months and it's being awesome! Something just popped into my mind when I started using it. Then, I remembered that I have read about the Flux design pattern a long time ago. Don't worry if you don't know anything about redux or flux, I will try to make it clear. I just realized something about these two things. I told myself: "So, this is just a flux implementation, but you only have one store".&lt;/p&gt;

&lt;p&gt;When I said it like that to some people, that were used to work with Redux, their reaction was something like: &lt;br&gt;
"What? What is flux?". This was kind of weird because, for me, it was clear that redux was related with flux. But then I realized something. It's really easy to talk about Redux without even thinking about flux. Most people describe Redux just as a application state management library for the frontend, which is true. But I think that is not the most complete answer. If you jump into it and just start using it, you are missing the big picture. Don't worry, I am not judging! If you just joined a project that uses this library, you have to learn how it works and get stuff done, right? It can take some time until it all makes sense.&lt;/p&gt;

&lt;p&gt;If you don't know what redux is, you came to the right place. I will try to explain it to you in simple words. Even if you are familiar with Redux, are you sure you really know what it is and where all its concepts come from? &lt;/p&gt;

&lt;p&gt;I think that, defining redux implies that you first explain what flux is. Once you understand it, redux just comes out naturally and you will see why I define it as a "flux with only one store".&lt;/p&gt;

&lt;p&gt;This is not a flux or redux tutorial. In this post, I will do the "&lt;em&gt;unboxing&lt;/em&gt;" of Redux, which means, I am going to try to explain what it is and where its core concepts come from.&lt;br&gt;
If you are already lost, don't worry. I will break it down so you end up understanding what Flux and Redux is. Then, you can jump into the &lt;a href="https://redux.js.org/introduction/getting-started" rel="noopener noreferrer"&gt;Redux getting started&lt;/a&gt; guide and start building awesome projects!&lt;br&gt;
Let me "&lt;em&gt;unbox&lt;/em&gt;" Redux for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  Flux
&lt;/h2&gt;

&lt;p&gt;When I open the Redux box, the first thing I find is &lt;a href="https://facebook.github.io/flux/" rel="noopener noreferrer"&gt;Flux&lt;/a&gt;. It is a design pattern, created by Facebook, for managing the application state. I am not going to focus on any particular implementation. I think once you understand the pattern, you can just find any implementation and start using it.&lt;br&gt;
It has &lt;em&gt;actions&lt;/em&gt;, a single &lt;em&gt;dispatcher&lt;/em&gt;, one or more &lt;em&gt;stores&lt;/em&gt; and of course the &lt;em&gt;views&lt;/em&gt;. Let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Action&lt;/em&gt;: This is just an object that needs, at least, to have a type key, which will define the action's type. This type will be used later on to make decisions about state updates&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Dispatcher&lt;/em&gt;: Its responsibility is to send the actions to all the stores&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Store&lt;/em&gt;: The store receives the actions and updates the application state accordingly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;View&lt;/em&gt;: This is just the UI component that the user is interacting with&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following diagram shows the main components of this pattern and how they interact with each other:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AxsAiyYD66qYqIxkJrsBl0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AxsAiyYD66qYqIxkJrsBl0g.png" alt="Diagram that shows how the Flux pattern works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It works like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The user interacts with the app &lt;em&gt;views&lt;/em&gt; and those interactions will create &lt;em&gt;actions&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Actions&lt;/em&gt; are sent to all stores through the &lt;em&gt;Dispatcher&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each &lt;em&gt;store&lt;/em&gt;, that is programmed to listen to a given &lt;em&gt;action&lt;/em&gt; can update its state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The new state will be available for the &lt;em&gt;views&lt;/em&gt; to use if they need to&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each store is responsible for handling the state and logic for a given domain. For instance, if you are creating a social network, you might have one store for managing users and another one for the feed. Some stores will listen to the dispatched actions and some stores will just ignore it, it's up to the developer to decide which store cares about which actions. In this example, an action could be "the user's feed is fetched" or "the user requested to follow another user".&lt;br&gt;
This is a really simplified view of Flux. I recommend that you dive into the &lt;a href="https://facebook.github.io/flux/docs/in-depth-overview.html" rel="noopener noreferrer"&gt;oficial documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that I showed you what was inside the Redux box, the Flux pattern, let's move on to the box itself… Redux!&lt;/p&gt;
&lt;h2&gt;
  
  
  Redux
&lt;/h2&gt;

&lt;p&gt;After looking at Flux, change it to only have one store and add the concept of &lt;em&gt;Reducer&lt;/em&gt;. That's it, now you have Redux.&lt;br&gt;
The following diagram shows how the different components of Redux interact with each other:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aj69swQLVOLX7gwj3vUFeVw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aj69swQLVOLX7gwj3vUFeVw.png" alt="Diagram that shows how Redux works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It works like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The user interacts with the &lt;em&gt;views&lt;/em&gt;, which can create some &lt;em&gt;actions&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Those &lt;em&gt;actions&lt;/em&gt; are dispatched (through the &lt;em&gt;Dispatcher&lt;/em&gt;) to the &lt;em&gt;reducer&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;reducer&lt;/em&gt; receives the current state and the action and it returns the new state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The views now just have to get the new state from the &lt;em&gt;store&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Looks pretty much like Flux, right? But you only have one store. The only way to change the state is dispatching an action. The reducer will get it and return the new state. One really important thing about the reducer is that, it must be a &lt;em&gt;pure function&lt;/em&gt;, which is a function that produces the same result for the same arguments and doesn't have any side effects. This means that the reducer has to create a new state object instead of modifying the existing one. This is the only way for Redux to detect that the state was updated.&lt;br&gt;
For instance, if you are working on a social network project, your state could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"followers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"following"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"feed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the reducer…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reducer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FETCH_FEED_SUCCESS&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;feed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;feed&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FETCH_FOLLOWERS_SUCCESS&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;followers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;followers&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FOLLOW_SOMEONE&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userToFollow&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;following&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]:&lt;/span&gt; &lt;span class="nx"&gt;currentFollowing&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;state&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;following&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;currentFollowing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userToFollow&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;following&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&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;The default case does not need to create a new object because it's not modifying the state.&lt;br&gt;
Having only one reducer will take us to a really big and messy reducer function, right? Well, fortunately you can split the reducer into multiple functions and each one will be responsible for handling only part of the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Feed reducer&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;feed&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FETCH_FEED_SUCCESS&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;feed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&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;span class="c1"&gt;// Followers reducer&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;followers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FETCH_FOLLOWERS_SUCCESS&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;followers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;followers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&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;span class="c1"&gt;// Following reducer&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;following&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FOLLOW_SOMEONE&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userToFollow&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userToFollow&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&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;You can do this thanks to the &lt;a href="https://redux.js.org/api/combinereducers" rel="noopener noreferrer"&gt;combineReducers&lt;/a&gt; function. All your combined reducers will receive any action you dispatch. You have to do this because there is only one store here, which means, only one reducer.&lt;/p&gt;

&lt;p&gt;Like Flux, you still have actions and the store but, in a more simplified way. You just need to dispatch actions and handle the state changes in the reducer.&lt;/p&gt;

&lt;p&gt;You can use Redux with any UI framework of your choice. There are some packages that give you some functions that makes easier to connect your framework to the redux store. For instance, you have &lt;a href="https://github.com/reduxjs/react-redux" rel="noopener noreferrer"&gt;react-redux&lt;/a&gt; package if you want to use Redux with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It's really easy to jump right into redux without understanding what inspired all those concepts, such as, actions and the store. I think it's important to get the big picture and learn about flux, even if it is just a simplified overview, like the one you just read. Flux can be a bit tricky to get at the first glance. I think Redux gives you this pattern in a much more simpler and easier way. You only have one store and state updates are handled by reducers, that need to be pure functions.&lt;/p&gt;

&lt;p&gt;Next time someone asks you what is redux, you can just do this unboxing. You first show the Redux box. Then, you open it and take Flux out of the box. Now you explain Flux and in the end, you get back to the Redux box and everything makes sense. This is why I define Redux with this simple sentence: "&lt;em&gt;Redux is just flux with only one store&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;I hope you enjoyed and learned something. Happy coding! :)&lt;/p&gt;

&lt;p&gt;Let me know what you think and follow me for more cool content about dev stuff :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/codingsam01" rel="noopener noreferrer"&gt;codingsam01 @ Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.instagram.com/codingsam01" rel="noopener noreferrer"&gt;codingsam01 @ Instagram&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>javascript</category>
      <category>react</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>I am a software engineer. I moved from a big company to a startup since October last year, Ask Me Anything!</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Sat, 06 Apr 2019 13:16:29 +0000</pubDate>
      <link>https://dev.to/codingsam/i-am-a-software-engineer-i-moved-from-a-big-company-to-a-startup-since-october-last-year-ask-me-anything-2blm</link>
      <guid>https://dev.to/codingsam/i-am-a-software-engineer-i-moved-from-a-big-company-to-a-startup-since-october-last-year-ask-me-anything-2blm</guid>
      <description>

</description>
      <category>ama</category>
    </item>
    <item>
      <title>Why don’t we use var anymore?</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Thu, 28 Mar 2019 22:20:26 +0000</pubDate>
      <link>https://dev.to/codingsam/why-don-t-we-use-var-anymore-c71</link>
      <guid>https://dev.to/codingsam/why-don-t-we-use-var-anymore-c71</guid>
      <description>&lt;p&gt;If you used to code in Javascript in the old days, you used the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var" rel="noopener noreferrer"&gt;&lt;em&gt;var&lt;/em&gt;&lt;/a&gt; keyword a lot. There was no other way to declare a variable. It was really simple. The only thing you needed to do was just something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// etc...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since ES6, there are two new keywords, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const" rel="noopener noreferrer"&gt;&lt;em&gt;const&lt;/em&gt;&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="noopener noreferrer"&gt;&lt;em&gt;let&lt;/em&gt;&lt;/a&gt;. The first one is a bit self-explanatory, it allows you to define a constant. If you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can’t reassign another value to the constant &lt;em&gt;x&lt;/em&gt;. There are many programming languages that allow developers to define constants. This is a good way to write less error-prone code. However, there is also this “&lt;em&gt;let&lt;/em&gt;” keyword, which allows you to declare variables. And that’s all you need, just another keyword that does the same as &lt;em&gt;var&lt;/em&gt;, right? Why there is not just “var” and &lt;em&gt;const&lt;/em&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why don’t we use var anymore?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To answer that question, you need to know how &lt;em&gt;var&lt;/em&gt; really works. In Javascript, before ES6, there was no &lt;strong&gt;block scope&lt;/strong&gt;. You have &lt;strong&gt;function scope&lt;/strong&gt; instead. Let’s break down these two concepts.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Block scope&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you declare a variable or a function, it will be accessible inside the block it was declared on. Most programming languages have this type of scope, for instance, Java. See the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processArray &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I cannot use the variable i here&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can only use the variable &lt;em&gt;i&lt;/em&gt; inside the for loop. It doesn’t exist outside that block. In Java, each time you use a “{“ you are creating a new block and the “}” means, you are closing that block. Of course, if you declare a variable outside the block, you can use it inside the block.&lt;/p&gt;

&lt;p&gt;Let me show you the following diagram that illustrates how the block scope would work in this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2AWQqkmBqRLovbfM1fp-2XlQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2AWQqkmBqRLovbfM1fp-2XlQ.png"&gt;&lt;/a&gt;Block scope using the Example class.&lt;/p&gt;

&lt;p&gt;Each rectangle is a scope. The “children” scopes can access the functions and variables in the “parent” scopes, but the “parents” cannot access the “children”. In this example, the Example class is a parent of &lt;em&gt;processArray&lt;/em&gt; method, which is a parent of the &lt;em&gt;for loop&lt;/em&gt; block. The Example class cannot access whatever belongs to the &lt;em&gt;processArray&lt;/em&gt; method, which cannot access whatever belongs to the &lt;em&gt;for loop&lt;/em&gt;. However, the &lt;em&gt;for loop&lt;/em&gt; can access anything in the &lt;em&gt;processArray&lt;/em&gt; method and anything in the &lt;em&gt;Example&lt;/em&gt; class, for instance, a variable or any other method. This is the kind of scope that most developers are used to work with.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Function scope&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Unlike Java, Javascript (ES5) creates scopes based on functions. This means, once you declare a variable inside a function, you can use it anywhere in that function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processArray&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Element &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I can use variable i outside the loop &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&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;Of course, you also have the global scope. Each time you declare a variable outside a function, it will belong to the global scope.&lt;/p&gt;

&lt;p&gt;Let me show you another diagram, but this time, for the function scope:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2A3JueUzrZxCjytZCh9_sDuw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2A3JueUzrZxCjytZCh9_sDuw.png"&gt;&lt;/a&gt;Function scope for the processArray function&lt;/p&gt;

&lt;p&gt;Looks much simpler, right? But where is the for loop?&lt;/p&gt;

&lt;p&gt;The for loop is a block, but there is no block scope here, that’s why, it does not have its own scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we stop using the &lt;em&gt;var&lt;/em&gt; keyword?
&lt;/h3&gt;

&lt;p&gt;It’s all about scope! Most programming languages have block scope. Why? Because it is less error-prone. You can declare variables inside blocks (if statements, for loops and so on) without worrying about overwriting some previously declared variable.&lt;/p&gt;

&lt;p&gt;Let me show you an example. Let’s say you are implementing a function that prints each position of a matrix. You will write a &lt;em&gt;for loop&lt;/em&gt; inside another &lt;em&gt;for loop&lt;/em&gt;. In ES5, where you only have the &lt;em&gt;var&lt;/em&gt; keyword available, a beginner would write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printMatrix&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nf"&gt;printMatrix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F84%2F1%2ABvtUBy7SAw6t9pGK-mDATQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F84%2F1%2ABvtUBy7SAw6t9pGK-mDATQ.png"&gt;&lt;/a&gt;Output of printMatrix function in ES5&lt;/p&gt;

&lt;p&gt;It only logged the first line. To help you understand what is going on, let me show you a scope diagram for this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2AijeggeoI_dbSCgIrwQrvBQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2AijeggeoI_dbSCgIrwQrvBQ.png"&gt;&lt;/a&gt;Breaking down the scope in the printMatrix ES5 version&lt;/p&gt;

&lt;p&gt;All variables inside the &lt;em&gt;printMatrix&lt;/em&gt; function are on the same level. The two &lt;em&gt;for loops&lt;/em&gt; are actually using the same variable &lt;em&gt;i&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;What happened? When I started coding in Javascript some years ago, I wrote a lot of code like this, because I used to code a lot in Java, which has block scope. So, I thought that if I declared a variable inside the loop, it will stay there… But not really.&lt;/p&gt;

&lt;p&gt;After a lot of bugs and frustration I learned that Javascript does not have block scope. There is only function scope. But, even after I learned about it, I forgot a lot of times! This is something that is really easy to forget about. The usual expectation is that, in the second for loop, you are creating a new variable. But you are not. You are just overwriting the variable &lt;em&gt;i&lt;/em&gt; in the first &lt;em&gt;for loop&lt;/em&gt;. After running the second loop, the condition of the first one will be evaluated again, but the variable &lt;em&gt;i&lt;/em&gt; now has the value 3 (the size of the first line in the matrix), which is equal to the matrix length (3). The condition returns false and the loop stops. That’s why only the first line of the matrix gets logged.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Javascript, it doesn’t matter how many times you use the keyword “var”. If it’s the same name in the same function, you are pointing to the same variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This function scope can be a source of a lot of bugs. Fortunately, Javascript has been changing and now we have ES6 and more. There are these two “new” keywords, &lt;em&gt;const&lt;/em&gt; and &lt;em&gt;let&lt;/em&gt;, which allow you to define a constant and a variable, respectively. They both work with block scope, which means, if variables or constants are declared inside a block, they will not be available to the “parent” blocks.&lt;/p&gt;

&lt;p&gt;Let’s rewrite the previous example, logging the matrix, taking advantage of these new keywords. If you replace &lt;em&gt;var&lt;/em&gt; by &lt;em&gt;let&lt;/em&gt; and add use &lt;em&gt;const&lt;/em&gt; for things you know you will need to need to change after initialization…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printMatrix&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nf"&gt;printMatrix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F84%2F1%2AQn2GnKEM3bIgxec14dc5aQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F84%2F1%2AQn2GnKEM3bIgxec14dc5aQ.png"&gt;&lt;/a&gt;Output of printMatrix function in ES5&lt;/p&gt;

&lt;p&gt;All positions in the matrix got logged. It just worked and I just had to replace &lt;em&gt;var&lt;/em&gt; by &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; keywords! Because in each &lt;em&gt;for loop&lt;/em&gt; I am actually creating a different variable &lt;em&gt;i&lt;/em&gt;. In the second loop the first &lt;em&gt;i&lt;/em&gt; will not get overwritten.&lt;/p&gt;

&lt;p&gt;Let’s see what happened under the hood in terms of scope:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2A71bRLyJxbpXYrAFc5xzJTA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F322%2F1%2A71bRLyJxbpXYrAFc5xzJTA.png"&gt;&lt;/a&gt;Breaking down the scope in the printMatrix ES6 version&lt;/p&gt;

&lt;p&gt;Seems a bit more complicated, but this gives you block scope and each &lt;em&gt;for loop&lt;/em&gt; has its own variable &lt;em&gt;i&lt;/em&gt;. In the old days Javascript, you would need to give different names to avoid this naming conflict. But seriously, when you need to do a &lt;em&gt;for loop&lt;/em&gt; to iterate over an array, the first name that comes to your head, for the current array index value is “i”, isn’t it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just a small note&lt;/strong&gt; : I know that you have &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" rel="noopener noreferrer"&gt;&lt;em&gt;forEach&lt;/em&gt;&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;&lt;em&gt;map&lt;/em&gt;&lt;/a&gt;, and more functions to deal with arrays. This is just a simple example to show how the &lt;em&gt;var&lt;/em&gt;, &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; keywords work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; are not just two new cool keywords, they also introduce block scope that allows us to write clean and less error-prone code.&lt;/p&gt;

&lt;p&gt;Why don’t we use &lt;em&gt;var&lt;/em&gt; anymore? Because now there is a better way of declaring variables and even constants… With block scope! You don’t need to think twice when declaring variables inside blocks. I think that is easier to work with block scope than with function scope. The &lt;em&gt;var&lt;/em&gt; usage has being been discouraged. For instance, if you use &lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt; to check your code, you can configure a “&lt;a href="https://eslint.org/docs/rules/no-var" rel="noopener noreferrer"&gt;no-var&lt;/a&gt;” rule that throws an error if there is any &lt;em&gt;var&lt;/em&gt; being used.&lt;/p&gt;

&lt;p&gt;Please, keep in mind that you should use a transpiler tool, like &lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;Babel&lt;/a&gt;, to transpile your code from ES6 to ES5, to make sure it will run in any browser. Not all browsers support the complete ES6 specification yet.&lt;/p&gt;

&lt;p&gt;Embrace &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; and let &lt;em&gt;var&lt;/em&gt; go forever!&lt;/p&gt;

&lt;p&gt;I hope you enjoyed and learned something. Happy coding! :)&lt;/p&gt;

&lt;p&gt;Let me know what you think and follow me for more cool content about dev stuff :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.com/u/58cf94a36c97" rel="noopener noreferrer"&gt;Coding Sam&lt;/a&gt; @ Medium&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/codingsam01" rel="noopener noreferrer"&gt;codingsam @ Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.instagram.com/codingsam01" rel="noopener noreferrer"&gt;codingsam01 @ Instagram&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>es5vses6</category>
      <category>es6</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Awesome Javascript — Destructuring assignment</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Wed, 30 Jan 2019 21:53:55 +0000</pubDate>
      <link>https://dev.to/codingsam/awesome-javascriptdestructuring-assignment-1bh7</link>
      <guid>https://dev.to/codingsam/awesome-javascriptdestructuring-assignment-1bh7</guid>
      <description>&lt;h3&gt;
  
  
  Awesome Javascript — Destructuring assignment
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nGafbpKo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/750/1%2AjC6q6xNDHlNS5_mVT3Qawg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nGafbpKo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/750/1%2AjC6q6xNDHlNS5_mVT3Qawg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Javascript is becoming more and more a really cool language. Why? Because of all the features introduced in &lt;a href="http://es6-features.org/"&gt;ES6&lt;/a&gt; (ECMAScript 6 aka ECMAScript 2015) and all the other more advanced versions. ECMAScript is just the standardized specification of Javascript. Some browsers already support it. However, it is usually not safe to rely on that, because there is no guarantee that any browser will support the complete specification. The best thing to do is to use a transpiler, that converts your ES6 code to ES5, which for sure will run in any browser. &lt;a href="https://babeljs.io/"&gt;&lt;em&gt;Babel&lt;/em&gt;&lt;/a&gt; is a well known transpiler that you can integrate in most used build tools, such as, &lt;a href="https://webpack.js.org/"&gt;&lt;em&gt;webpack&lt;/em&gt;&lt;/a&gt; or &lt;a href="https://gulpjs.com/"&gt;&lt;em&gt;gulp&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I had worked with a lot of Javascript in a web development project in one of my previous jobs. I mean… a lot! Try to work in a frontend without any framework (no React, angular or anything else!). So, as you can imagine, I got really familiar with Javascript, but it was ES5.&lt;/p&gt;

&lt;p&gt;Since a couple months ago, I have to deal with a lot of ES6 in my full time job. It has a lot of cool features. That’s why I decided to start a new series of posts about the features I learned about (and still learning)!&lt;/p&gt;

&lt;p&gt;This post is about the &lt;em&gt;destructuring assignment&lt;/em&gt;, which I think it is a clean way to deal with objects and arrays.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Basically, it is just an awesome way to get values from arrays and objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s see how it works!&lt;/p&gt;

&lt;h3&gt;
  
  
  Array destructuring
&lt;/h3&gt;

&lt;p&gt;You can get some values from a given array and give them any name you want. For instance, let’s say you have an array of numbers, &lt;em&gt;[1, 2, 3, 5, 8, 13, 21]&lt;/em&gt; and you want to store the first two indexes in separate variables. In “normal” Javascript you would do something like:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The variable x and y contain the indexes 0 and 1, respectively. The rest, contains all other remaining numbers &lt;em&gt;[3, 5, 8, 13, 21]&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now, let’s do it the ES6 way!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This does the same as the previous code, but it looks much better. You get the first two positions from the array in variables &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt;. The remaining values are stored in the &lt;em&gt;rest&lt;/em&gt; constant. If you don’t know about what the “&lt;em&gt;const&lt;/em&gt;” keyword means, don’t worry, it is something from ES6, it’s used to define a value that cannot be reassigned.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object destructuring
&lt;/h3&gt;

&lt;p&gt;In ES5, to get values from objects, you just simply need to know which keys have the values you want and type something like &lt;em&gt;myObject.myKey&lt;/em&gt;. Let’s say, you have an object that describes a person with the properties: name, age, gender, address and phone number. If you want to extract the name and age properties, you just need to simply do:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you are using ES6 you can do it like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You just extracted some keys from the object and also got another object with the remaining keys (gender, address and phone). Remember the weird “…” thing from the array destructuring? This “…rest” does the same, but in this case, it is an object.&lt;/p&gt;

&lt;p&gt;I hope you get the idea, but let me give you an example to help you to get a better feeling about how awesome is this!&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Let’s say, you are implementing a 2D game in Javascript, for instance Bomberman. Who does not know about this one, right? Right??? :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--afYDW9o_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/256/1%2A__Qjq_ioJpGPq8h4phyyng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--afYDW9o_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/256/1%2A__Qjq_ioJpGPq8h4phyyng.png" alt=""&gt;&lt;/a&gt;Bomberman game&lt;/p&gt;

&lt;p&gt;You already have the following function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;getPosition&lt;/em&gt;: returns the bomberman’s current position, in a pair of coordinates (&lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt;), in the 2D grid where all the action is happening&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two ways of defining a position, using an array or an object. In the array implementation, the zero and one indexes are the &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; coordinates, respectively. Using an object, this will have the &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; keys with the coordinates values. To use this &lt;em&gt;getPosition&lt;/em&gt; function, you can use the destructuring assignment to get your code a lot cleaner than without using it.&lt;/p&gt;

&lt;p&gt;Now, you need to implement a function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;getDistance&lt;/em&gt;: returns the distance (in &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; axis) between two positions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance, if you have the following positions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;position0 = {x: 2, y: 4}&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;position1 = {x: 5, y: 3}&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distance between them should be &lt;em&gt;{x: 3, y: -1}&lt;/em&gt; because, &lt;em&gt;5-2=3&lt;/em&gt; and 3–4=-1.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using an array
&lt;/h4&gt;

&lt;p&gt;Choosing the array implementation, if you call the &lt;em&gt;getPosition&lt;/em&gt; function, you will get an array where the index zero is the &lt;em&gt;x&lt;/em&gt; coordinate and the index one is the _y _value.&lt;/p&gt;

&lt;p&gt;Let’s see how we can use this function, using the &lt;em&gt;destructuring assignment&lt;/em&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;What if our &lt;em&gt;getPosition&lt;/em&gt; function does return an &lt;em&gt;undefined&lt;/em&gt; coordinate for some reason? You can set default values, so your code does not crash!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Remember the &lt;em&gt;getDistance&lt;/em&gt; function introduced in the example? Let’s define it assuming our positions are represented using an array:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It is already a really looking good code, but you can remove some lines if you do the destructuring stuff in the function’s arguments list!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Is this too fast? Let’s break it down because there are a lot of stuff going on here :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;[x1 = 0, y1 = 0]:&lt;/em&gt; this thing in the function’s header, basically means you expect an array with two positions. You are naming its indexes zero and one to &lt;em&gt;x1&lt;/em&gt; and &lt;em&gt;y1&lt;/em&gt;, respectively. In case that the array has less than two positions, or is completely empty, those &lt;em&gt;x1&lt;/em&gt; and &lt;em&gt;y1&lt;/em&gt; values become zero. &lt;strong&gt;Have in mind that you can pass an empty array but you cannot omit it, otherwise, you will get an error&lt;/strong&gt;!&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;const [xDistance, yDistance] = getDistance(position1, position2)&lt;/em&gt;: here you are just calling the &lt;em&gt;getDistance&lt;/em&gt; function that returns an array. Instead of storing that array in a variable (or a constant as I am doing in this example), you are assigning the zero and one indexes in two constants named &lt;em&gt;xDistance&lt;/em&gt; and &lt;em&gt;yDistance,&lt;/em&gt; respectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using an object
&lt;/h4&gt;

&lt;p&gt;The same &lt;em&gt;getPosition&lt;/em&gt; exercise could be implemented using an &lt;em&gt;object&lt;/em&gt; instead of an &lt;em&gt;array&lt;/em&gt;. Let’s write an &lt;em&gt;object&lt;/em&gt; version!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And you also can set &lt;em&gt;default&lt;/em&gt; values, like we did in the array version!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Awesome right? :)&lt;/p&gt;

&lt;p&gt;Now, let’s create the &lt;em&gt;getDistance&lt;/em&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I am using the destructuring assignment here with default values. Also, I am renaming its keys, so I can distinguish between the &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; from &lt;em&gt;position1&lt;/em&gt; (&lt;em&gt;x1&lt;/em&gt; and &lt;em&gt;y2&lt;/em&gt;) and &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; from &lt;em&gt;position2&lt;/em&gt; (&lt;em&gt;x2&lt;/em&gt; and &lt;em&gt;y2&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Is it possible to do better? Yes! Let’s do it!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can save some lines if you already know that your function arguments are objects with some given keys. Like I have done in the array implementation, here I can also move the destructuring part to the function’s header! Let’s break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;{ x: x1 = 0, y: y2 = 0 }&lt;/em&gt;: Basically this means that you expect an object, as the function’s first argument, with two keys &lt;em&gt;x&lt;/em&gt; and y. However, you are naming those keys &lt;em&gt;x1&lt;/em&gt; and &lt;em&gt;y1&lt;/em&gt; (this is useful because the function expects two objects with the same keys). &lt;strong&gt;Have in mind that you are not changing anything in the object passed to the function (renaming the keys, {x: x1, y: y2}, does not change the keys names in the object).&lt;/strong&gt; Also, if this object does not have a defined value for the &lt;em&gt;x&lt;/em&gt; key, the value zero (&lt;em&gt;x: x1 = 0&lt;/em&gt;) is used instead. The same is true for the &lt;em&gt;y&lt;/em&gt; key.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;const { x: xDistance, y: yDistance } = getDistance(position1, position2)&lt;/em&gt;: This calls the getDistance function. You could just put the result in a constant, but as you can see, you can destructure that object if you already know which keys you are expecting. The “&lt;em&gt;x: Distance”&lt;/em&gt; part gets the &lt;em&gt;x&lt;/em&gt; value from the returned object and renames it to &lt;em&gt;xDistance&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If you already work with Javascript ES6 (and more advanced standards), it’s very likely that you already know this destructuring assignment and you love Javascript.&lt;/p&gt;

&lt;p&gt;Anyway, have in mind that, in order to use this feature, you should transpile your code to ES5. Otherwise, there is a small chance that your code will not run in some browsers. As mentioned in the beginning of this post, go check &lt;em&gt;Babel&lt;/em&gt;. I didn’t explained anything about how to setup a transpiler because I just wanted to focus on one particular feature instead of writing a really big post.&lt;/p&gt;

&lt;p&gt;I hope you liked it and learned something. Share this, especially, with your developer friends that hate Javascript :)&lt;/p&gt;

&lt;p&gt;Let me know what you think and follow me for more cool content about dev stuff :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.com/u/58cf94a36c97"&gt;Coding Sam&lt;/a&gt; @ Medium&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/codingsam01"&gt;codingsam @ Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.instagram.com/codingsam01"&gt;codingsam01 @ Instagram&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>web</category>
      <category>dev</category>
      <category>es6</category>
      <category>javascript</category>
    </item>
    <item>
      <title>The Command Line — Your best friend</title>
      <dc:creator>Coding Sam</dc:creator>
      <pubDate>Thu, 27 Sep 2018 20:59:19 +0000</pubDate>
      <link>https://dev.to/codingsam/the-command-line--your-best-friend-51h3</link>
      <guid>https://dev.to/codingsam/the-command-line--your-best-friend-51h3</guid>
      <description>&lt;p&gt;The command line is a tool used by software developers, to run framework or development tools related commands, run scripts to automate processes and a lot more. If you are learning to code or already code, I think you should not be afraid of the command line. It can seem intimidating at first, but it will improve your workflow.&lt;/p&gt;

&lt;p&gt;I met some people that always ran away from anything that didn’t have a GUI (Graphical User Interface). In some cases, you will be just fine. For instance, if you are a C# developer, you will use Visual Studio. Actions related to running, debugging and deployment can be triggered just by clicking in some icons in the IDE (Integrated Development Environment), in this case, Visual Studio. There is nothing wrong with this approach but… When you try to explore other technologies, languages, frameworks, etc, you end up dealing with the command line.&lt;/p&gt;

&lt;p&gt;If you are already a software developer, at this point, your reaction should be something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/0aa640dad9d0aa8f10d4709e424b8bae/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/0aa640dad9d0aa8f10d4709e424b8bae/href"&gt;https://medium.com/media/0aa640dad9d0aa8f10d4709e424b8bae/href&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“What are you talking about? I know how to open the Terminal app on my Mac or Linux or cmd on Windows and run commands in it”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I am not here to tell you that you must learn how to use the command line… Instead, let me show you, from my experience, how you can have the best command line… Ever!&lt;/p&gt;

&lt;p&gt;First, let me tell you how I learned what I am about to share with you. Everything started when I was in a summer internship at a startup. I was a student looking for some kind of real world experience, so I was not really familiar with the command line. The CTO (Chief Technology Officer) showed me how to have a Quake like console (the one that comes down when you press the ‘\’ key in Quake video game) on my laptop that was running Linux. Then, he told me to install &lt;em&gt;oh-my-zsh&lt;/em&gt;, which I will talk about later in this post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lb9jOjJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/637/1%2A8_M_UVHiOx6CfY8tgl-mnA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lb9jOjJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/637/1%2A8_M_UVHiOx6CfY8tgl-mnA.png" alt=""&gt;&lt;/a&gt;Console in Quake game&lt;/p&gt;

&lt;p&gt;The CTO is basically the person that is in charge of the technology and engineering departments of a given company.&lt;/p&gt;

&lt;p&gt;From that moment, I started to love the command line. Few years later I still use this tool and I am going to share it with you.&lt;/p&gt;

&lt;p&gt;Basically, you will need these things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A terminal app&lt;/li&gt;
&lt;li&gt;Z Shell&lt;/li&gt;
&lt;li&gt;oh-my-zsh&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will explain what these things are in a moment.&lt;/p&gt;

&lt;p&gt;Just follow the two steps below :)&lt;/p&gt;

&lt;h3&gt;
  
  
  First — Get a nice terminal app
&lt;/h3&gt;

&lt;p&gt;The first thing to do is to get a terminal app. You can use the one that comes with your Operating System but there are better solutions out there, especially for Windows, believe me, you wouldn’t want to use &lt;em&gt;cmd&lt;/em&gt;. The goal here is to have a better terminal app with a Unix based command line. Unix is the base system of Mac and Linux Operating Systems. This is why I prefer Mac or Linux, and maybe that’s why you see a lot of developers using such systems. Anyway, if you use Windows you can still get almost the same experience.&lt;/p&gt;

&lt;p&gt;Here are my terminal apps recommendations for Mac, Linux and Windows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mac&lt;/strong&gt; : &lt;a href="https://www.iterm2.com/"&gt;iTerm&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NeQN1rhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AyRnyT30la5AukGoDkzG2_Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NeQN1rhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AyRnyT30la5AukGoDkzG2_Q.png" alt=""&gt;&lt;/a&gt;Iterm2 running on my own Macbook Pro&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linux&lt;/strong&gt; : &lt;a href="http://guake-project.org/"&gt;Guake&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--smRCvPeI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AdirfG6Rb4J3hbxc0aRjAHQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--smRCvPeI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AdirfG6Rb4J3hbxc0aRjAHQ.jpeg" alt=""&gt;&lt;/a&gt;Guake running on Ubuntu. Source: &lt;a href="https://www.flickr.com/photos/xmodulo/14285529772"&gt;&lt;/a&gt;&lt;a href="https://www.flickr.com/photos/xmodulo/14285529772"&gt;https://www.flickr.com/photos/xmodulo/14285529772&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt; : &lt;a href="https://conemu.github.io/"&gt;ConEmu&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PuCBM3oU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/672/1%2ASCiK5lLCZjdlEAOY7CkBrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PuCBM3oU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/672/1%2ASCiK5lLCZjdlEAOY7CkBrg.png" alt=""&gt;&lt;/a&gt;ConEmu running on Windows. Source: &lt;a href="https://commons.wikimedia.org/wiki/File:ConEmu_screenshot.png"&gt;&lt;/a&gt;&lt;a href="https://commons.wikimedia.org/wiki/File:ConEmu_screenshot.png"&gt;https://commons.wikimedia.org/wiki/File:ConEmu_screenshot.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Windows, installing just ConEmu will not give you the best experience. You need to install &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10"&gt;WSL (Windows Subsystem for Linux)&lt;/a&gt; and a Linux distribution available in the Windows Store, for instance, &lt;a href="https://www.microsoft.com/p/ubuntu/9nblggh4msv6#activetab=pivot:overviewtab"&gt;Ubuntu&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This will give you a Unix like command line, which is what this entire post is all about.&lt;/p&gt;

&lt;p&gt;I think using one of the terminal apps I mentioned before, it is already an improvement… But let’s make it even better…&lt;/p&gt;

&lt;h3&gt;
  
  
  Second — Make your terminal awesome
&lt;/h3&gt;

&lt;p&gt;I will just show you something that you can install, to have, what I think is the best command line experience possible.&lt;/p&gt;

&lt;p&gt;Now, you need to install two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://zsh.sourceforge.net/"&gt;&lt;em&gt;Z Shell&lt;/em&gt;&lt;/a&gt;, which is essentially an Unix shell (command line interpreter). The installation will depend on your Operating System. I recommend that you use a package manager. On Windows, if you successfully installed the Linux Subsystem, just use the package manager available in your Linux distribution you are using inside the WSL. If you use Linux, you can just use the package manager of your distribution. On Mac OS, I recommend you to use &lt;a href="https://brew.sh/"&gt;&lt;em&gt;Home Brew&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ohmyz.sh/"&gt;&lt;em&gt;oh-my-zsh&lt;/em&gt;&lt;/a&gt;&lt;em&gt;,&lt;/em&gt; is a set of plugins that adds more features to the &lt;em&gt;Z Shell&lt;/em&gt;. The installation process is just a matter of copy and pasting a command to your terminal. Depending on the installation method you choose, you need &lt;em&gt;wget&lt;/em&gt; or &lt;em&gt;curl&lt;/em&gt;. Just use your package manager to install one of these two options.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you made this far, let me show you how awesome this is!&lt;/p&gt;

&lt;h3&gt;
  
  
  See it in action
&lt;/h3&gt;

&lt;p&gt;Let’s see some examples of cool things you can do with the terminal and &lt;em&gt;oh-my-zsh&lt;/em&gt; together.&lt;/p&gt;

&lt;p&gt;All the following screenshots were taken from a Mac OS, because I own a Macbook Pro… But if you installed a terminal app and ‘oh-my-zsh’, you will be able to experience the same awesomeness.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Plugins&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have a lot of plugins available. &lt;a href="https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins"&gt;Check the complete plugins list&lt;/a&gt; and how to install them. Is as simple as add the plugins names to the &lt;em&gt;plugins&lt;/em&gt; list in the &lt;em&gt;.zshrc&lt;/em&gt; file in your user’s home folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/80c061172fa043dadb7e68ae287a08c7/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/80c061172fa043dadb7e68ae287a08c7/href"&gt;https://medium.com/media/80c061172fa043dadb7e68ae287a08c7/href&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Choose a theme&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;oh-my-zsh&lt;/em&gt; allows you to customize the look and feel of your terminal. I just use the default one, but you are free to give it a different look. Choosing a theme just requires you to define a &lt;em&gt;ZSH_THEME&lt;/em&gt; variable in your &lt;em&gt;.zshrc&lt;/em&gt; file. &lt;a href="https://github.com/robbyrussell/oh-my-zsh/wiki/themes"&gt;Check all the available themes&lt;/a&gt; and set this variable according to your choice. For instance, if you want to use a theme named “&lt;a href="https://github.com/robbyrussell/oh-my-zsh/wiki/themes#awesomepanda"&gt;&lt;em&gt;awesomepanda&lt;/em&gt;&lt;/a&gt;&lt;em&gt;”,&lt;/em&gt; you need to add the following to the &lt;em&gt;.zshrc&lt;/em&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/bdb7a2c906ea7f790c93c9540ccff24f/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/bdb7a2c906ea7f790c93c9540ccff24f/href"&gt;https://medium.com/media/bdb7a2c906ea7f790c93c9540ccff24f/href&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using git&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use &lt;em&gt;git&lt;/em&gt; as the version control of your projects, you know you can use the command line for that. If you navigate to any of your projects folders, you will see the current branch’s name in the right side of the folder’s name. You also have some aliases to make your git commands shorter.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;git pull =&amp;gt; gl&lt;/li&gt;
&lt;li&gt;git push =&amp;gt; gp&lt;/li&gt;
&lt;li&gt;git commit =&amp;gt; gc&lt;/li&gt;
&lt;li&gt;git status =&amp;gt; gst&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See this in action…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pRf_KzQ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Au2PYNP6CFuwhMdG4sbLW8g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pRf_KzQ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Au2PYNP6CFuwhMdG4sbLW8g.gif" alt=""&gt;&lt;/a&gt;Using git with oh-my-zsh&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Autocomplete&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Maybe you already know that, when you are typing a command, if you press the &lt;em&gt;TAB&lt;/em&gt; key, the terminal will try to complete your command. This is already a cool thing. But… &lt;em&gt;Z Shell&lt;/em&gt; and &lt;em&gt;oh-my-zsh&lt;/em&gt; take this autocomplete thing to another new level. That’s right, it can help you, for instance, to complete a folder name even if you start typing a word in the middle. If more than one option is available, you can use the &lt;em&gt;TAB&lt;/em&gt; key to navigate through the available options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--feiyvTFp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AschkIWY2CsGb7VSt_bS5DA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--feiyvTFp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AschkIWY2CsGb7VSt_bS5DA.gif" alt=""&gt;&lt;/a&gt;Autocomplete feature for folders and files&lt;/p&gt;

&lt;p&gt;Another awesome level of autocomplete, is related with some software development tools. You have several plugins to help you autocomplete some specific tools commands. For instance, if you work with &lt;em&gt;nodejs&lt;/em&gt;, you will use a lot of &lt;em&gt;npm&lt;/em&gt; commands, like &lt;em&gt;npm install&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FSsqPheB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AGwySiHrVJzHoISnYlJ3Dqg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FSsqPheB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AGwySiHrVJzHoISnYlJ3Dqg.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because you know, developers are lazy, so they will avoid to write as much as possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Any software developer has to deal with the command line. Some people don’t like it. Unless you work with a really specific tool set, bad news… You can’t run away from it. I can tell you that, from my experience, it can seem intimidating in the beginning because it requires you to memorize a lot of commands. But… once you begin feeling comfortable, you will love it.&lt;/p&gt;

&lt;p&gt;Following two simple steps, you can have a really awesome experience. You just need to download a terminal app for your Operating System. Then, go ahead and install &lt;em&gt;z shell&lt;/em&gt; and &lt;em&gt;oh-my-zsh&lt;/em&gt; on top of it.&lt;/p&gt;

&lt;p&gt;If you are a software developer or thinking about being one, remember…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The command line is as important as any other development tool&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_-rho6_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ACioB717snwMtfEBeomP01A.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_-rho6_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ACioB717snwMtfEBeomP01A.gif" alt=""&gt;&lt;/a&gt;Having fun with the command line :)&lt;/p&gt;

&lt;p&gt;I talked about the terminal and &lt;em&gt;oh-my-zsh&lt;/em&gt; but didn’t go into much detail of the installation process of each required package. That would require a post for each Operating System. The most important thing here, is that you become aware of these tools. Let me know if you would like to read about each OS. I think you have the best experience in Mac and Linux. At my current job, I use a Windows laptop. I came from a Macbook from my previous job, so I was used to a kind of Unix based experience. Fortunately, Microsoft somehow partnered with Ubuntu. This partnership allows you to have a Linux command line in your Windows. Anyway, sometimes there are problems… But I can write about that in a new post.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Share this post with software developers that are in the beginning of their careers or always try to run away from the command line :)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>terminal</category>
      <category>developer</category>
      <category>softwaredevelopment</category>
      <category>zsh</category>
    </item>
  </channel>
</rss>
