<?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: Fabio Russo</title>
    <description>The latest articles on DEV Community by Fabio Russo (@genta).</description>
    <link>https://dev.to/genta</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%2F69798%2F18904c40-67ae-4b31-a8e5-ca286370d910.jpg</url>
      <title>DEV Community: Fabio Russo</title>
      <link>https://dev.to/genta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/genta"/>
    <language>en</language>
    <item>
      <title>CSS input checker</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Tue, 10 Jul 2018 10:28:57 +0000</pubDate>
      <link>https://dev.to/genta/css-input-checker-44lc</link>
      <guid>https://dev.to/genta/css-input-checker-44lc</guid>
      <description>&lt;h1&gt;
  
  
  Easy stuff... cool result
&lt;/h1&gt;

&lt;p&gt;Here we're going to use:&lt;/p&gt;

&lt;p&gt;:not()&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;:placeholder-shown&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The :placeholder-shown CSS pseudo-class represents any  or  element that is currently displaying placeholder text.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Check input with CSS
&lt;/h2&gt;

&lt;p&gt;If we use those together with an input (with a placeholder), like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;input:not(:placeholder-shown)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we can manage stuff on the page, we want to show, &lt;strong&gt;only&lt;/strong&gt; when there's something in the input form, &lt;strong&gt;without using Javascript&lt;/strong&gt;.&lt;/p&gt;


&lt;center&gt;HTML&lt;/center&gt;
&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"My Placeholder"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Now... you can see me&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;center&gt;CSS&lt;/center&gt;
&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;
&lt;span class="nc"&gt;.text&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:placeholder-shown&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;.text&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&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 &lt;strong&gt;+&lt;/strong&gt; selector will catch the &lt;code&gt;.text&lt;/code&gt; placed immediately after the &lt;code&gt;input&lt;/code&gt;) &lt;/p&gt;

&lt;p&gt;With that we're &lt;em&gt;hiding&lt;/em&gt; the &lt;code&gt;.text&lt;/code&gt;, just to show it when &lt;code&gt;input&lt;/code&gt; got no placeholder anymore, so, when we're writing over it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l4KhStwfccoc7t6IU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l4KhStwfccoc7t6IU/giphy.gif" alt="cool"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>placeholder</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Fast &amp; easy... React states management in one function</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Sat, 23 Jun 2018 15:28:33 +0000</pubDate>
      <link>https://dev.to/genta/fast--easy-react-states-management-in-one-function-2h08</link>
      <guid>https://dev.to/genta/fast--easy-react-states-management-in-one-function-2h08</guid>
      <description>&lt;h1&gt;
  
  
  Don't repeat the code...
&lt;/h1&gt;

&lt;p&gt;In React, like anywhere else in your code, you must &lt;strong&gt;never&lt;/strong&gt; repeat yourself unless it is &lt;strong&gt;strictly&lt;/strong&gt; necessary (almost never).&lt;/p&gt;

&lt;p&gt;Probably what you're going to read, it's easy stuff, but reading some code online, I thought about dealing with the subject ... I "apologize" to the &lt;em&gt;experts&lt;/em&gt; for the banality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example...
&lt;/h2&gt;

&lt;p&gt;We have to manage a text input tag, and make sure to memorize the value entered by the user, and show It somewhere, till it's changed again!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we do it in React?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We insert an &lt;code&gt;onChange&lt;/code&gt; that updates the &lt;code&gt;state&lt;/code&gt; of the component to the last input value ... and then we pass the value of the &lt;code&gt;state&lt;/code&gt; to the input tag, or to any other tag on the page, to show the last input value.&lt;/p&gt;

&lt;h2&gt;
  
  
  To the code!
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//we've a class... with a state defined&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;myFirstState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//we're going to manage the changes, and setState() equal to the user input&lt;/span&gt;

   &lt;span class="nx"&gt;valueChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;myFirstState&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//rendering time...we've an Input tag, with the state as value &lt;/span&gt;
&lt;span class="c1"&gt;//and the function, calling to onChange&lt;/span&gt;

&lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;just a class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;insertValue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;myFirstState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
                 &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;That's &lt;em&gt;basic&lt;/em&gt; stuff.&lt;br&gt;
And It's just a way to go for It.&lt;/p&gt;

&lt;p&gt;But what If we've more than one &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; ?&lt;br&gt;
Not two... not three... we've more than ten &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; doing the &lt;strong&gt;same&lt;/strong&gt; stuff?&lt;br&gt;
(it is an exaggeration useful for the post)&lt;/p&gt;

&lt;p&gt;As you can see our &lt;code&gt;handleChange&lt;/code&gt; function, changes a specific state in It's &lt;code&gt;setState()&lt;/code&gt; ... if we use the same function for other &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; we will change the value of &lt;code&gt;myFirstState&lt;/code&gt; everytime.&lt;br&gt;
(And trust me... I have seen people use numerous functions doing the same stuff, like these.)&lt;/p&gt;

&lt;h2&gt;
  
  
  My idea is...
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//we've a class... with a more states defined&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;myFirstState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;mySecondState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Akita&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//we're going to manage the changes, and setState() equal &lt;/span&gt;
&lt;span class="c1"&gt;//to the user input... for all the possible "keys" in the state object&lt;/span&gt;

      &lt;span class="nx"&gt;valueChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;obj&lt;/span&gt;&lt;span class="o"&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;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//rendering time...we've more Input tags, with the states as values and one&lt;/span&gt;
&lt;span class="c1"&gt;//function, calling onChange... we're passing the "key" as argument.&lt;/span&gt;

&lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;just a class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;insertValue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;myFirstState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
                 &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myFirstState&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;insertValue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;mySecondState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
                 &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mySecondState&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;With that, we're calling the method &lt;code&gt;setState()&lt;/code&gt; passing an object where the &lt;code&gt;key&lt;/code&gt; is the state we want to change and the value is the user input!.&lt;br&gt;
We're also binding &lt;code&gt;this&lt;/code&gt; or we're going to receive an &lt;em&gt;error&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;(Keep in mind... &lt;code&gt;setState()&lt;/code&gt; triggers the render)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I hope I've been useful ... I'm looking for suggestions to manage this kind of case in more efficient ways!&lt;/strong&gt;&lt;/p&gt;



&lt;center&gt;&lt;img src="https://i.giphy.com/media/QbqiJzOOpolWg/source.gif" alt="hug"&gt;&lt;/center&gt; 



&lt;center&gt;Cya adventurers&lt;/center&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>function</category>
      <category>web</category>
    </item>
    <item>
      <title>There's no "else if" in JS </title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Sun, 20 May 2018 10:12:21 +0000</pubDate>
      <link>https://dev.to/genta/theres-no-else-if-in-js--24f9</link>
      <guid>https://dev.to/genta/theres-no-else-if-in-js--24f9</guid>
      <description>&lt;h1&gt;
  
  
  Grammar is not a joke...
&lt;/h1&gt;

&lt;p&gt;Exactly, in Javascript's grammar &lt;strong&gt;there's no&lt;/strong&gt; &lt;code&gt;else if&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;How many times have you used it before? &lt;strong&gt;Why It's still working?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We always code 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="nx"&gt;wow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&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;arg&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dog&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;LOVELY&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="k"&gt;else&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;arg&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CUTE&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="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gimme an animal&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="nx"&gt;wow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; "CUTE"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;But what's really happening is 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="nx"&gt;wow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&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;arg&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dog&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;LOVELY&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="k"&gt;else&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;arg&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CUTE&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="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gimme an animal&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="nx"&gt;wow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&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;What's happening here?&lt;/p&gt;

&lt;p&gt;Literally, we're using some implicit JS behavior about &lt;code&gt;{}&lt;/code&gt; uses.&lt;/p&gt;

&lt;p&gt;When we use the &lt;code&gt;else if&lt;/code&gt; statement we're omitting the &lt;code&gt;{}&lt;/code&gt; but Javascript It's still working because It does not requires the parentheses in that case, like in many other cases!&lt;/p&gt;

&lt;h2&gt;
  
  
  ...so what?
&lt;/h2&gt;

&lt;p&gt;I'm not writing this post, just because It's something &lt;strong&gt;really curious&lt;/strong&gt; to know.&lt;/p&gt;

&lt;p&gt;I'm writing this to make you think about all the &lt;em&gt;good parts&lt;/em&gt; or &lt;em&gt;right ways&lt;/em&gt; to code, that forces you to write code in a way, that sometimes It's not really the &lt;strong&gt;best way&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There's a lot to discuss about &lt;em&gt;implicit&lt;/em&gt; and &lt;em&gt;explicit&lt;/em&gt; declaration of stuff like: coercion, parentheses, semicolon...&lt;/p&gt;

&lt;p&gt;But the true always stands in the middle!.&lt;/p&gt;

&lt;p&gt;If you just follow some specific rules on &lt;em&gt;how to...&lt;/em&gt; you're not understanding &lt;strong&gt;why&lt;/strong&gt; those &lt;em&gt;rules&lt;/em&gt; were written, and this &lt;code&gt;else if&lt;/code&gt; should make you think about It.&lt;/p&gt;

&lt;p&gt;How many time have you written code, because someone told you &lt;em&gt;to do so&lt;/em&gt; but you were totally blind about It?&lt;/p&gt;

&lt;p&gt;I bet, &lt;strong&gt;a lot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I'm not saying that we should not care about &lt;strong&gt;ALL&lt;/strong&gt; those rules, and that we should know &lt;strong&gt;ALL&lt;/strong&gt; the JS documentation.&lt;/p&gt;

&lt;p&gt;I'm just saying that right now, your duty is to write &lt;strong&gt;good code that can be understood by someone else&lt;/strong&gt; and to go that way... &lt;em&gt;some&lt;/em&gt; rules are ok, but you should know &lt;strong&gt;the why&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because someone is good at code, It does not mean that you &lt;strong&gt;have to&lt;/strong&gt; follow his &lt;em&gt;golden rules&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What's &lt;em&gt;implicit&lt;/em&gt; for him, maybe &lt;em&gt;explicit&lt;/em&gt; for you and many other people. &lt;/p&gt;

&lt;p&gt;If you don't have the same knowledge, about that specific argument (and It's not possible to have exactly the same level of &lt;em&gt;know about It&lt;/em&gt; in every single part of the code) you've two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Do what he's telling you to do... If It works.&lt;/li&gt;
&lt;li&gt;Go out and check &lt;strong&gt;the why&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Always care about &lt;em&gt;the good parts&lt;/em&gt; but first of all, always care about &lt;em&gt;your knowledge&lt;/em&gt; and don't code just by rules.&lt;/p&gt;



&lt;center&gt;Best-practices must be accepted by many people&lt;/center&gt;
&lt;br&gt;&lt;br&gt;
&lt;center&gt;&lt;img src="https://i.giphy.com/media/GD99jGJdGxgTS/giphy.gif" alt="think"&gt;&lt;/center&gt; 

</description>
      <category>javascript</category>
      <category>think</category>
      <category>conditional</category>
    </item>
    <item>
      <title>Is Javascript a compiled language?</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Wed, 16 May 2018 09:26:21 +0000</pubDate>
      <link>https://dev.to/genta/is-javascript-a-compiled-language-20mf</link>
      <guid>https://dev.to/genta/is-javascript-a-compiled-language-20mf</guid>
      <description>&lt;h1&gt;
  
  
  It really Is...
&lt;/h1&gt;

&lt;p&gt;Why do people still look at JS as a &lt;em&gt;dynamic&lt;/em&gt; or &lt;em&gt;interpreted&lt;/em&gt; language?&lt;/p&gt;

&lt;p&gt;There's a lot of misconception about the &lt;em&gt;compiling&lt;/em&gt; of JS, and still now, with lots of information on the web, most people still argue with that and still don't know how exactly JS works before the &lt;em&gt;runtime&lt;/em&gt; phase.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript is a compiled language...
&lt;/h3&gt;

&lt;p&gt;despite the fact that the &lt;em&gt;compiling&lt;/em&gt; of JS works in a different way, if compared to other compiled language, It's still &lt;strong&gt;following some rules&lt;/strong&gt; that reflect the process of &lt;em&gt;compiling&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First... we've to quote this from &lt;em&gt;wikipedia&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We all knows that computers &lt;strong&gt;don't&lt;/strong&gt; speak Java or JS or Python and don't matter which language we're using, we're always &lt;em&gt;translating&lt;/em&gt; our code into something that the machine can understand... but that's not the most important thing for now.&lt;/p&gt;

&lt;p&gt;Important is... this kind of translation is called &lt;em&gt;code generation&lt;/em&gt; and it's input is the &lt;strong&gt;Abstract Syntax Tree&lt;/strong&gt; (AST) that is about some &lt;em&gt;nested elements&lt;/em&gt; that represent the structure of the program. The structuring of this tree, happens in the  &lt;em&gt;parsing&lt;/em&gt; phase of compiling.&lt;/p&gt;

&lt;p&gt;Of course we've to provide something to create this &lt;em&gt;AST&lt;/em&gt; ... and we do ... we provide an &lt;em&gt;array of tokens&lt;/em&gt;, from the previous compiling &lt;em&gt;lexing&lt;/em&gt; phase.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let dog = labrador;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;will be &lt;em&gt;tokenized&lt;/em&gt; like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let,dog,=,labrador,;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This &lt;em&gt;splitted&lt;/em&gt; version of our code, means something for the language and creates that stream of informations to generate the &lt;em&gt;AST&lt;/em&gt;.&lt;br&gt;
We now have a &lt;code&gt;variableDeclaration&lt;/code&gt; and an &lt;code&gt;assignment&lt;/code&gt; and so on... in our &lt;em&gt;tree&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I've been &lt;strong&gt;not&lt;/strong&gt; very specific, because this post is about the fact that &lt;strong&gt;in JS all of this, It's happening&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Yes.&lt;/p&gt;

&lt;p&gt;Javascript follows all of this &lt;em&gt;compiling&lt;/em&gt; phases, in the order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexing&lt;/li&gt;
&lt;li&gt;Parsing&lt;/li&gt;
&lt;li&gt;Code Generation &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The JS &lt;em&gt;compiling&lt;/em&gt; It's not happening to make it works on different platforms or stuff like that... but &lt;strong&gt;It's happening&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is not something you should know as generic... this is something that can &lt;strong&gt;totally change&lt;/strong&gt; your point of view about lots of JS behaviours.&lt;/p&gt;

&lt;p&gt;Just quick examples are &lt;strong&gt;lexical scoping&lt;/strong&gt; and &lt;strong&gt;hoisting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The variable declarations in JS happens during the &lt;em&gt;lexing&lt;/em&gt; phase, while the assignement happens &lt;em&gt;on runtime&lt;/em&gt; and that's why &lt;strong&gt;hoisting&lt;/strong&gt; It's happening in a more technical and correct point of view.&lt;br&gt;
The scope, in JS, It's defined in It's &lt;em&gt;lexing&lt;/em&gt; phase and that's why JS has got the &lt;strong&gt;lexical scoping&lt;/strong&gt; definition.&lt;/p&gt;

&lt;p&gt;What about &lt;strong&gt;closures&lt;/strong&gt; ? More complex... but still something that happens because of &lt;em&gt;scope reference&lt;/em&gt; and &lt;em&gt;lexical scoping&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, people, JS is quickly compiled, everytime... and there's lot of optimization included in the engine to make it possible without any collateral problem in performances, that you can break if you're not conscious about this stuff.&lt;/p&gt;



&lt;center&gt;&lt;strong&gt;Have fun, looking for more info!&lt;/strong&gt;&lt;/center&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>compile</category>
    </item>
    <item>
      <title>What the... error handling! (try...catch) </title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Tue, 08 May 2018 13:53:38 +0000</pubDate>
      <link>https://dev.to/genta/what-the-error-handling-trycatch--k48</link>
      <guid>https://dev.to/genta/what-the-error-handling-trycatch--k48</guid>
      <description>&lt;h1&gt;
  
  
  Please check your code!
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Bugs&lt;/strong&gt; exist... and they always will.&lt;/p&gt;

&lt;p&gt;No matter how &lt;em&gt;good&lt;/em&gt; we are at code, or how &lt;em&gt;focused&lt;/em&gt; is our mind while coding... there will be &lt;strong&gt;bugs&lt;/strong&gt; like &lt;em&gt;Coding bugs&lt;/em&gt; or &lt;em&gt;Input bugs&lt;/em&gt; or whatever.&lt;/p&gt;

&lt;h2&gt;
  
  
  we have a duty ... to do our best to control them
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;very basic&lt;/strong&gt; way to control our code, is something really smart and easy.&lt;/p&gt;

&lt;p&gt;Is there an error? Do this.&lt;br&gt;
Not an error? Do this.&lt;br&gt;
Anyway... do also this.&lt;/p&gt;

&lt;p&gt;We can see this kind of stuff, in code 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;const&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;samoyed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//is there an&lt;/span&gt;
        &lt;span class="nx"&gt;akita&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//?&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="s2"&gt;Ouch, no Akita here &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;finally&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="s2"&gt;At least there's a labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; Ouch, no Akita here ReferenceError: akita is not defined&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; At least there's a labrador&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, what's happening here?&lt;/p&gt;

&lt;p&gt;We created a simple:&lt;br&gt;
&lt;code&gt;try{}&lt;br&gt;
catch{}&lt;br&gt;
finally{}&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  try{}
&lt;/h3&gt;

&lt;p&gt;Here we're &lt;em&gt;trying&lt;/em&gt; to execute the code&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;breeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;samoyed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//is there an&lt;/span&gt;
        &lt;span class="nx"&gt;akita&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//?&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see there's an &lt;code&gt;akita&lt;/code&gt; variable, but nothing in the program has to do with that, so there's an error&lt;/p&gt;

&lt;h3&gt;
  
  
  catch{}
&lt;/h3&gt;

&lt;p&gt;When an &lt;strong&gt;error occurs&lt;/strong&gt; the &lt;code&gt;catch{}&lt;/code&gt; statement is called.&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;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="s2"&gt;Ouch, no Akita here &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;e&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;All the &lt;em&gt;stuff inside&lt;/em&gt; the statement will be executed and we will &lt;em&gt;console out&lt;/em&gt; the String and that &lt;strong&gt;strange&lt;/strong&gt; &lt;em&gt;e&lt;/em&gt;.&lt;br&gt;
That one is an &lt;em&gt;Error Object&lt;/em&gt; a built in &lt;em&gt;Object&lt;/em&gt; that &lt;em&gt;JS&lt;/em&gt; provides us to &lt;strong&gt;capture&lt;/strong&gt; what's happening when an error occurs.&lt;/p&gt;

&lt;p&gt;There are lot of stuff that can be done with It. If you use &lt;code&gt;e.stack&lt;/code&gt; It will return &lt;em&gt;where in the stack&lt;/em&gt; the error occurred.&lt;/p&gt;

&lt;p&gt;We can also define our &lt;em&gt;custom error&lt;/em&gt;, later we'll see It.&lt;/p&gt;
&lt;h3&gt;
  
  
  finally{}
&lt;/h3&gt;

&lt;p&gt;This statement, always happens.&lt;br&gt;
No matter if there's an error or the code is &lt;em&gt;good&lt;/em&gt; ... the &lt;code&gt;finally{}&lt;/code&gt; will always be executed.&lt;/p&gt;
&lt;h2&gt;
  
  
  our custom error...
&lt;/h2&gt;

&lt;p&gt;Sometimes, It can be really important to create our &lt;em&gt;custom error&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Maybe we want give a name to that &lt;em&gt;kind of error&lt;/em&gt;.&lt;br&gt;
Maybe we want a better code, more understandable.&lt;br&gt;
There are lot of good reasons.&lt;/p&gt;

&lt;p&gt;Here It's the &lt;code&gt;throw&lt;/code&gt;&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;breeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;samoyed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;try&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Akita&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no Akita 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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; SyntaxError: no Akita here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See? We've just created a new "SyntaxError".&lt;br&gt;
When the &lt;em&gt;try meets an error&lt;/em&gt; the &lt;em&gt;throw&lt;/em&gt; will immediately call the &lt;em&gt;catch&lt;/em&gt; and a &lt;em&gt;new Error&lt;/em&gt; will be created.&lt;/p&gt;
&lt;h3&gt;
  
  
  We can go deeper with this.
&lt;/h3&gt;

&lt;p&gt;What If we want to create a totally &lt;em&gt;new Error Object&lt;/em&gt; so that, the &lt;em&gt;Error&lt;/em&gt; will be thrown &lt;strong&gt;only&lt;/strong&gt; if It's an instance of that &lt;em&gt;kind of Error&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;We can do 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;class&lt;/span&gt; &lt;span class="nx"&gt;noDog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Error&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;findDog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Akita&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="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Here you are&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="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="nx"&gt;noDog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Akita 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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;findDog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&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;e&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;noDog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;e&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="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="nx"&gt;doggy&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labradors&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; No Akita here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hm... this is really not that easy like our previous examples.&lt;br&gt;
So If you dunno what's happening here, don't be scared, It's ok.&lt;/p&gt;

&lt;p&gt;We're defining a &lt;em&gt;class&lt;/em&gt; that &lt;em&gt;extends&lt;/em&gt; the built in &lt;em&gt;Error&lt;/em&gt;.&lt;br&gt;
We're &lt;strong&gt;just&lt;/strong&gt; extending that class. Nothing more.&lt;br&gt;
This new class has got no special properties, It's inheriting everything from the &lt;em&gt;Error&lt;/em&gt; Object.&lt;/p&gt;

&lt;p&gt;In short... we've created a &lt;strong&gt;new instance&lt;/strong&gt; for our &lt;em&gt;errors&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;With that in mind, we can now &lt;em&gt;catch&lt;/em&gt; exceptions &lt;strong&gt;only&lt;/strong&gt; if those are &lt;strong&gt;instanceof noDog&lt;/strong&gt;, and we can separate &lt;em&gt;this kind&lt;/em&gt; of error, from &lt;em&gt;standard one&lt;/em&gt;.&lt;/p&gt;



&lt;center&gt;&lt;img src="https://i.giphy.com/media/TzAorTGGXzkdO/giphy.gif" alt="wow"&gt;&lt;/center&gt;
&lt;br&gt;&lt;br&gt;
&lt;center&gt;COOL&lt;/center&gt;

</description>
      <category>javascript</category>
      <category>bugs</category>
      <category>trycatch</category>
      <category>errors</category>
    </item>
    <item>
      <title>What the... generators!</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Sun, 06 May 2018 08:49:40 +0000</pubDate>
      <link>https://dev.to/genta/what-the-generators-4fmn</link>
      <guid>https://dev.to/genta/what-the-generators-4fmn</guid>
      <description>&lt;p&gt;Want to know about &lt;em&gt;generators&lt;/em&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  FIRST you need to check &lt;a href="https://dev.to/genta/what-the-iterators-1lhc"&gt;iterators&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;That's not something you can skip, because, generators work &lt;em&gt;through&lt;/em&gt; iterators. We can say that &lt;em&gt;generators&lt;/em&gt; are a simpler way to write our own iterators.&lt;/p&gt;

&lt;p&gt;This is a &lt;em&gt;generator&lt;/em&gt;:&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="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&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="nx"&gt;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "labrador", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "chow-chow", done: true}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: undefined, done: true}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let's see what's happening here.&lt;/p&gt;

&lt;p&gt;First, we need to create a &lt;em&gt;generator function&lt;/em&gt;, and we can do that by using a &lt;em&gt;*&lt;/em&gt; between &lt;em&gt;function&lt;/em&gt; and the &lt;em&gt;function name&lt;/em&gt;.&lt;br&gt;
No matter if it's:&lt;br&gt;
&lt;code&gt;function* breeds()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;function *breeds()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const breeds = function *()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const breeds = function*()&lt;/code&gt;&lt;br&gt;
This will work anyway!.&lt;/p&gt;

&lt;p&gt;(But, It's a &lt;em&gt;standard&lt;/em&gt; to use the &lt;em&gt;*&lt;/em&gt; just after the &lt;em&gt;function&lt;/em&gt; ... so try to always use the syntax &lt;code&gt;function*&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;Inside that function we're using that &lt;em&gt;yield&lt;/em&gt; ... what's that?&lt;br&gt;
&lt;em&gt;yield&lt;/em&gt; It's something listening to the &lt;em&gt;next()&lt;/em&gt; calls.&lt;/p&gt;

&lt;p&gt;Everytime we use the &lt;em&gt;.next()&lt;/em&gt; on the iterator, the generator will use the &lt;em&gt;next not yet used&lt;/em&gt; yield.&lt;br&gt;
That's the same &lt;em&gt;idea&lt;/em&gt; behind &lt;em&gt;iterators&lt;/em&gt; and we're checking that, with the &lt;em&gt;done&lt;/em&gt; key the iterators returns!.&lt;/p&gt;

&lt;p&gt;If It's value is &lt;em&gt;false&lt;/em&gt; the &lt;em&gt;.next()&lt;/em&gt; will call the &lt;em&gt;yield&lt;/em&gt; AFTER the previous one.&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;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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "labrador", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "chow-chow", done: true}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: undefined, done: true}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When the &lt;em&gt;done:true&lt;/em&gt; will happens?&lt;br&gt;
When there's no more to &lt;em&gt;yield out&lt;/em&gt; that generator.&lt;/p&gt;

&lt;p&gt;But we need to be more &lt;em&gt;precise&lt;/em&gt; about this.&lt;/p&gt;


&lt;center&gt;&lt;img src="https://i.giphy.com/media/YPiZm67MsCpCo/giphy.gif" alt="focus"&gt;&lt;/center&gt;

&lt;p&gt;We all know that a function, in JS, always returns something.&lt;br&gt;
If you do not define a &lt;em&gt;return&lt;/em&gt;, JS will do It for you, by &lt;em&gt;returning an "undefined"&lt;/em&gt; at the end of It.&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="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&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="nx"&gt;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "labrador", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "chow-chow", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: undefined, done: true}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;By taking away the &lt;em&gt;return&lt;/em&gt; from the function, calling &lt;em&gt;.next()&lt;/em&gt; will force JS to return a &lt;code&gt;{value: undefined, done: true}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So JS still returns It for you, also with &lt;em&gt;generators&lt;/em&gt;, because, we're still talking about functions here!&lt;/p&gt;

&lt;p&gt;If you want to &lt;em&gt;force a return&lt;/em&gt; you can use the &lt;em&gt;.return()&lt;/em&gt; method on the &lt;em&gt;iterator&lt;/em&gt; and finish the &lt;em&gt;generator&lt;/em&gt;.&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="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&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="nx"&gt;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&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;iterator&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;we &amp;lt;3 dogs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: "we &amp;lt;3 dogs", done: true}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: undefined, done: true}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt;{value: undefined, done: true}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As you can see, we're immediately &lt;em&gt;returning&lt;/em&gt; and finishing the generator with the &lt;em&gt;.return()&lt;/em&gt; method!.&lt;/p&gt;

&lt;h2&gt;
  
  
  generators are not magic!
&lt;/h2&gt;

&lt;p&gt;We're really not &lt;em&gt;executing&lt;/em&gt; stuff by calling the &lt;em&gt;generators&lt;/em&gt; ... indeed, we're using the &lt;em&gt;iterator&lt;/em&gt; interface every time we're calling &lt;em&gt;generators&lt;/em&gt; with the &lt;em&gt;.next()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;We can see the &lt;em&gt;.next()&lt;/em&gt; as a remote controller to START (calling yield) and PAUSE (checking the &lt;em&gt;done:&lt;/em&gt; value) the &lt;em&gt;generator function&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's why, we need to define:&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;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;and call the &lt;code&gt;iterator.next()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;iterator&lt;/em&gt; It's just the variable name, not the &lt;em&gt;iterator object&lt;/em&gt; itself)&lt;/p&gt;

&lt;h2&gt;
  
  
  lot of stuff can be done
&lt;/h2&gt;

&lt;p&gt;In real life work, we can do really lot of stuff with &lt;em&gt;generators&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can use &lt;em&gt;generators&lt;/em&gt; values with variables.&lt;br&gt;
We can &lt;em&gt;fetch&lt;/em&gt;, &lt;em&gt;spread&lt;/em&gt; and use the &lt;em&gt;for...of&lt;/em&gt; loop.&lt;/p&gt;

&lt;p&gt;At the moment we're using &lt;em&gt;generators&lt;/em&gt; to call one single &lt;em&gt;yield&lt;/em&gt; at time, we can say, that we're using It in &lt;em&gt;synchronous&lt;/em&gt; way.&lt;/p&gt;

&lt;p&gt;But, generators can be also used in &lt;em&gt;asynchronous&lt;/em&gt; way.&lt;br&gt;
I'm not going to cover It now... you can check about &lt;em&gt;Promises&lt;/em&gt; or &lt;em&gt;async/await&lt;/em&gt;, and maybe, we we'll talk about that, another day.&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;Promises&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function"&gt;Async&lt;/a&gt; &lt;/p&gt;



&lt;center&gt;HAVE FUN WITH CODE&lt;/center&gt;



&lt;center&gt;&lt;img src="https://i.giphy.com/media/WOXeOKbVHcUG4/giphy.gif" alt="Pearl Jam"&gt;&lt;/center&gt;

</description>
      <category>javascript</category>
      <category>iterators</category>
      <category>generators</category>
      <category>loops</category>
    </item>
    <item>
      <title>objects? No... array, please!</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Fri, 04 May 2018 17:44:37 +0000</pubDate>
      <link>https://dev.to/genta/objects-no-array-please-2am</link>
      <guid>https://dev.to/genta/objects-no-array-please-2am</guid>
      <description>&lt;h1&gt;
  
  
  I don't like objects...that much!
&lt;/h1&gt;

&lt;p&gt;This is an &lt;em&gt;object&lt;/em&gt;:&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But sometimes I prefer to work with arrays.&lt;/p&gt;

&lt;p&gt;Why? Because they really look better &lt;strong&gt;to me&lt;/strong&gt;... and there are really a lot of methods or loops that work &lt;em&gt;just&lt;/em&gt; with [arrays]!&lt;/p&gt;

&lt;p&gt;These are some tools used to &lt;em&gt;"convert"&lt;/em&gt; objects to arrays.&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;//Object.values() will give you an array of all the object "values"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;values&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="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; ["labrador", 9]&lt;/span&gt;



&lt;span class="c1"&gt;//Object.keys() will give you an array of all the object "keys"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keys&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="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;keys&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; ["breed", "age"]&lt;/span&gt;



&lt;span class="c1"&gt;//Object.entries()  will give you an arraysh version of the object. &lt;/span&gt;
&lt;span class="c1"&gt;//Where the key and the value will be paired into an array... &lt;/span&gt;
&lt;span class="c1"&gt;//and all of those arrays will be "pushed" into another array.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;entries&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="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;entries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt;[["breed", "labrador"], ["age", 9]]&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;This is &lt;em&gt;easy peasy&lt;/em&gt; stuff, but very often, at the beginning of my journey in &lt;em&gt;JS&lt;/em&gt;, objects were very often a problem for me.&lt;/p&gt;

&lt;p&gt;If only they had told me before ...&lt;/p&gt;

&lt;center&gt;![img](http://static.yourtango.com/cdn/farfuture/6HjAhU1pGFNDKoarYIDtEGMvpAPpSjYWMm6xn0ZlrJw/mtime:1475815278/sites/default/files/notyourfault.gif)&lt;/center&gt; 

&lt;p&gt;&lt;strong&gt;P.S: These tools are ok... if it’s ok to work with arrays instead of objects.&lt;br&gt;
Sometimes you’ve to use objects... because of performance or long term maintenance.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>arrays</category>
      <category>object</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What the... iterators?</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Thu, 03 May 2018 18:26:35 +0000</pubDate>
      <link>https://dev.to/genta/what-the-iterators-1lhc</link>
      <guid>https://dev.to/genta/what-the-iterators-1lhc</guid>
      <description>&lt;h1&gt;
  
  
  ITERATORS are not that scary...
&lt;/h1&gt;

&lt;p&gt;According to the &lt;em&gt;MDN web docs&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;em&gt;for...of&lt;/em&gt; statement creates a loop iterating over &lt;strong&gt;iterable&lt;/strong&gt; objects, , invoking a custom iteration hook with statements to be executed for the value of each distinct property of the object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok... hm... but what's that Iterable object?&lt;/p&gt;

&lt;p&gt;We use iteration almost every time we code... with stuff like: &lt;em&gt;map, filter, for, forEach, ecc...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But the &lt;em&gt;for...of&lt;/em&gt; statement is special, because:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Iterators (...) bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of &lt;em&gt;for...of&lt;/em&gt; loops.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you can think that the &lt;em&gt;for...of&lt;/em&gt; loop knows how to iterate over an array... but that's not true!.&lt;br&gt;
We need the &lt;strong&gt;iterator&lt;/strong&gt; object as "interface" to loop over it:&lt;/p&gt;


&lt;center&gt;&lt;em&gt;for...of &amp;lt;------&amp;gt; &lt;strong&gt;ITERATOR&lt;/strong&gt; &amp;lt;------ array&lt;/em&gt;&lt;/center&gt;

&lt;p&gt;Now that we know this... let code it:&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;dogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cavalier&lt;/span&gt;&lt;span class="dl"&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;breed&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;dogs&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="nx"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; labrador, chow-chow, cavalier&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Behind that... we've the &lt;strong&gt;iterator&lt;/strong&gt; object that looks 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;const&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cavalier&lt;/span&gt;&lt;span class="dl"&gt;"&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;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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;breed&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;iterator&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="nx"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; labrador, chow-chow, cavalier (the same result)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  wait... WHAT?
&lt;/h3&gt;


&lt;center&gt;&lt;img src="https://i.giphy.com/media/1nHK1JcpgGZNu/giphy.gif" alt="what"&gt;&lt;/center&gt;

&lt;p&gt;Yes... that seems really strange, but It's not that bad.&lt;br&gt;
Let's quote, again:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to be iterable, an object must implement the &lt;strong&gt;@@iterator&lt;/strong&gt; method, meaning that the object (or one of the objects up its prototype chain) must have a property with a &lt;strong&gt;Symbol.iterator&lt;/strong&gt; key. This function should return a new iterator for each call, however, this is not a requirement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That &lt;code&gt;Symbol.iterator&lt;/code&gt; is the property every &lt;strong&gt;iterable&lt;/strong&gt; object needs, to loop over It.&lt;/p&gt;

&lt;p&gt;Iterable objects can be:&lt;/p&gt;

&lt;p&gt;String&lt;br&gt;
Array&lt;br&gt;
Map&lt;br&gt;
Set&lt;br&gt;
nodeList&lt;br&gt;
...&lt;/p&gt;

&lt;p&gt;and all these possess that property, behind the scene.&lt;/p&gt;
&lt;h1&gt;
  
  
  Symbol.iterator
&lt;/h1&gt;

&lt;p&gt;The property provides us, the &lt;code&gt;next()&lt;/code&gt; method, that works 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;const&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cavalier&lt;/span&gt;&lt;span class="dl"&gt;"&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;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; {value: "labrador", done: false}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Every time we call the &lt;code&gt;next()&lt;/code&gt; method, we loop over the &lt;strong&gt;iterable&lt;/strong&gt; object.&lt;/p&gt;

&lt;p&gt;Now we've got two keys:&lt;br&gt;
&lt;em&gt;value: "labrador"&lt;/em&gt;&lt;br&gt;
(the current value of the &lt;strong&gt;iterable&lt;/strong&gt; object)&lt;br&gt;
&lt;em&gt;done: false&lt;/em&gt;&lt;br&gt;
(will be &lt;em&gt;false&lt;/em&gt; until we've values inside of it!)&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;dogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chow-chow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cavalier&lt;/span&gt;&lt;span class="dl"&gt;"&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;iterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; {value: "labrador", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; {value: "chow-chow", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; {value: "cavalier", done: false}&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;iterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; {value: undefined, done: true}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;done:true&lt;/code&gt; is the ending point for the &lt;strong&gt;iterable&lt;/strong&gt; object.&lt;/p&gt;

&lt;p&gt;There are some other powerful tools in &lt;em&gt;JS&lt;/em&gt; that need the object to be &lt;strong&gt;iterable&lt;/strong&gt;&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;//TAKE A LOOK AT THE SPREAD OPERATOR...&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;dogs&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; TypeError: dogs is not iterable&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;labrador&lt;/span&gt;&lt;span class="dl"&gt;"&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;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;dogs&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;doggy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//-&amp;gt; ["l", "a", "b", "r", "a", "d", "o", "r"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You've to be &lt;em&gt;always&lt;/em&gt; careful about this, and use only &lt;strong&gt;iterable&lt;/strong&gt; objects with tools like these!.&lt;/p&gt;


&lt;center&gt;&lt;img src="https://i.giphy.com/media/3o7Zemg33eB4BFOeRi/giphy.gif" alt="danger"&gt;&lt;/center&gt;

&lt;p&gt;I want to say &lt;em&gt;byebye&lt;/em&gt; adding a bit of spice:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Symbol.iterator&lt;/code&gt; is a property... &lt;strong&gt;so&lt;/strong&gt;... you can create your own &lt;strong&gt;iterable&lt;/strong&gt; object if you want to!&lt;/p&gt;

&lt;p&gt;For more info:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators"&gt;The mighty MDN guide&lt;/a&gt;&lt;/p&gt;



&lt;center&gt;&lt;strong&gt;Have fun with it!&lt;/strong&gt;&lt;/center&gt;

</description>
      <category>javascript</category>
      <category>iterators</category>
      <category>loops</category>
      <category>forof</category>
    </item>
    <item>
      <title>Array reference... and how to not!</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Sat, 28 Apr 2018 15:56:48 +0000</pubDate>
      <link>https://dev.to/genta/array-reference-and-how-to-not-54ng</link>
      <guid>https://dev.to/genta/array-reference-and-how-to-not-54ng</guid>
      <description>&lt;h1&gt;
  
  
  Sometimes it's sticky...
&lt;/h1&gt;

&lt;p&gt;Look at this code... what do you think is going to happen to the &lt;em&gt;breeds&lt;/em&gt; array?&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;breeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Akita&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Siamese&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// Let's make an array of only Dogs Breed... splice out Siamese.&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;doggy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&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="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;doggy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; ["Labrador", "Akita"]&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;breeds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; ["Labrador", "Akita"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So... we just wanted to change the doggy &lt;em&gt;array&lt;/em&gt;, and not the &lt;em&gt;breeds&lt;/em&gt; one.&lt;/p&gt;

&lt;p&gt;We told JavaScipt:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I want a new array;&lt;/li&gt;
&lt;li&gt;Call It &lt;em&gt;doggy&lt;/em&gt;;&lt;/li&gt;
&lt;li&gt;Give doggy the &lt;strong&gt;same values&lt;/strong&gt; of &lt;em&gt;Breeds&lt;/em&gt;;
&lt;/li&gt;
&lt;/ol&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;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Javascript with the &lt;em&gt;"="&lt;/em&gt; has created a &lt;strong&gt;REFERENCE&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With our declaration &lt;em&gt;doggy&lt;/em&gt; and &lt;em&gt;breeds&lt;/em&gt; are pointing at the "same object" by reference... the same memory, and by changing one, you're changing both!&lt;/p&gt;


&lt;center&gt;&lt;strong&gt;evil TWINS&lt;/strong&gt;&lt;/center&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/iGei6Nd3NlwnC/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/iGei6Nd3NlwnC/giphy.gif" alt="twins"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Let's make a list... how not to create a reference
&lt;/h1&gt;

&lt;p&gt;If we want to pass just the &lt;em&gt;values&lt;/em&gt; of an array into another, and at the same time create a &lt;em&gt;"new object"&lt;/em&gt;.&lt;br&gt;
We can use this useful methods.&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;//1. Create a copy with slice.&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//2. Create a concatenation with an empty array.&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//3. Spread an array into another one.&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;//4. Use the Array.from() method, to create an array, with the same value of //another one&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the methods, up here, are creating a totally &lt;strong&gt;NEW&lt;/strong&gt; &lt;em&gt;doggy&lt;/em&gt; array, without any reference to the &lt;em&gt;breeds&lt;/em&gt; one.&lt;br&gt;
You can now take off the &lt;em&gt;Siamese&lt;/em&gt; without any collateral effect.&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;breeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Labrador&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Akita&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Siamese&lt;/span&gt;&lt;span class="dl"&gt;"&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;doggy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;breeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;doggy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&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="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;doggy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; ["Labrador", "Akita"]&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;breeds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; ["Labrador", "Akita", "Siamese"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;center&gt;Now they're in different arrays... but can still love each other!&lt;/center&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/796g6BvEXi7v2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/796g6BvEXi7v2/giphy.gif" alt="love"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>reference</category>
      <category>tricks</category>
    </item>
    <item>
      <title>What the... hoisting?</title>
      <dc:creator>Fabio Russo</dc:creator>
      <pubDate>Fri, 27 Apr 2018 20:47:35 +0000</pubDate>
      <link>https://dev.to/genta/what-the-hoisting-5f82</link>
      <guid>https://dev.to/genta/what-the-hoisting-5f82</guid>
      <description>&lt;p&gt;Hoisting in Javascript... has always been a &lt;strong&gt;really bad&lt;/strong&gt; behavior of the language itself.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why are you doing this for me?
&lt;/h1&gt;

&lt;p&gt;If you dunno what I'm talking about, that's what (in short words) the hoisting is:&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;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;test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm not here&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;As you can see, when I'm calling the &lt;em&gt;console.log(test)&lt;/em&gt; the variable itself, has not been declared at all!.&lt;/p&gt;

&lt;p&gt;But Javascript is a bad guy that chooses to declare it for you.&lt;br&gt;
Here's what happened:&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;//javascript will declare the variable, moving it at the TOP of it's scope&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&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;test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; undefined&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm not here&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;this is really confusing, and should not happen at all!.&lt;/p&gt;

&lt;p&gt;And we've not finished yet, because this also happens into function's scope.&lt;br&gt;
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;test&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;test&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm not 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="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="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  And also happening with a function itself!.
&lt;/h2&gt;

&lt;p&gt;If we move the call to &lt;em&gt;console.log(test)&lt;/em&gt; at the top of the call stack, the &lt;br&gt;
function &lt;em&gt;test()&lt;/em&gt; should not exists...&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;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="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I should not be here&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;test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// -&amp;gt; "I should not be here"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing to do... Javascript is moving the function to the top of the Scope... so you can call a function, before you declare it...&lt;/p&gt;

&lt;p&gt;But wait:&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;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;test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm not here&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;test&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;Why there's an &lt;em&gt;undefined&lt;/em&gt; if we can call a function, before we declare it?&lt;/p&gt;

&lt;p&gt;Because, in this last code, only the &lt;em&gt;var test&lt;/em&gt; declaration has been moved to the top, and not the function assigned to It.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's RECAP!
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Everytime&lt;/strong&gt; you declare a &lt;em&gt;var&lt;/em&gt; into a scope or write a function declaration, &lt;strong&gt;Javascript HOISTING moves those at the TOP of their scope without their value&lt;/strong&gt;.&lt;/p&gt;

&lt;center&gt;No way... I'm leaving!&lt;/center&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%2Fi.imgur.com%2F4jop4FB.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%2Fi.imgur.com%2F4jop4FB.gif" title="Logo Title Text 1" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  So... &lt;strong&gt;"Let"&lt;/strong&gt; and &lt;strong&gt;"Const"&lt;/strong&gt; ...they are our salvation!
&lt;/h1&gt;

&lt;p&gt;Let's look at what happens with &lt;em&gt;Let&lt;/em&gt; and &lt;em&gt;Const&lt;/em&gt; if we try to do the same dirty stuff:&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;test&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;confusing&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;confusing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finally!&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="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="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;//-&amp;gt; ReferenceError: can't access lexical declaration `confusing' before         initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;AH AH!&lt;/strong&gt; so, here you're!&lt;/p&gt;

&lt;p&gt;and the same happens with const:&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;test&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;confusing&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;confusing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Still Safe!&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="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="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Can you guess, what happens with "let" and "const" in the global scope?&lt;/strong&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%2Fm.popkey.co%2F8b80f7%2FNv8Gx.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%2Fm.popkey.co%2F8b80f7%2FNv8Gx.gif" title="Logo Title Text 1" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because let is a "block scope local variable" and you need to put it into a block to unleash it's power...&lt;/p&gt;

&lt;center&gt;_But this is another story_&lt;/center&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%2Fmedia3.giphy.com%2Fmedia%2FudRSyh5yLCqKQ%2Fgiphy.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%2Fmedia3.giphy.com%2Fmedia%2FudRSyh5yLCqKQ%2Fgiphy.gif" title="Logo Title Text 1" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hoisting</category>
      <category>behavior</category>
    </item>
  </channel>
</rss>
