<?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: Ilê Caian</title>
    <description>The latest articles on DEV Community by Ilê Caian (@caiangums).</description>
    <link>https://dev.to/caiangums</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%2F328470%2F8c84fa62-89d7-4225-860d-cade31a4e607.jpeg</url>
      <title>DEV Community: Ilê Caian</title>
      <link>https://dev.to/caiangums</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/caiangums"/>
    <language>en</language>
    <item>
      <title>Eryn -  React Native Template</title>
      <dc:creator>Ilê Caian</dc:creator>
      <pubDate>Tue, 26 Jan 2021 15:55:04 +0000</pubDate>
      <link>https://dev.to/caiangums/eryn-react-native-template-1921</link>
      <guid>https://dev.to/caiangums/eryn-react-native-template-1921</guid>
      <description>&lt;p&gt;Always starting a project with React Native leads me to make some changes and make choices on how it should be structured. In general, I think in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Directories Structure&lt;/li&gt;
&lt;li&gt;Common Libraries&lt;/li&gt;
&lt;li&gt;Libraries Configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even tho some choices can vary between projects, some of them are constant. I noticed then that I could create a template for making life easier and simple, yet still keep offering a place for dealing with the different kinds of projects that could appear.&lt;/p&gt;

&lt;p&gt;I also noticed that I could create a "foundation" for all those who also wanted to use this template at this peak, leaving the project and architectural choices with documentation and some more general Q&amp;amp;A about my vision related to the project structure.&lt;/p&gt;

&lt;p&gt;So, I present you 🌲Eryn Template! A React Native Template that could be used directly with the CLI and gives you the support to scale your application and let it open to community contributions! Use it with the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npx react-native init MyProject &lt;span class="nt"&gt;--template&lt;/span&gt; react-native-template-eryn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/caiangums/react-native-template-eryn/"&gt;GitHub Project&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://caiangums.github.io/react-native-template-eryn/"&gt;Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Cover Photo by &lt;a href="https://unsplash.com/@jaymantri?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Jay Mantri&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Yarn + NodeJS LTS with Homebrew</title>
      <dc:creator>Ilê Caian</dc:creator>
      <pubDate>Fri, 03 Apr 2020 13:34:40 +0000</pubDate>
      <link>https://dev.to/caiangums/yarn-nodejs-lts-with-homebrew-1256</link>
      <guid>https://dev.to/caiangums/yarn-nodejs-lts-with-homebrew-1256</guid>
      <description>&lt;p&gt;The last &lt;strong&gt;Yarn&lt;/strong&gt; version in &lt;strong&gt;Homebrew&lt;/strong&gt; uses a non-LTS version (&lt;code&gt;15&lt;/code&gt;) of &lt;strong&gt;NodeJS&lt;/strong&gt; as its dependency. If this is an issue for you as it is for me, I just managed to accomplish use &lt;strong&gt;Yarn&lt;/strong&gt; with the latest LTS version of &lt;strong&gt;NodeJS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I created a &lt;a href="https://gist.github.com/caian-gums/b9ddd042772649bca8d55ff6ed94b2be"&gt;gist for it&lt;/a&gt; and here are the following steps&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uninstall Yarn and Node
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew uninstall yarn
&lt;span class="nv"&gt;$ &lt;/span&gt;brew uninstall node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install the latest LTS version of Node
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;node@12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create the Symlink for it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /usr/local/opt/node@12 /usr/local/opt/node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install Yarn without its dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;yarn &lt;span class="nt"&gt;--ignore-dependencies&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it!&lt;/p&gt;

&lt;p&gt;If you want to check the versions of each package, just run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Yarn Version: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;yarn &lt;span class="nt"&gt;--version&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Node Version: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;node &lt;span class="nt"&gt;--version&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Cover Photo by &lt;a href="https://unsplash.com/@kimtheris?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Sereja Ris&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/cat?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>node</category>
      <category>yarn</category>
      <category>homebrew</category>
    </item>
    <item>
      <title>The JS equality comparison issue</title>
      <dc:creator>Ilê Caian</dc:creator>
      <pubDate>Mon, 30 Mar 2020 14:25:18 +0000</pubDate>
      <link>https://dev.to/caiangums/the-js-equality-comparison-issue-1i7m</link>
      <guid>https://dev.to/caiangums/the-js-equality-comparison-issue-1i7m</guid>
      <description>&lt;p&gt;As a common starting point of learning programming languages, one of the first things to do is discover the basic types of that language. Using them to store information as variables or even creating more complex data types, in general, all languages share some simple data types between them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Numbers&lt;/em&gt;: &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;-4&lt;/code&gt;, &lt;code&gt;4.2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Text&lt;/em&gt;: &lt;code&gt;"John Doe"&lt;/code&gt;, &lt;code&gt;"Insert your age"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Lists&lt;/em&gt;: [&lt;code&gt;"the list has"&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;"elements"&lt;/code&gt;]&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Boolean&lt;/em&gt;: &lt;code&gt;true&lt;/code&gt;/&lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are more basic types considered essential for storing information depending on each choice of language but for now, those types can fill all the needs to the reader.&lt;/p&gt;

&lt;p&gt;Another common knowledge that a developer aspires to acquire in a new language is &lt;strong&gt;compare things&lt;/strong&gt;. Compare information during the execution of the program and do things based on the result of the &lt;em&gt;comparision&lt;/em&gt; is essential software development. Those &lt;em&gt;comparisons&lt;/em&gt; can happen using the so-called &lt;code&gt;if&lt;/code&gt;-statement and passing a comparison using one of the comparisons operators&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&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;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm John!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output =&amp;gt; "Hi, I'm John!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of one comparison always evaluates a &lt;em&gt;Boolean&lt;/em&gt; value: &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. The actual comparison can be between anything and will always check if those values respect the &lt;em&gt;comparison operator&lt;/em&gt; rule. The &lt;code&gt;==&lt;/code&gt; is called the &lt;em&gt;equality operator&lt;/em&gt; and compares if both values are equal and result in &lt;code&gt;true&lt;/code&gt; if they are, and &lt;code&gt;false&lt;/code&gt; if don't.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhoibor5bhu33ftriprox.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhoibor5bhu33ftriprox.jpeg" alt="Photo by Annie Spratt on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Annie Spratt on Unsplash



&lt;h2&gt;
  
  
  What can be compared?
&lt;/h2&gt;

&lt;p&gt;JavaScript is a language born with some features (or flaws 🤔). As JavaScript born to be a language to run inside the Browser and used by doing things across the page filled with text in it, one of the &lt;code&gt;==&lt;/code&gt; features is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare if two values are equal, not considering if they are &lt;em&gt;Numbers&lt;/em&gt; or &lt;em&gt;Text&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This feature results in a strange behavior that confuses more than help. An example of this is the following code resulting in a valid comparison&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12&lt;/span&gt;&lt;span class="dl"&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;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm 12 years old!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output =&amp;gt; "I'm 12 years old!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And will log the text &lt;code&gt;"I'm 12 years old!"&lt;/code&gt;. At first sight, this is not a real problem but this can result in strange behaviors. Check the following code and guess what will be the result&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12&lt;/span&gt;&lt;span class="dl"&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;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output =&amp;gt; ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result should be&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Number&lt;/em&gt; &lt;code&gt;13&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Text&lt;/em&gt; &lt;code&gt;"13"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Text&lt;/em&gt; &lt;code&gt;"121"&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result is the answer 3: &lt;code&gt;"121"&lt;/code&gt;. Why?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; operator behaves like a common &lt;strong&gt;sum&lt;/strong&gt; for &lt;em&gt;Numbers&lt;/em&gt; and like a &lt;strong&gt;concatenation&lt;/strong&gt; operator for &lt;em&gt;Text&lt;/em&gt;, even if one of the parts is a &lt;em&gt;Number&lt;/em&gt;. The following example helps to understand how that works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; "John Doe"&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;olderAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 13&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  But what about the === operator?
&lt;/h2&gt;

&lt;p&gt;After acknowledging this behavior with the &lt;code&gt;==&lt;/code&gt; operator, JavaScript received a new 'comparison' operator: &lt;code&gt;===&lt;/code&gt; or the &lt;strong&gt;triple equality operator&lt;/strong&gt;. With this operator, it is possible to compare the value &lt;strong&gt;and&lt;/strong&gt; if both of them are the same basic type.&lt;/p&gt;

&lt;p&gt;This guarantees that the following code will only execute if the &lt;code&gt;age&lt;/code&gt; variable is a &lt;em&gt;Number&lt;/em&gt; with the value of &lt;code&gt;12&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;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12&lt;/span&gt;&lt;span class="dl"&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;age&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output =&amp;gt; "12"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the final log will print just the &lt;code&gt;"12"&lt;/code&gt; value, as the &lt;code&gt;age&lt;/code&gt; variable is a &lt;em&gt;Text&lt;/em&gt;  and not a &lt;em&gt;Number&lt;/em&gt;, even respecting that &lt;code&gt;age&lt;/code&gt; variable has the &lt;code&gt;"12"&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;There are more issues with the &lt;code&gt;==&lt;/code&gt; operator that will not be covered here but as a small example of them, there is a comparison of &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; and what they evaluate&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="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fljgy5yi4vcxocenf5j8q.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fljgy5yi4vcxocenf5j8q.jpeg" alt="Photo by Kelly Sikkema on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Kelly Sikkema on Unsplash



&lt;h2&gt;
  
  
  What operator should I use?
&lt;/h2&gt;

&lt;p&gt;Here is the common ground: &lt;strong&gt;Always use the &lt;code&gt;===&lt;/code&gt; (triple equal) operator.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is equivalent to &lt;code&gt;==&lt;/code&gt; in all other languages like &lt;code&gt;Python&lt;/code&gt;, &lt;code&gt;C/C++&lt;/code&gt;, &lt;code&gt;Ruby&lt;/code&gt;, or &lt;code&gt;Java&lt;/code&gt;. And in JS, there's always a good chance of using the &lt;code&gt;==&lt;/code&gt; (double equal) operator resulting in hidden bugs that will haunt the future developers as long as the software keeps running.&lt;/p&gt;

&lt;p&gt;Did I miss or forgot something? Is there a thing that you think it's not clear or can be better explained? Feel free to reach me at the comment section or by message and discuss it!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover Photo by &lt;a href="https://unsplash.com/@unitasphotography?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Charles "Duck" Unitas&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://bytearcher.com/articles/equality-comparison-operator-javascript/" rel="noopener noreferrer"&gt;Should I use === or == equality comparison operator in JavaScript?&lt;/a&gt; by Panu Pitkamaki&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using" rel="noopener noreferrer"&gt;Equality table for ==&lt;/a&gt; at MDN&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Modules vs Classes - How to Manage Privacy in JS</title>
      <dc:creator>Ilê Caian</dc:creator>
      <pubDate>Fri, 21 Feb 2020 17:05:53 +0000</pubDate>
      <link>https://dev.to/caiangums/modules-vs-classes-how-to-manage-privacy-in-js-3gch</link>
      <guid>https://dev.to/caiangums/modules-vs-classes-how-to-manage-privacy-in-js-3gch</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Object-Oriented Paradigm&lt;/strong&gt; made a huge shake in the way developers think and write code even if you don't like it or don't like its premises. This &lt;em&gt;not so new&lt;/em&gt; paradigm of encapsulating data and procedures in form of &lt;strong&gt;attributes&lt;/strong&gt; and &lt;strong&gt;methods&lt;/strong&gt; influenced a lot of recent languages even if some of them are not using it as its main paradigm.&lt;/p&gt;

&lt;p&gt;Languages like: C++, Java, Python, and even JavaScript are considered and declared languages that implement the Object-Oriented Paradigm (OOP). As will be discussed, JavaScript has its own way of dealing with Objects with some specificities. But first, there is a starting point that should be discussed: there's one essential fact in JavaScript that goes in the wrong direction of OOP: non-existence of &lt;strong&gt;encapsulation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There's a repo with some tests too! &lt;a href="https://github.com/caian-gums/modules-vs-classes-post" rel="noopener noreferrer"&gt;Check it out&lt;/a&gt;!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes and Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Classes&lt;/strong&gt; are definitions of data-types: what data they will &lt;strong&gt;store/hide&lt;/strong&gt; and how it &lt;strong&gt;should behave&lt;/strong&gt;. An instance of one Class will be able to execute functions as methods and store data as attributes. Those instances are the so-called &lt;strong&gt;Objects&lt;/strong&gt; that lives inside the runtime execution of a program.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy51exh459iveu5owao7m.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy51exh459iveu5owao7m.jpeg" alt="Photo by Dan Dimmock on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Dan Dimmock on Unsplash



&lt;p&gt;One of the important features in OOP is the fact that the Objects should have the ability to &lt;strong&gt;encapsulate&lt;/strong&gt;(hide) its data. That means that if someone tries accessing some information from the Object, it should &lt;strong&gt;not&lt;/strong&gt; be able to do it if the Class says so. Consider the following example:&lt;/p&gt;

&lt;p&gt;Let's say that Elon Musk 🧑‍💼 created an awesome &lt;code&gt;Trash Can&lt;/code&gt; that can perform 3 simple tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Throw &lt;em&gt;one&lt;/em&gt; 'junk' item into the Trash Can&lt;/li&gt;
&lt;li&gt;Clean all items inside the Trash Can, all at once&lt;/li&gt;
&lt;li&gt;One button that shows at the display if the Trash Can is fully empty or not&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The interface for that &lt;code&gt;TrashCan&lt;/code&gt; is something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;TrashCan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;isEmpty&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;As JavaScript has the &lt;code&gt;class&lt;/code&gt; keyword, it's possible to consider one implementation of this &lt;code&gt;Trash&lt;/code&gt; as the following&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="nc"&gt;TrashCan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&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;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&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;items&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;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&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;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;isEmpty&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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;elonTrashCan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TrashCan&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now the &lt;code&gt;elonTrashCan&lt;/code&gt; is empty and ready to start doing its work. But what happens with the execution of&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;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;paper ball&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;empty Starbucks cup of coffee&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;empty package of Cookies&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="s1"&gt;SpaceX secret project&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Elon Musk 🧑‍💼 will be mad at us that we broke his Trash Can&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.isEmpty()&lt;/code&gt; will return &lt;code&gt;false&lt;/code&gt;, because we defined &lt;code&gt;elonTrashCan.items&lt;/code&gt; with 1 item inside&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.items&lt;/code&gt; is not accessible, so the &lt;code&gt;elonTrashCan.isEmpty()&lt;/code&gt; call will returns &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The answer is &lt;strong&gt;option 2&lt;/strong&gt;. It is possible to access &lt;code&gt;items&lt;/code&gt; inside the Object instance even without explicit &lt;code&gt;items&lt;/code&gt; declaration outside &lt;code&gt;constructor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using that example and considering an ideal Object-Oriented language implementation, the execution of &lt;code&gt;elonTrashCan.items&lt;/code&gt; should result in an error of the program trying to access a &lt;strong&gt;private attribute&lt;/strong&gt;. In JavaScript, these calls are possible, valid ones, and results in &lt;strong&gt;no error&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, isn't it possible to create &lt;strong&gt;Privacy&lt;/strong&gt; in JavaScript? Is there a way of hiding data from outside the Object and exposing just &lt;code&gt;public&lt;/code&gt; data?&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fghpswvj6jgx2bz892rqf.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fghpswvj6jgx2bz892rqf.jpeg" alt="Photo by Folco Masi on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Folco Masi on Unsplash


&lt;h2&gt;
  
  
  Module Pattern
&lt;/h2&gt;

&lt;p&gt;The good news is there's one behavior in JavaScript that provides something related to &lt;strong&gt;privacy&lt;/strong&gt;: &lt;strong&gt;Closures&lt;/strong&gt;. There's this post written about Closures in case of interest &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/caiangums" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F328470%2F8c84fa62-89d7-4225-860d-cade31a4e607.jpeg" alt="caiangums"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/caiangums/closures-in-js-and-why-it-matters-pb9" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Closures in JS and Why It Matters&lt;/h2&gt;
      &lt;h3&gt;Ilê Caian ・ Feb 6 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Using &lt;strong&gt;Closures&lt;/strong&gt; for hiding variables and functions is a good approach for encapsulate data inside one instance and just expose the desired interface of it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But how do actually this works?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's create the same Elon Musk 🧑‍💼 &lt;code&gt;TrashCan&lt;/code&gt; Object writing a Function and returning just the public interface of it as the following 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;TrashCan&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&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;throwAway&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&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;clean&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isEmpty&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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="nx"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elonTrashCan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TrashCan&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for the new &lt;code&gt;elonTrashCan&lt;/code&gt; let's try to execute the same code as above&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;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;paper ball&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;empty Starbucks cup of coffee&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;throwAway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;empty package of Cookies&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="s1"&gt;SpaceX secret project&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Elon Musk 🧑‍💼 will be even madder at us that we broke his second Trash Can&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.isEmpty()&lt;/code&gt; will return &lt;code&gt;false&lt;/code&gt;, because we again defined &lt;code&gt;elonTrashCan.items&lt;/code&gt; with 1 item inside&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.items&lt;/code&gt; is not accessible, so the &lt;code&gt;elonTrashCan.isEmpty()&lt;/code&gt; call will returns &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Actually something very strange happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.isEmpty()&lt;/code&gt; returns &lt;code&gt;false&lt;/code&gt; because our internal &lt;code&gt;items&lt;/code&gt; are empty&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elonTrashCan.items&lt;/code&gt; has 1 item in it&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Finb6jyfktbdjb5tc70wk.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Finb6jyfktbdjb5tc70wk.jpeg" alt="Photo by Scott Gummerson on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Scott Gummerson on Unsplash



&lt;p&gt;Using this approach, it is possible to 'limit' the outside world on accessing just the desired interface and has hidden content inside it. On the other hand, JavaScript lets the definition of new properties in runtime execution, even with the same names used by its Closure.&lt;/p&gt;

&lt;p&gt;The code inside of the Closure will not depend on those new properties, as the original ones are stored inside that Closure, unaccessible. And, the original mission is now accomplished: &lt;strong&gt;Privacy&lt;/strong&gt;. The Module Pattern is viable for attributes and can be used for hiding methods too.&lt;/p&gt;

&lt;p&gt;For the side-effect with the creation of new properties, it's hardly suggested not to change the original interface and even make some tests before using those properties like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;elonTrashCan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;No exposed items!&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 exposed items!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;After some discussion about the Object-Oriented Paradigm and the Class implementation of JavaScript, maybe the JS Class is not the best choice for creating Objects with private data if you are not using a transpiler like Babel.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;Closures&lt;/strong&gt; and the &lt;strong&gt;Module Pattern&lt;/strong&gt; it is possible to accomplish &lt;strong&gt;Privacy&lt;/strong&gt; in JavaScript in a simple and reusable way. If the &lt;code&gt;class&lt;/code&gt; implementation is inevitable, consider using a transpiler or a more robust approach with some use of the Module Pattern. The use of transpilers is hardly incouraged!&lt;/p&gt;

&lt;p&gt;Even with some apparent losses like &lt;code&gt;inheritance&lt;/code&gt;, there are still valid ways of implementing that benefits even with &lt;strong&gt;Modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Did I miss something? Is there a thing that you think it's not clear? Feel free to reach me at the comment section or by message and discuss it!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover Photo by &lt;a href="https://unsplash.com/@ciprian?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Ciprian Boiciuc&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields" rel="noopener noreferrer"&gt;Article at MDN about Class fields(attributes) options&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/caiangums/closures-in-js-and-why-it-matters-pb9"&gt;Closures in JS and Why It Matters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;Babel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/caian-gums/modules-vs-classes-post" rel="noopener noreferrer"&gt;GitHub repo with tests&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Closures in JS and Why It Matters</title>
      <dc:creator>Ilê Caian</dc:creator>
      <pubDate>Thu, 06 Feb 2020 17:00:40 +0000</pubDate>
      <link>https://dev.to/caiangums/closures-in-js-and-why-it-matters-pb9</link>
      <guid>https://dev.to/caiangums/closures-in-js-and-why-it-matters-pb9</guid>
      <description>&lt;p&gt;One of the main features when a developer writes JavaScript code is maybe the one most unknown by them. Maybe this happens just because no one writes code thinking directly or even knowing that the reason their code doesn't break is related to this feature.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But what is that feature?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well... it is not a feature actually. It is a side-effect of how JavaScript is built and how it 'compile', run and executes. Let's dig in with an example.&lt;/p&gt;

&lt;p&gt;Running the following in the browser dev tools will result in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&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;getOlder&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;getOlder&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="s2"&gt;`I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;It breaks (🤷)&lt;/li&gt;
&lt;li&gt;Print &lt;code&gt;I am 14 years old.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Print &lt;code&gt;I am 15 years old.&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The correct answer is &lt;code&gt;2&lt;/code&gt;: &lt;code&gt;I am 14 years old.&lt;/code&gt;! But why?&lt;/p&gt;

&lt;h2&gt;
  
  
  Explaining the Execution
&lt;/h2&gt;

&lt;p&gt;There is a lot of important information about AST (Abstract Syntax Tree) and how JS was conceived that will not be the case of study here but for the reader (check the References!), think like this:&lt;/p&gt;

&lt;p&gt;When the Virtual Machine that runs inside your Browser (V8 in Chrome for example) executes the code it makes the naming resolution of each variable. This process of resolving the variables is required so while using a variable that is declared and defined it doesn't break your code. If the code tries to access some function or variable that is not properly defined yet it will output the famous:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Uncaught ReferenceError: yourVariable is not defined&lt;/code&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqzy1lwadofqm6l85m9w.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqzy1lwadofqm6l85m9w.jpeg" alt="Photo by Mark de Rooij on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Mark de Rooij on Unsplash



&lt;h2&gt;
  
  
  Resolving Variables by Hand
&lt;/h2&gt;

&lt;p&gt;If the result after naming resolution is accessible, the original code will be converted to something roughly similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;global__age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&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;global__getOlder&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;getOlder__age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;getOlder__age&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;global__getOlder&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="s2"&gt;`I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;global_age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; 'I am 14 years old.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it makes sense that the output is &lt;code&gt;I am 14 years old.&lt;/code&gt;, right? This prefix added is related to the &lt;strong&gt;Closure&lt;/strong&gt; of each variable and method when the naming resolution happens. As can be observed, there are 2 &lt;strong&gt;Closures&lt;/strong&gt; in this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;global&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getOlder&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can be noticed that the &lt;code&gt;getOlder&lt;/code&gt; Closure is inside the &lt;code&gt;global&lt;/code&gt; Closure but the variables inside the &lt;code&gt;getOlder()&lt;/code&gt; original function are only accessible inside those brackets.&lt;/p&gt;

&lt;p&gt;So, it makes much more sense saying that the &lt;code&gt;getOlder__age&lt;/code&gt; variable &lt;strong&gt;only exists&lt;/strong&gt; inside the &lt;code&gt;global__getOlder()&lt;/code&gt; function. A good example to validate is trying to log the variable from inside the function, outside 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;var&lt;/span&gt; &lt;span class="nx"&gt;global__age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&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;global__getOlder&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;getOlder__age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;getOlder__age&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;global__getOlder&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="s2"&gt;`I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;getOlder__age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; Error!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulted output is &lt;code&gt;Uncaught ReferenceError: getOlder__age is not defined&lt;/code&gt; and the reason is that there is no variable with the naming resolved to &lt;code&gt;global&lt;/code&gt; Closure valid for &lt;code&gt;getOlder__age&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  But what about the Scopes?
&lt;/h2&gt;

&lt;p&gt;In the creation of a function, a Closure is created the same way as a Scope. All variables and functions inside that both are accessible to all child functions and not outside of it (except if they are exposed like it'll be discussed ahead).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt; and &lt;strong&gt;Closure&lt;/strong&gt; are almost equal but the second one has some 'super-powers': Variables and functions created inside the &lt;strong&gt;Closure&lt;/strong&gt; and exposed will still work outside of it, even without the existence of &lt;strong&gt;Scope&lt;/strong&gt;. This is a very tight line between those two concepts.&lt;/p&gt;

&lt;p&gt;This is true even if those exposed items depends on other variables/functions inside the &lt;strong&gt;Closure&lt;/strong&gt; but are not exposed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closures vs. Scopes
&lt;/h2&gt;

&lt;p&gt;Using almost the same example as above with little changes in order to explain differences between these two concepts, the following code is a starting point&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;main&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&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;getOlder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&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="s2"&gt;`I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old now.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; 'I am 15 years old.'&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;getOlder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this example, the function &lt;code&gt;getOlder()&lt;/code&gt; will be called inside the &lt;code&gt;main()&lt;/code&gt; function and it'll print &lt;code&gt;I am 15 years old now.&lt;/code&gt;, correct? The variable &lt;code&gt;age&lt;/code&gt; is inside the &lt;code&gt;main&lt;/code&gt; scope and can be accessed by &lt;code&gt;getOlder()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Returning the &lt;code&gt;getOlder()&lt;/code&gt; function to the outside 'world' and executing it 3 times as the following example, what will be the result?&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;main&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&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;getOlder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&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="s2"&gt;`I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old now.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ???&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;getOlder&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;getOlder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;getOlder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ???&lt;/span&gt;
&lt;span class="nf"&gt;getOlder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ???&lt;/span&gt;
&lt;span class="nf"&gt;getOlder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Nothing. The code will break.&lt;/li&gt;
&lt;li&gt;3 times &lt;code&gt;I am 15 years old now.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The value of the &lt;code&gt;age&lt;/code&gt; variable will still increase from &lt;code&gt;15&lt;/code&gt;, to &lt;code&gt;16&lt;/code&gt;, and then to &lt;code&gt;17&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The correct answer is &lt;strong&gt;answer 3&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But why this happens?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every time a &lt;strong&gt;Closure&lt;/strong&gt; is created, all the variables and functions are stored inside its &lt;em&gt;state&lt;/em&gt;. Even after the end of execution of the &lt;code&gt;main()&lt;/code&gt; function, the respective &lt;strong&gt;Closure&lt;/strong&gt; &lt;em&gt;state&lt;/em&gt; is still alive storing variables and functions!&lt;/p&gt;

&lt;p&gt;Maybe the most awesome part of it is: the &lt;code&gt;age&lt;/code&gt; variable is lost inside that &lt;code&gt;main()&lt;/code&gt; &lt;strong&gt;Closure&lt;/strong&gt; and is not accessible outside of it! If the next part of the code tries accessing that &lt;code&gt;age&lt;/code&gt; variable, it'll result in the already discussed &lt;code&gt;Uncaught ReferenceError: age is not defined&lt;/code&gt; error, as this variable doesn't exist outside the &lt;code&gt;main()&lt;/code&gt; function!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwanmznk1hjsdsr6pckj0.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwanmznk1hjsdsr6pckj0.jpeg" alt="Photo by Maria Teneva on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by Maria Teneva on Unsplash



&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;Some awesome differences between &lt;strong&gt;Closure&lt;/strong&gt; and &lt;strong&gt;Scope&lt;/strong&gt; concepts were discussed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures always store &lt;em&gt;state&lt;/em&gt; about its variables and functions&lt;/li&gt;
&lt;li&gt;It is possible to expose some, all or none of those variables/functions by returning them at the end of the function that creates the Closure&lt;/li&gt;
&lt;li&gt;It is possible to even redefine some outside variables/functions inside the Closure with the same name and the Virtual Machine compiler will take care of it, avoiding errors in runtime and name collisions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is this article useful for you? Did I miss something while explaining? Please, let me know in the comment section or send me a message!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.sessionstack.com/how-javascript-works-parsing-abstract-syntax-trees-asts-5-tips-on-how-to-minimize-parse-time-abfcf7e8a0c8" rel="noopener noreferrer"&gt;Article about AST and how JS works from Lachezar Nickolov
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;MDN Article about Closures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/v8/v8" rel="noopener noreferrer"&gt;GitHub Mirror repo of V8&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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