<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Konstantin Meiklyar</title>
    <description>The latest articles on Forem by Konstantin Meiklyar (@shadowwarior5).</description>
    <link>https://forem.com/shadowwarior5</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%2F237914%2F166a5a61-74f8-480b-9269-ae67d844bb5b.jpg</url>
      <title>Forem: Konstantin Meiklyar</title>
      <link>https://forem.com/shadowwarior5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shadowwarior5"/>
    <language>en</language>
    <item>
      <title>Who needs Javascript symbols?</title>
      <dc:creator>Konstantin Meiklyar</dc:creator>
      <pubDate>Tue, 29 Sep 2020 04:28:52 +0000</pubDate>
      <link>https://forem.com/shadowwarior5/who-needs-javascript-symbols-4g6l</link>
      <guid>https://forem.com/shadowwarior5/who-needs-javascript-symbols-4g6l</guid>
      <description>&lt;p&gt;Cover image by &lt;a href="https://pixabay.com/users/fradellafra-2541818/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=1748102" rel="noopener noreferrer"&gt;Alexander Fradellafra&lt;/a&gt; from &lt;a href="https://pixabay.com/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=1748102" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tc39.es/ecma262/#sec-symbol-objects" rel="noopener noreferrer"&gt;Symbols&lt;/a&gt; are a less known primitive data type among &lt;code&gt;string, number, bigint, boolean and undefined&lt;/code&gt; of Javascript. They were added as part of ES6 specification which was a big facelifting of Javascript language and included a lot of new features.&lt;/p&gt;

&lt;h3&gt;Why do we need Symbols?&lt;/h3&gt;

&lt;p&gt;Symbols have 2 main use cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create hidden properties on objects that no other code (that has no reference to the symbol used) can access or overwrite. The convention of most built-in functions and libraries is to avoid referencing symbols declared on an object if there is no direct need to change them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;System symbols that are used to change default behaviors of object - for example, &lt;code&gt;Symbol.toPrimitive&lt;/code&gt; that is used to define object behavior during the conversion of an object to primitive or &lt;code&gt;Symbol.iterator&lt;/code&gt; that is used to set object behavior during the iteration.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Symbols basics&lt;/h3&gt;

&lt;p&gt;Symbols' syntax is very &lt;del&gt;symbol&lt;/del&gt; simple. We can create a new symbol by writing:&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;// mySymbol is a new created symbol&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mySymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&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;mySymbol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Symbol()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Symbol() function has an optional description field and can be used in this way:&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;// mySymbol is a new created symbol that now has a description&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mySymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;decription of my symbol&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;mySymbol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Symbol(decription of my symbol)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The description field is just a text that will be attached to the symbol - it's mostly used for debugging purposes.&lt;/p&gt;

&lt;p&gt;Every symbol returned from Symbol() function is unique, meaning that 2 symbols created using the function will never be equal (even they have same description passed to the function):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstSymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sameDescription&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;secondSymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sameDescription&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;firstSymbol&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;secondSymbol&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;Creating hidden properties in object&lt;/h3&gt;

&lt;p&gt;Now when we know how to create a new Symbol let's see how can use it to create a hidden property of an object.&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%2F07gkld0lvqikmrhnbs4r.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F07gkld0lvqikmrhnbs4r.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all - why would we do that? &lt;/p&gt;

&lt;p&gt;As a common use case, I can mention an example when our code is used by some third party. For example - we are writing an open-source library or a library that is going to be used by other teams of developers in our organization. We may want to add some "under-the-hood" properties to objects to be able to access them in our code - but at the same time, we want to guarantee that no other code will be able to access these properties.&lt;/p&gt;

&lt;p&gt;If we were using regular object properties declared by a string - the developers using our library can do that accidentally by iterating over object keys or creating a property with the same name and overwriting it.&lt;/p&gt;

&lt;p&gt;Symbols are here to help us.&lt;/p&gt;

&lt;p&gt;For example - let's say we have an object representing a rock star:&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;rockStar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Hetfield&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;band&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Metallica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Voice &amp;amp; Rythm guitar&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;Now we want to add a hidden property that will represent an internal id that we want to be exposed only in our code and avoid using it outside our internal 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;let&lt;/span&gt; &lt;span class="nx"&gt;idSymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id symbol used in rockStar object&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;rockStar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Hetfield&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;band&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Metallica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Voice &amp;amp; Rythm guitar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;idSymbol&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this-id-property-is-set-by-symbol&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;If we now want to access / change / delete the property set using the Symbol - we need to have the reference to the Symbol that was used to declare it. Without having it - we can't do that.&lt;/p&gt;

&lt;p&gt;Also - when iterating over the keys of an object - we will not get a reference to a property set using the Symbol:&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="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;rockStar&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// (3) ["name", "band", "role"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;for ... in ...&lt;/code&gt; loop will also ignore our symbol:&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;rockStar&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="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="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// name&lt;/span&gt;
&lt;span class="c1"&gt;// band&lt;/span&gt;
&lt;span class="c1"&gt;// role&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;Global symbol registry&lt;/h3&gt;

&lt;p&gt;What if in some cases we do want to add an ability to give access to properties that were defined using symbols? What if we need to share access to these properties between different modules of our application? &lt;/p&gt;

&lt;p&gt;This is where &lt;b&gt;Global symbol registry&lt;/b&gt; comes to help us. Think of it as a dictionary placed on a global level - accessible everywhere in our code where we can set or get Symbols by a specific key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Symbol.for&lt;/code&gt; is a syntax used to get Symbols from the global registry.&lt;/p&gt;

&lt;p&gt;Let's take the same example and re-write it using the global registry:&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;idSymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rockStarIdSymbol&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;rockStar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Hetfield&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;band&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Metallica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Voice &amp;amp; Rythm guitar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;idSymbol&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this-id-property-is-set-by-symbol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let idSymbol = Symbol.for('rockStarIdSymbol');&lt;/code&gt; will do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the global registry has a symbol related to the key that equals &lt;code&gt;rockStarIdSymbol&lt;/code&gt; and if there is one - return it&lt;/li&gt;
&lt;li&gt;If not - create a new symbol, store it in the registry and return it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This means, that if we will need to access our property in any other place in the code we can do 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;let&lt;/span&gt; &lt;span class="nx"&gt;newSymbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rockStarIdSymbol&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;rockStar&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newSymbol&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// "this-id-property-is-set-by-symbol"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result - worth mentioning that 2 different Symbols returned by the same key in the global registry will be equal:&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;symbol1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rockStarIdSymbol&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;symbol2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rockStarIdSymbol&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;symbol1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;symbol2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is also a way to check which key Symbol is related to in the global registry using &lt;code&gt;Symbol.keyFor&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;symbolForRockstar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rockStarIdSymbol&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="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keyFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symbolForRockstar&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//rockStarIdSymbol&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Symbol.keyFor&lt;/code&gt; is checking the global registry and finds the key for the symbol. If the symbol is not registered in the registry - &lt;code&gt;undefined&lt;/code&gt; will be returned.&lt;/p&gt;

&lt;h3&gt;System symbols&lt;/h3&gt;

&lt;p&gt;System symbols are symbols that can be used to customize the behavior of objects. The full list of system symbols can be found in &lt;a href="https://tc39.es/ecma262/#sec-well-known-symbols" rel="noopener noreferrer"&gt;latest language specification&lt;/a&gt;. Each system symbol gives access to some specification which behavior we can overwrite and customize.&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%2Fwazjx8utvwrkt2r8cqm5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwazjx8utvwrkt2r8cqm5.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As an example - let's see a usage one of the commonly used symbols - &lt;code&gt;Symbol.iterator&lt;/code&gt; that gives us access to the &lt;code&gt;iterator&lt;/code&gt; specification.&lt;/p&gt;

&lt;p&gt;Let's assume we want to write a Javascript class representing a music band. &lt;br&gt;
It will probably have a band's name, style, and a list of band members.&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;Band&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;members&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;style&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;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;members&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;And we will be able to create a new instance of the class by writing something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;metallicaBand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Band&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Metallica&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="s1"&gt;Heavy metal&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;James&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="s1"&gt;Lars&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="s1"&gt;Kirk&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="s1"&gt;Robert&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 if we'll want our users to be able to iterate of the instance of the class like it was an array and get the names of band members? This behavior is reused in a few libraries having arrays wrapped inside objects.&lt;/p&gt;

&lt;p&gt;Right now - if we will try to iterate over our object using a &lt;code&gt;for ... of&lt;/code&gt; loop - we will get an error saying &lt;code&gt;Uncaught TypeError: "metallicaBand" is not iterable&lt;/code&gt;. That's because our class definition has no instruction on how this iteration should be done. If we do want to enable iteration over it - we need to set the behavior and Symbol.iterator is a system symbol that we should use.&lt;/p&gt;

&lt;p&gt;Let's add it to our class definition:&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;Band&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;members&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;style&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;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;members&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="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="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BandIterator&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BandIterator&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// iterator implementation&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I will not dive into the actual implementation of the iterator - this can be a good topic for a separate post. But talking of Symbols - that's the use case we should know. Almost every native behavior can be changed and system symbols are the way to do it in javascript classes.&lt;/p&gt;

&lt;h3&gt;What else?&lt;/h3&gt;

&lt;p&gt;1) Well, technically properties on objects that are set using symbols are not 100% hidden. There are methods &lt;code&gt;Object.getOwnPropertySymbols(obj)&lt;/code&gt;, that returns all symbols set on an object and &lt;code&gt;Reflect.ownKeys(obj)&lt;/code&gt; that lists all properties of on object, including symbols. But the common convention is not to use these methods for listing, iteration, and any other generic actions performed on objects.&lt;/p&gt;

&lt;p&gt;2) Few times I saw code that had symbols used to declare enum values, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ColorEnum&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;freeze&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;RED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;RED&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
  &lt;span class="na"&gt;BLUE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;BLUE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not sure how good this practice is. Assuming that Symbols are not serializable and every attempt to stringify these values will just remove them from the object. &lt;/p&gt;

&lt;p&gt;When using symbols - use serialization carefully. And overall - avoid making deep copies using &lt;code&gt;JSON.parse(JSON.stringify(...))&lt;/code&gt;. This approach sometimes can cause hard to catch bugs that are causing sleepless nights!&lt;/p&gt;

&lt;p&gt;3) Function used for shallow object cloning - &lt;code&gt;Object.assign&lt;/code&gt; copies both symbols and regular string properties. This sounds like a proper design behavior.&lt;/p&gt;

&lt;p&gt;I think that's all you need to know about symbols to have the full picture. Did I forget anything?&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%2F9q711k1m1ley2jkcu89r.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9q711k1m1ley2jkcu89r.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy you made it until this point!&lt;/p&gt;

&lt;p&gt;Thanks for reading, as usual, I will appreciate any feedback.&lt;/p&gt;

&lt;p&gt;If you love Javascript as I do - visit &lt;a href="https://watcherapp.online/" rel="noopener noreferrer"&gt;https://watcherapp.online/&lt;/a&gt; - my side project having all javascript blog posts in one place, there is a ton of interesting stuff!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to make your functional React components perform better (with useCallback and memo)</title>
      <dc:creator>Konstantin Meiklyar</dc:creator>
      <pubDate>Sat, 02 Nov 2019 08:38:17 +0000</pubDate>
      <link>https://forem.com/shadowwarior5/how-to-make-your-functional-react-components-perform-better-using-usecallback-and-memo-2f9a</link>
      <guid>https://forem.com/shadowwarior5/how-to-make-your-functional-react-components-perform-better-using-usecallback-and-memo-2f9a</guid>
      <description>&lt;p&gt;These days I am getting into React after 4 years of AngularJs and Angular 2+. I really like the unopinionated nature of this library and the flexibility to decide on patterns and implementation details. &lt;/p&gt;

&lt;p&gt;With lots of choices you make - comes a big number of mistakes that can follow up these choices and more work we need to do as developers to ensure we are making our best to optimize our web applications and decide on the right patterns.&lt;/p&gt;

&lt;p&gt;In this post, I will cover some optimization techniques I learned that may be useful for you. &lt;/p&gt;

&lt;p&gt;This is my first React related post and I hope you'll like it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;React hooks&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;React hooks were a new addition in React 16.8. They give you an ability to use state without using javascript classes. These are very powerful and easy to use tools. I will not cover hooks basics here, you can learn them by yourself &lt;a href="https://reactjs.org/docs/hooks-reference.html"&gt;using official API reference&lt;/a&gt;, but I will use them in the demo application.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Demo application&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;For this post, I created a simple React application that we will change in a few ways to improve its performance. It's small and simple, but it's enough to describe the issues that we are going to solve in the next 5 minutes.&lt;/p&gt;

&lt;p&gt;It's a number selection component and you can see it here and explore the code:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/rNNYBGa?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;We have 2 components.&lt;br&gt;
&lt;b&gt;NumPad&lt;/b&gt; component represents a button having a number. As a props, it receives a &lt;b&gt;value&lt;/b&gt; - a number to display, &lt;b&gt;handleClick&lt;/b&gt; a callback function to handle clicks and &lt;b&gt;isSelected&lt;/b&gt; - a boolean indicating if the button should get selected class or not. In case button has a positive &lt;b&gt;isSelected&lt;/b&gt; - it will get a green color to indicate the selection.&lt;/p&gt;

&lt;p&gt;The second and the bigger component is &lt;b&gt;NumberSelection&lt;/b&gt;. This component has stateful logic and handles the &lt;b&gt;selectedValue&lt;/b&gt; variable using &lt;b&gt;useState&lt;/b&gt; hook. &lt;b&gt;NumberSelection&lt;/b&gt; renders 10 buttons in the loop and one of them that is equal to &lt;b&gt;selectedValue&lt;/b&gt;  is getting &lt;b&gt;isSelected&lt;/b&gt; class. It also represents the selected value inside the header element. Initial &lt;b&gt;selectedValue&lt;/b&gt; equals 5.&lt;/p&gt;

&lt;p&gt;Easy until now?&lt;/p&gt;

&lt;p&gt;Now let's dive into the problems this code has.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Functional components&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In 2019 functional components are considered a &lt;a href="https://programmingwithmosh.com/react/react-functional-components/"&gt;better practice&lt;/a&gt; than class components. With the help of hooks, they now allow creating stateful logic. &lt;/p&gt;

&lt;p&gt;There is 1 important thing that we need to remember about functional components - &lt;b&gt;they are functions&lt;/b&gt; that run on each render, meaning that every single thing inside them is invoked and every variable or function is declared again.&lt;/p&gt;

&lt;p&gt;Inside &lt;b&gt;NumberSelection&lt;/b&gt; we have a function called &lt;b&gt; changeSelection&lt;/b&gt;. Every time the state of the component is changed - component is rendered and the function is declared again and again. To show this in a more visual way, I changed our original code and added a set called &lt;b&gt;functionsSet&lt;/b&gt;. I will add our &lt;b&gt; changeSelection&lt;/b&gt; to that set every time the component is rendered.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/PooENzj?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
As you see, every time we change the selection - a new function reference is created and added to the set. This is definitely not so good for our memory and in some cases can cause memory leaks. But even a bigger problem - this function is passed to child components using props. This means that they will be rerendered too because of the change in the props passed inside.&lt;/p&gt;

&lt;p&gt;What can help us here as a hook called &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecallback"&gt;&lt;b&gt;useCallback&lt;/b&gt;&lt;/a&gt;. It will create a &lt;a href="https://en.wikipedia.org/wiki/Memoization"&gt;memoized&lt;/a&gt; version of the function that will be reused when the component will be rerendered.&lt;/p&gt;

&lt;p&gt;Let's rewrite &lt;b&gt;changeSelection&lt;/b&gt; declaration using &lt;b&gt;useCallback&lt;/b&gt; hook:&lt;br&gt;
See updated codepen example:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/oNNpxqz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
We are not redeclaring them anymore. Great success!&lt;/p&gt;

&lt;p&gt;One more thing to remember here - in many cases there is no need to keep stateless functions inside functional components. If your function is completely static - consider placing it outside, move it to utils library or just before the component is declared. This will keep it from being redeclared even without the usage of &lt;b&gt;useCallback&lt;/b&gt;. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;What are we really rendering?&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;There is a big number of &lt;a href="https://medium.com/@gethylgeorge/how-virtual-dom-and-diffing-works-in-react-6fc805f9f84e"&gt;great articles&lt;/a&gt; explaining in-depth how rendering works overall and in React applications. I will not dive into it here as it will take too much time. I assume that you know what React Virtual Dom is and how it works.&lt;/p&gt;

&lt;p&gt;How our application is rendered? What happens when we change the selection and the state of &lt;b&gt;NumberSelection&lt;/b&gt; is updated?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The whole application is rendered for the first time.&lt;/li&gt;
&lt;li&gt;We click the button represented by &lt;b&gt;NumPad&lt;/b&gt; component.&lt;/li&gt;
&lt;li&gt;This calls the callback function received from &lt;b&gt;NumberSelection&lt;/b&gt; component.&lt;/li&gt;
&lt;li&gt;State of &lt;b&gt;NumberSelection&lt;/b&gt; component is changed as a result of the callback.&lt;/li&gt;
&lt;li&gt;Once the state is changed - the component that was using the state and the tree of the child components are rebuilt in virtual DOM.&lt;/li&gt;
&lt;li&gt;Once the virtual DOM is ready - the diffing algorithm decides which parts of real DOM should be updated. Assuming we changed the selection - it updates 2 &lt;b&gt;NumPad&lt;/b&gt; components - one is deselected and one is now selected.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is nice to understand. &lt;br&gt;
I added a new log written when &lt;b&gt;NumPad&lt;/b&gt; is rendered.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/abbEZQV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you see - &lt;b&gt;NumPad&lt;/b&gt; is rerendered on each state change of parent component - meaning that on every click we are rendering all 10 buttons again. This is a rendering done for the virtual DOM - the component is not really updated in the real DOM, but we still invoke the whole render process. This is a lot of code running. Do we really need this? What if we have 100 buttons, 1000 buttons?&lt;/p&gt;

&lt;p&gt;On every selection change, we have only 2 &lt;b&gt;NumPad&lt;/b&gt; components that are actually changed - the one that was selected - it will get the selection now, and the old one that is now deselected. We don't really need to render all 10 &lt;b&gt;NumPads&lt;/b&gt; again.&lt;/p&gt;

&lt;p&gt;How can we know if a component should be rendered or not? Assuming that components are pure functions - we can just look at the props passed into. If they are changed - that's the sign we need to render the component. If not - we don't need to render them.&lt;/p&gt;

&lt;p&gt;This is a place we should consider using &lt;a href="https://reactjs.org/docs/react-api.html#reactmemo"&gt;&lt;b&gt;React.memo&lt;/b&gt;&lt;/a&gt;. It does exactly what we need. &lt;/p&gt;

&lt;p&gt;React API says:&lt;br&gt;
If your function component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.&lt;/p&gt;

&lt;p&gt;Sounds like our case! Let's wrap our &lt;b&gt;NumPad&lt;/b&gt; in React.memo:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/abbEBme?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now we see that only the 2 relevant components are rendered. Great success again!&lt;/p&gt;

&lt;p&gt;One thing that worth mentioning here - without using &lt;b&gt;useCallback&lt;/b&gt; hook from the previous example - this optimization was not working. Because without &lt;b&gt;useCallback&lt;/b&gt; hook new function is generated every time and is passed to all components, meaning the React.memo will detect change prop value and render the component.&lt;/p&gt;

&lt;p&gt;If &lt;b&gt;React.memo&lt;/b&gt; is so useful for us, why can't React just wrap all components by default?&lt;/p&gt;

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

&lt;p&gt;Remember that using &lt;b&gt;memo&lt;/b&gt; and &lt;b&gt;useCallback&lt;/b&gt; should not be done by default by you. Check the exact reference and think of each case separately as a misunderstanding of the way they should be used can cause side effects and bugs in your code.&lt;/p&gt;

&lt;p&gt;Hope you had fun!&lt;br&gt;
Will be happy to get your feedback on my post.&lt;/p&gt;

&lt;p&gt;Check out my &lt;a href="https://dev.to/shadowwarior5/10-superpowers-that-html5-gives-you-and-you-are-not-using-4ph1"&gt;previous post&lt;/a&gt; talking about interesting features in HTML5.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/meiklyar"&gt;Follow me on Twitter&lt;/a&gt; to get my latest updates!&lt;/p&gt;

&lt;p&gt;See you&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>10 superpowers HTML5 gives you (and you are not using)</title>
      <dc:creator>Konstantin Meiklyar</dc:creator>
      <pubDate>Sat, 19 Oct 2019 05:14:37 +0000</pubDate>
      <link>https://forem.com/shadowwarior5/10-superpowers-that-html5-gives-you-and-you-are-not-using-4ph1</link>
      <guid>https://forem.com/shadowwarior5/10-superpowers-that-html5-gives-you-and-you-are-not-using-4ph1</guid>
      <description>&lt;p&gt;The latest HTML spec has a big number of new features and the plans for future additions are constantly growing.&lt;/p&gt;

&lt;p&gt;I have a feeling that the lack of awareness and cross-browser support are causing developers to be less curious about the updates and the new specs. But I definitely agree that overcoming the compatibility issues to use some new feature natively can be overwhelming.&lt;/p&gt;

&lt;p&gt;In this post, I will list some nice features that may be out of your radar and can probably help you be more productive when writing your web applications. Some of them are widely supported by all browsers, some of them have very limited support.&lt;/p&gt;

&lt;h3&gt;1. &lt;i&gt;details&lt;/i&gt; and &lt;i&gt;summary&lt;/i&gt; tags&lt;/h3&gt;

&lt;p&gt;How many times did you create flags in your React and Angular code to keep the state of toggles, accordions, sliders in your components? Using &lt;b&gt;details&lt;/b&gt; and &lt;b&gt;summary&lt;/b&gt; tags you can handle this without writing a single line of javascript code.&lt;/p&gt;


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


&lt;p&gt;Check this codepen for the full example&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/zYYBJrj?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;b&gt;details&lt;/b&gt; and &lt;b&gt;summary&lt;/b&gt; tags are supported by most major browsers excepting Edge and IE but have a &lt;a href="https://github.com/javan/details-element-polyfill"&gt;polyfill&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;2. Native modals&lt;/h3&gt;

&lt;p&gt;Everyone knows the headache of creating dialog and modal components that are responsive enough and flexible enough to be used across large systems. Actually we do have a native solution for this.&lt;/p&gt;

&lt;p&gt;In this codepen you can see a simple example of how the &lt;b&gt;dialog&lt;/b&gt; component of HTML 5.2 can be used. &lt;br&gt;
&lt;i&gt;Warning: the example is not including polyfills and is working only on desktop or android Chrome&lt;/i&gt;&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/eYYzLQL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;dialog&lt;/b&gt; has some nice capabilities like &lt;b&gt;backdrop&lt;/b&gt; pseudo-class. Check &lt;a href="https://alligator.io/html/dialog-element/"&gt;this blog post&lt;/a&gt; that is showing really nice example of using it.&lt;/p&gt;

&lt;p&gt;Dialog element is supported by modern desktop and Android versions of Chrome. That's all. But you can use the &lt;a href="https://github.com/GoogleChrome/dialog-polyfill"&gt;polyfill that Google created&lt;/a&gt; to use in other browsers and prepare your code for the day it will be supported natively by all browsers.&lt;/p&gt;

&lt;h3&gt;3. &lt;i&gt;calc()&lt;/i&gt; &lt;/h3&gt;

&lt;p&gt;&lt;i&gt;calc()&lt;/i&gt; is a CSS way to do any math and you can replace any numeric value by using this function. Modern preprocessors have capabilities that allow using math functions but the superpower of &lt;i&gt;calc()&lt;/i&gt; is the ability to mix units - for example percents and pixels. &lt;br&gt;
&lt;i&gt;calc()&lt;/i&gt; can be very useful in places you used javascript to calculate container height or width in a dynamic way.&lt;/p&gt;

&lt;p&gt;If you write HTML and CSS you'll definitely need this function, don't skip learning it. Check &lt;a href="https://slicejack.com/how-to-use-css-calc-function/"&gt;this post&lt;/a&gt; for a bunch of nice examples.&lt;/p&gt;

&lt;h3&gt;4. &lt;i&gt;contenteditable&lt;/i&gt; attrubute&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;contenteditable&lt;/b&gt; attribute turns any element to editable in a magic way. This can be very useful when creating some custom user inputs like text processors, blog engines or anything else working with the text. Check this codepen to show a basic feature of this attribute.&lt;/p&gt;

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

&lt;p&gt;The big suprise - &lt;i&gt;contenteditable&lt;/i&gt; is supported by all major browsers, even by IE 6.&lt;/p&gt;

&lt;h3&gt;5. &lt;i&gt;mark&lt;/i&gt; tag&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;mark&lt;/b&gt; is a very simple and useful native tag. Since dev.io supports native markup I can just show it here. It just marks things.&lt;/p&gt;

&lt;p&gt;The syntax is very simple:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;b&gt;mark&lt;/b&gt; element is supported by major browsers, IE support is starting from IE 9+.&lt;/p&gt;

&lt;h3&gt;6. &lt;i&gt;@supports()&lt;/i&gt;
&lt;/h3&gt;

&lt;p&gt;Feature support in web development is a real pain point. CSS3 gave us a lot of nice tools to work with but we can never be sure that the cool new feature we are using is natively supported by different versions of different browsers on different operations systems on different devices. &lt;b&gt;@supports&lt;/b&gt; function was created to make things easier. Overall - CSS supports natural fallback mechanisms - if something is not recognized - it will just be ignored by the browser. I think this function can make code cleaner and make it easier to build those logical blocks.&lt;/p&gt;

&lt;p&gt;Example of such CSS block can look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It's important to say that this example is a bit utopic as &lt;i&gt;@supports&lt;/i&gt; itself is not supported by all browsers. So, in real life our code will look this way:&lt;/p&gt;


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


&lt;p&gt;&lt;b&gt;@supports()&lt;/b&gt; is supported by the modern version of all major browsers and not supported by IE.&lt;/p&gt;

&lt;h3&gt;7. &lt;i&gt;meter&lt;/i&gt; and &lt;i&gt;progress&lt;/i&gt; tags &lt;/h3&gt;

&lt;p&gt;&lt;b&gt;meter&lt;/b&gt; and &lt;b&gt;progress&lt;/b&gt; tags are the native way to build progress bars and measurement visualizations:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/YzzGoBv?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Excepting the way they look, the difference between these 2 tags is only in semantics. The &lt;a href="https://html.spec.whatwg.org/#the-progress-element"&gt;spec&lt;/a&gt; says:&lt;br&gt;
The &lt;b&gt;progress&lt;/b&gt; element represents the completion progress of a task.&lt;br&gt;
The &lt;b&gt;meter&lt;/b&gt; element represents a scalar measurement within a known range or a fractional value; for example disk usage, the relevance of a query result, or the fraction of a voting population to have selected a particular candidate.&lt;/p&gt;

&lt;p&gt;Needless to say that &lt;a href="https://css-tricks.com/html5-meter-element/"&gt;there is an post&lt;/a&gt; doing amazing things with &lt;i&gt;meter&lt;/i&gt; on CSS-tricks. &lt;br&gt;
This tag is supported by all major browsers including IE10+ and also have a &lt;a href="https://gist.github.com/strings28/667320"&gt;polyfil&lt;/a&gt; if needed.&lt;/p&gt;

&lt;h3&gt;8. Multicolumn elements&lt;/h3&gt;

&lt;p&gt;Multicolumn is a set of CSS attributes giving an easy way to split every HTML element to columns, very similar to what modern UI frameworks like Bootstrap offer.&lt;br&gt;
Check this example:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/pooEMpP?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Without changing any display or sizing attributes we can easily split everything by using 1 attribute. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Columns/Using_multi-column_layouts"&gt;The spec&lt;/a&gt; has some additional nice features that are worth checking. Multicolumn is a codename for a variety of CSS-attributes and their support depends on the exact subset. Check &lt;a href="https://caniuse.com/#feat=multicolumn"&gt;can-i-use&lt;/a&gt; and query attributes you need.&lt;/p&gt;

&lt;h3&gt;9. &lt;i&gt;picture&lt;/i&gt; tag&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;picture&lt;/b&gt; tag comes to solve the lack of ability to set different image sources and sizes for different media sources. It's a more flexible way to handle different versions of images for different resolutions. Check this example (this is a fork of &lt;a href="https://codepen.io/CharlesKiarie77/pen/xbrLYq"&gt;original codepen by CharlesKiarie&lt;/a&gt; and a credit goes to him).&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/erictheodore/embed/KKKgOYX?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
The image source is responsive to the media query as you can see if you will resize the screen to mobile dimensions.&lt;br&gt;
&lt;i&gt;picture&lt;/i&gt; tag is supported by all major browsers and has a &lt;a href="https://scottjehl.github.io/picturefill/"&gt;polyfill&lt;/a&gt; for older versions of IE.&lt;/p&gt;

&lt;h3&gt;10. Web Components&lt;/h3&gt;

&lt;p&gt;Taking the whole web and mobile applications development stack - HTML and JS are parts of it that were very innovated in the last 5 years. The number and the frequency of newborn frameworks are growing. &lt;b&gt;Web Components&lt;/b&gt; are the attempt to establish common conventions and patterns and it looks like they are here to stay, backed by top companies in the industry.&lt;br&gt;
If you missed it - you should &lt;a href="https://medium.com/@javier.ramos1/introduction-to-web-components-4c9bd528baee"&gt;definitely check&lt;/a&gt; and play with them.&lt;/p&gt;

&lt;p&gt;These were 10 HTML5 features that I found useful and worth sharing. I hope you're able to find something that was new to you and something that can be useful in your work. &lt;/p&gt;

&lt;p&gt;You still need to remember that you'll need to use them carefully as not all of them are supported by all browsers in a native way.&lt;/p&gt;

&lt;p&gt;Hope you had fun.&lt;br&gt;
Will be happy to get your feedback.&lt;/p&gt;

&lt;h2&gt;Follow me on &lt;a href="https://twitter.com/meiklyar"&gt;Twitter&lt;/a&gt; to get my latest updates!&lt;/h2&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>css</category>
    </item>
    <item>
      <title>Understanding Javascript Scope</title>
      <dc:creator>Konstantin Meiklyar</dc:creator>
      <pubDate>Mon, 14 Oct 2019 04:38:14 +0000</pubDate>
      <link>https://forem.com/shadowwarior5/understanding-javascript-scope-41m2</link>
      <guid>https://forem.com/shadowwarior5/understanding-javascript-scope-41m2</guid>
      <description>&lt;p&gt;In the last 4 years, I interviewed around 100 javascript developers, experienced and not, for senior and junior positions. Many of them were good at what they are doing but were still missing knowledge of some very basic concepts that the language has. I can explain this by the fact, that many developers come and stay in javascript from other languages without taking time to learn some basics.&lt;/p&gt;

&lt;p&gt;I did this mistake by myself several times when switching between languages and frameworks. It took me almost 10 years to realize how important is to learn the basics before diving into code.&lt;/p&gt;

&lt;p&gt;In this post, I will cover a very basic JS concept called Scope. I will try to include everything you need to know about it in 5 minutes of reading.&lt;/p&gt;

&lt;p&gt;This is my first tech blog post ever. I hope you'll have fun reading it and will be happy for any feedback.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Compilation and interpretation&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;One popular mistake about Javascript is thinking of Javascript as an "Interpreted language", that is not compiled. It's not exactly working this way. Javascript is not compiled to some "byte-code" similar to other languages, but there are definitely some processes happening before the execution of the code.&lt;/p&gt;

&lt;p&gt;Most of major javascript engines (for example V8 used by in NodeJS and Chrome browser) are using JIT (Just-in-time) compilers. This is a very interesting topic and can require an additional blog post. I recommend reading &lt;a href="https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design"&gt;this good old thread&lt;/a&gt; to get some understanding of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/javascript-essentials-why-you-should-know-how-the-engine-works-c2cc0d321553"&gt;This article&lt;/a&gt; has a good overview of the compiler/interpreter relationship in Javascript. &lt;/p&gt;

&lt;p&gt;I also recommend reading &lt;a href="https://hackernoon.com/javascript-v8-engine-explained-3f940148d4ef"&gt;this article&lt;/a&gt; explaining what V8 is and how it works.&lt;/p&gt;

&lt;p&gt;So, for making things easier for now - let's say that when running JS code we have 2 phases - compilation phase and execution phase.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Scope basics&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;JS Scope is born during the compilation phase. Scope - is just something that tells the running code where to look for things. During that phase variables and functions are added to the scope for every function/block or on the global level. They still don't have any values. Values are something that comes to the scene only during the execution phase. Let's take a look on a piece of code:&lt;/p&gt;


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


&lt;p&gt;Here we have 3 scopes: global scope and 2 function scopes - a scope of &lt;i&gt;go&lt;/i&gt; and scope of &lt;i&gt;go_again&lt;/i&gt;. Lets run this code and explore the scope using Chrome dev tools.&lt;/p&gt;

&lt;p&gt;1) Global scope has a variable called &lt;i&gt;bar&lt;/i&gt; and a function called &lt;i&gt;go&lt;/i&gt;. If we will stop during the execution process on the first line we can see the scope in &lt;i&gt;Sources&lt;/i&gt; tab in Google Chrome:&lt;/p&gt;

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

&lt;p&gt;This is one more evidence that right now we are in the global scope and on the right side we have a long list of functions and variables that exist there. You can find &lt;i&gt;bar&lt;/i&gt; variable there in the list. It's in the scope and it's still undefined.&lt;/p&gt;

&lt;p&gt;2) Scope of &lt;i&gt;go&lt;/i&gt; function. &lt;/p&gt;

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

&lt;p&gt;Here we can see that actual scope that is different from the global scope. It has 2 declarations inside: a variable called &lt;i&gt;foo&lt;/i&gt; and a function called &lt;i&gt;go_again&lt;/i&gt;. We also have &lt;i&gt;this&lt;/i&gt; inside this scope, I will ignore it for now as it's a topic for a whole separate post and I hope I will cover it there later.&lt;/p&gt;

&lt;p&gt;The important thing here is &lt;i&gt;goo&lt;/i&gt; variable that we see in the code but don't see in the scope of the function. The reason for that is the way it was declared. It has no initial declaration using var (or let and const that we will review later) keyword. In this case - during the compilation - it was not added to any scope. &lt;/p&gt;

&lt;p&gt;The following will happen during the execution phase: The interpreter will look for the variable in local (function) scope - as we can see - it's not there, after that it will try to find it in parent/grandparent scope until it will get to the global scope. Global scope in default conditions will declare this variable and add it to the list of other variables declared on it.&lt;/p&gt;

&lt;p&gt;3) Scope of &lt;i&gt;go_again&lt;/i&gt; function&lt;/p&gt;

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

&lt;p&gt;The thing worth mentioning here is that the value passed from parent function to &lt;i&gt;go_again&lt;/i&gt; is declared in the local scope as well. Simple variables in JS are passed by value - so in this case - the local variable &lt;i&gt;foo&lt;/i&gt; will be assigned to "xyz" while the original variable &lt;i&gt;foo&lt;/i&gt; inside &lt;i&gt;go&lt;/i&gt; function will keep the value of "def".&lt;/p&gt;

&lt;p&gt;&lt;b&gt;let vs. var&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Since ES6 we have a new (well, not so new already) way to declare variables using &lt;i&gt;let&lt;/i&gt; and &lt;i&gt;const&lt;/i&gt; keywords. Declaring variables with &lt;i&gt;let&lt;/i&gt; keyword has also effect on the scope - it creates a new type of scope - &lt;b&gt;Block scope&lt;/b&gt;. Let's review a short piece of code using &lt;i&gt;let&lt;/i&gt; and &lt;i&gt;const&lt;/i&gt; in variables declaration.&lt;/p&gt;


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


&lt;p&gt;As in the previous example - let's stop in debug mode during the execution and see how our scope looks like. Let's start with line #2:&lt;/p&gt;

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

&lt;p&gt;We are now inside &lt;i&gt;foo&lt;/i&gt; function and as we see inside our scope we don't have any variable excepting &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;z&lt;/i&gt; variables. This is because rest of the variables here are declared using &lt;i&gt;let&lt;/i&gt; keyword and exist only inside &lt;i&gt;block scope&lt;/i&gt; of the block they appear in. Let's take a few more steps in the program and stop inside &lt;i&gt;for&lt;/i&gt; loop:&lt;/p&gt;

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

&lt;p&gt;So here, we see 3 new scopes added inside our function scopes. These are scopes of 3 blocks that we have inside our code.&lt;/p&gt;

&lt;p&gt;One thing about the difference between &lt;i&gt;var&lt;/i&gt; and &lt;i&gt;let&lt;/i&gt; declarations are the way they are hoisted and initialized. You can learn more details from &lt;a href="https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-not-hoisted-in-es6"&gt;this post&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Classic block scope problem&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Since the release of ES6, there are few technical questions asked during the interviews that became classic problems related to function/block scope in JS. Let's review one of them briefly.&lt;/p&gt;


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


&lt;p&gt;Having this code - what is the output of it? And how can we fix it to print the array in the right way?&lt;/p&gt;

&lt;p&gt;So, the output of this loop will be &lt;i&gt;5 5 5 5 5&lt;/i&gt;. At the point where we will print the variable &lt;i&gt;i&lt;/i&gt; that is set on the global level and not on block-level it will be changed to 5 globally.&lt;br&gt;
You definetely understand at this point - that changing &lt;i&gt;var count&lt;/i&gt; to &lt;i&gt;let count&lt;/i&gt; inside the definition of &lt;i&gt;for&lt;/i&gt; loop will change the scope of the variable to block level and will cause the loop to be printed in a way it was supposed to be - &lt;i&gt;0 1 2 3 4&lt;i&gt;.&lt;/i&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;So, what we've learned?&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scope in JS is a place where declared variables and functions live.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The scope can be easily examined using Google Chrome dev tools. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Global variables are attached to the global scope and are accessible everywhere in the code. This is a bad practice and should be avoided, excepting some specific cases - mostly because for these variables the garbage collection is never done and can cause collisions with locally declared variables with the same names.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables declared as &lt;i&gt;var&lt;/i&gt; are added to &lt;i&gt;function scope&lt;/i&gt; and are accessible everywhere inside that function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables declared as &lt;i&gt;let&lt;/i&gt; and &lt;i&gt;const&lt;/i&gt; are added to block scope and are accessible everywhere inside that block.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all for now!&lt;br&gt;
Like and share if this post was useful for you.&lt;br&gt;
Leave feedback if you have any.&lt;/p&gt;

&lt;p&gt;And never lose your scope! :)&lt;/p&gt;

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