<?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: Hihi</title>
    <description>The latest articles on DEV Community by Hihi (@ngquan).</description>
    <link>https://dev.to/ngquan</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%2F233246%2F0d325a5e-3524-4d8c-9543-c050682381b7.png</url>
      <title>DEV Community: Hihi</title>
      <link>https://dev.to/ngquan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ngquan"/>
    <language>en</language>
    <item>
      <title>DNS resolution - How does it work basically?</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Sat, 11 Nov 2023 06:32:49 +0000</pubDate>
      <link>https://dev.to/ngquan/dns-resolution-how-does-it-work-basically-4ho5</link>
      <guid>https://dev.to/ngquan/dns-resolution-how-does-it-work-basically-4ho5</guid>
      <description>&lt;p&gt;The process of typing url to browser bar and then enter, wait for a little bit and you get your webpage in front of you seems like to be simple but there's a lot of communication and works between computers under the hood, and more amazingly, these complex stuffs are done in a time counted by just milliseconds!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/1RpVCiRh5zzaJMa5sA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/1RpVCiRh5zzaJMa5sA/giphy.gif" alt="high-speed-internet" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you type in the url, for example &lt;code&gt;gg.com&lt;/code&gt; to browser and go, the browser first will ask the DNS resolver (usually your ISP) if he saved a cached IP address &lt;code&gt;gg.com&lt;/code&gt; or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If he say yes, he will return the IP address to the browser and then the browser can go to that address to fetch assets of &lt;code&gt;gg.com&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If not, he has to proceed on finding the url's IP address&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That process of IP finding is called &lt;strong&gt;DNS resolution&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Find the IP address
&lt;/h3&gt;

&lt;p&gt;Overall, the DNS resolver (now I will call it &lt;code&gt;resolver&lt;/code&gt;) has to scope down the zone of searching: he will search for where is containing all the &lt;code&gt;.com&lt;/code&gt; domains among others (&lt;code&gt;.net&lt;/code&gt;, &lt;code&gt;.co&lt;/code&gt;,...) first, then go to that &lt;code&gt;.com&lt;/code&gt; domains container and search for &lt;code&gt;gg.com&lt;/code&gt;'s address and finally go to that final address to retrieve assets.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The resolver asks the first information bar (&lt;strong&gt;rootname server&lt;/strong&gt;) where contains all the &lt;code&gt;.com&lt;/code&gt; domains, the RS returns the IP address of the &lt;code&gt;.com&lt;/code&gt; container - this container's address is called Top Level Domain server.&lt;/li&gt;
&lt;li&gt;The resolver goes to the &lt;strong&gt;Top Level Domain&lt;/strong&gt; server address, asking for the address of &lt;code&gt;gg.com&lt;/code&gt;. The TLD server returns the IP address where containing the &lt;code&gt;gg.com&lt;/code&gt; domain - This address is called authoritative name server, the information bar knows where &lt;code&gt;gg.com&lt;/code&gt; is living.&lt;/li&gt;
&lt;li&gt;The resolver go to the &lt;strong&gt;Authoritative name server&lt;/strong&gt;, asking the exact address of &lt;code&gt;gg.com&lt;/code&gt;, the ANS return the exact IP address of &lt;code&gt;gg.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Having the final IP address of &lt;code&gt;gg.com&lt;/code&gt;, the resolver saves that IP address for a limited time (cached), then tell the browser "hey, this is where &lt;code&gt;gg.com&lt;/code&gt; lives, go here to find him".&lt;/li&gt;
&lt;li&gt;Browser go to that IP address and retrieve &lt;code&gt;gg.com&lt;/code&gt; assets.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iXw-Yl1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gs4ppl1nsmpdbc76m2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iXw-Yl1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gs4ppl1nsmpdbc76m2c.png" alt="DNS resolution diagram" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next time when we want to access &lt;code&gt;gg.com&lt;/code&gt;, the resolver will check if there's a time-valid cached IP address of &lt;code&gt;gg.com&lt;/code&gt; or not, if the cached IP is not obsolete the resolver will return that cached IP to browser, or else it will start the DNS resolution process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some takes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;DNS servers communicate with address in IPs type, not human url.&lt;/li&gt;
&lt;li&gt;An IP address points to a specific computer containing the url's assets (HTML, CSS, JS files).&lt;/li&gt;
&lt;li&gt;A certain url can have multiple IP address in ANS, each of these IP address is called &lt;strong&gt;A record&lt;/strong&gt;. When requested, the ANS will choose 1 of these &lt;code&gt;A records&lt;/code&gt; and return to resolver. By doing this the loads among servers containing assets of the url can be shared and balanced and avoid failures when one or more of the servers are down.
For example: beside &lt;code&gt;9.9.9.9&lt;/code&gt;, the IP address of &lt;code&gt;gg.com&lt;/code&gt; can be &lt;code&gt;8.8.8.8&lt;/code&gt; or &lt;code&gt;7.7.7.7&lt;/code&gt;. We can go to one of them to get the same assets for &lt;code&gt;gg.com&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Name_server"&gt;https://en.wikipedia.org/wiki/Name_server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cloudflare.com/learning/dns/dns-server-types/"&gt;https://www.cloudflare.com/learning/dns/dns-server-types/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://umbrella.cisco.com/blog/what-is-the-difference-between-authoritative-and-recursive-dns-nameservers"&gt;https://umbrella.cisco.com/blog/what-is-the-difference-between-authoritative-and-recursive-dns-nameservers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>network</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript - interpretation and JIT</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Sun, 09 Jul 2023 10:54:23 +0000</pubDate>
      <link>https://dev.to/ngquan/javascript-interpretation-and-jit-4nc7</link>
      <guid>https://dev.to/ngquan/javascript-interpretation-and-jit-4nc7</guid>
      <description>&lt;p&gt;We hear about Javascript as it is an interpreted language and the JIT engine made up of it. But did you know that Javascript is also considered either a compiled or an interpreted language thanks to the JIT engine?&lt;/p&gt;

&lt;h3&gt;
  
  
  What is JIT?
&lt;/h3&gt;

&lt;p&gt;JIT stands for Just-in-time, it is a dynamic compilation which is being done before the code execution. In the normal process of translate Javascript into machine readable code, the interpreter translates the human code (Javascript) &lt;strong&gt;line by line&lt;/strong&gt; and execute that code (bytecode - 0 and 1) immediately, so there's no room for optimization, just translate and run. The JIT leans in this process to do the optimization between the translation and execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  How JIT helps
&lt;/h3&gt;

&lt;p&gt;The JIT engine plays a vital role in the optimization. It will do assumptions and estimations in parts of our code, then decide what code can be sent directly to execution and which one should be hold to optimize.&lt;br&gt;
The JIT holds some assumptions for our code when it's doing the optimization. With some code is run frequently, the engine will capture the code, make assumption and optimized version for each of its assumption. If the assumption is correct, the optimized code will be pushed to the compiled code and ready for execution, if it's not, the wrongly-optimized code will be trashed out - this is called &lt;strong&gt;deoptimization&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/"&gt;Elaborate more on how JIT works - A crash course in just-in-time (JIT) compilers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The process of op-deop may cause the engine to slow down the run time of the code due to making to many assumptions by the JIT. The browsers solve this problem by limiting how many time the op-deop should occur, if it reaches the limit, the op-deop should be stopped trying and use the baseline code.&lt;/p&gt;

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

&lt;p&gt;We studied what is JIT and how it helps transform Javascript in to a well-balanced between compiled and interpreted language. Each browser or server engine has different ways of implementing JIT but in general, they have the same core idea which includes the op/deoptimization with the baseline compiler and optimization compiler.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascript.plainenglish.io/why-javascript-is-called-interpreted-or-jit-just-in-time-compiled-c8cc490682bd"&gt;Why JavaScript is called Interpreted or JIT(Just In Time) Compiled&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.bitsrc.io/the-jit-in-javascript-just-in-time-compiler-798b66e44143"&gt;The JIT in JavaScript: Just In Time Compiler&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript compile time and runtime</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Sat, 08 Jul 2023 07:50:42 +0000</pubDate>
      <link>https://dev.to/ngquan/javascripts-compile-time-and-runtime-2eip</link>
      <guid>https://dev.to/ngquan/javascripts-compile-time-and-runtime-2eip</guid>
      <description>&lt;p&gt;In this article I will give a brief about Javascript compilation and runtime. I recommend you to elaborate to these terms to have a deep knowledge of how Javascript is run under the hood.&lt;/p&gt;

&lt;h4&gt;
  
  
  Compile time:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The phase when Javascript is parsed into machine code - set of instructions for the machine to do as follow.&lt;/li&gt;
&lt;li&gt;The compilation process has 3 phases: - Tokenizing/Lexing - Parsing - Code-Generation.&lt;/li&gt;
&lt;li&gt;All the checking and optimization is done in this compile time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Runtime:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;The phase when the code is done compiled and is invoked to execute.&lt;/li&gt;
&lt;li&gt;In Javascript specifically, the runtime system facilitates storing functions, variables, and managing memory by using data structures such as queues, heaps, and stacks. All these engines are provided by Javascript v8 if you run code in Chrome.&lt;/li&gt;
&lt;li&gt;Ex: NodeJS and Browser runtime provide all the things need to run our Javascript code. The run kit is comprise of Event loop, callback queue and Javascript engines (heap and callstack).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;=&amp;gt; When we type and run the JS code in the console browser, the browser will firstly &lt;strong&gt;compile&lt;/strong&gt; the code into machine code, and then run the byte code (compiled code) in the environment (JS v8) - which means this in &lt;strong&gt;runtime&lt;/strong&gt;. &lt;a href="https://cabulous.medium.com/how-v8-javascript-engine-works-5393832d80a7"&gt;How JS v8 works&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ref:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/@osmanakar_65575/javascript-how-the-engine-compiles-6df6d5c6439c"&gt;Javascript - How the engine compiles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Compile"&gt;MDN - Compile&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://algodaily.com/lessons/introduction-to-js-engines-and-runtimes"&gt;Introduction to JS Engines and Runtimes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Javascript Object - Prototype</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Sat, 17 Jun 2023 06:08:10 +0000</pubDate>
      <link>https://dev.to/ngquan/javascript-object-prototype-541d</link>
      <guid>https://dev.to/ngquan/javascript-object-prototype-541d</guid>
      <description>&lt;p&gt;Firstly, keep in mind that in the Javascript world there are only 2 types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primitives: number, string, boolean, undefined and null.&lt;/li&gt;
&lt;li&gt;Objects: Objects, functions and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects"&gt;its standard built-in Objects&lt;/a&gt; (all these built-in are Object-extended);&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we can say that Javascript is an Object-based language.&lt;/p&gt;

&lt;p&gt;When creating an object, we usually do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myOb&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="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&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;This will create an object with the default constructor of &lt;code&gt;Object&lt;/code&gt; in Js. To check this we can simply &lt;code&gt;console.log(myOb.__proto__)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C5Xfyc5O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs3pkxjpcxfwqlbxnbg6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C5Xfyc5O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs3pkxjpcxfwqlbxnbg6.png" alt="Object construction" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what is &lt;code&gt;__proto__&lt;/code&gt; and why all Js Objects have that property? We usually hear the term &lt;code&gt;Prototype&lt;/code&gt; but what it actually is?&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototype
&lt;/h2&gt;

&lt;p&gt;The concept of prototype relating to the inheritance of Javascript's objects and its extends. For example, &lt;code&gt;Array&lt;/code&gt; is an extend of &lt;code&gt;Object&lt;/code&gt;, so it also inherited some property of &lt;code&gt;Object&lt;/code&gt; and have some built-in methods like &lt;code&gt;push, pop, shift, unshift,...&lt;/code&gt;. The &lt;code&gt;Object&lt;/code&gt; is not having these &lt;code&gt;Array&lt;/code&gt;'s built-in methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ar&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="nx"&gt;log&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;ar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'object', not 'array'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, each &lt;strong&gt;instance&lt;/strong&gt; of &lt;code&gt;Object&lt;/code&gt; or &lt;code&gt;Object-extends&lt;/code&gt; has a special property called &lt;code&gt;__proto__&lt;/code&gt; which will drive us to the inheritance information of the instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__prototype__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// [constructor: ƒ, at: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, …]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that the constructor of Array, all the built-in methods that inherit from the &lt;code&gt;Array&lt;/code&gt; standard object. However, &lt;code&gt;__proto__&lt;/code&gt; is also an object, and like I said before every object has the &lt;code&gt;__proto__&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see the construction and built-in methods of the &lt;code&gt;__proto__&lt;/code&gt;, it is an object itself, so what if we go deeper?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Haaa! we reached the end of inheritance chain, the Object itself is the final node. We can say &lt;code&gt;Object&lt;/code&gt; is the final boss in Javascript. I recommend that you should copy &amp;amp; run all above snippet in your browser's console and see the result and do the exploration yourself.&lt;/p&gt;

&lt;p&gt;That inheritance chain is usually called &lt;strong&gt;Prototype chain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the upcoming post, I will show you the application of Prototype and some syntactic sugar syntax relating to Object. Stay tuned!&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics"&gt;JavaScript object basics - MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes"&gt;Javascript Object Prototype - MDN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Javascript event loop - approach the first meet</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Sun, 30 Apr 2023 09:13:47 +0000</pubDate>
      <link>https://dev.to/ngquan/javascript-event-loop-47p8</link>
      <guid>https://dev.to/ngquan/javascript-event-loop-47p8</guid>
      <description>&lt;p&gt;Let's take a context in a browser where we can execute JS directly in the &lt;code&gt;console&lt;/code&gt;. Browsers provide JS runtime engine so that we can run the code in the console. Beside the JS engine, browsers also have alot of other built-in tools like HTML parsers, networking, storages,... And the Web API is one of the pieces in the browsers tools.&lt;br&gt;
To this point we can note that &lt;strong&gt;JS runtime&lt;/strong&gt; and &lt;strong&gt;Web API&lt;/strong&gt; is 2 separately tools in the browser. This is important to acknowledge before we continued to the Event loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mx96evYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu8ftubgnzc0cbapdoo3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mx96evYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu8ftubgnzc0cbapdoo3.png" alt="Image description" width="711" height="433"&gt;&lt;/a&gt;&lt;br&gt;
Now consider this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;setTimeout&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Time is up!&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we execute the snippet, the log now shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start
end
Time is up!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why shouldn't the log be this order &lt;code&gt;start - time is up! - end&lt;/code&gt; although we set the timer to &lt;code&gt;0&lt;/code&gt; in &lt;code&gt;setTimeout&lt;/code&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  Debug the event loop
&lt;/h3&gt;

&lt;p&gt;Firstly, keep in mind that JS doesn't provide the &lt;code&gt;setTimeout&lt;/code&gt; function. It's part of the &lt;strong&gt;Web API&lt;/strong&gt; that provided by browser. &lt;strong&gt;So it has nothing to do with the Javascript things&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yJ0IACmH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kim9igv8jr2hk6si8zyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yJ0IACmH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kim9igv8jr2hk6si8zyf.png" alt="Image description" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the code run, the functions are gradually added to the Callstack and then executed immediately.&lt;br&gt;
While the &lt;code&gt;console.log()&lt;/code&gt; functions are freshly executed right away and log the texts, the &lt;code&gt;setTimeout&lt;/code&gt; is a special function from Web API. When it's called, the Web API will get the hands in: it holds the callback passed in &lt;code&gt;setTimeout&lt;/code&gt; for a &lt;code&gt;t&lt;/code&gt; second.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RWOzAQJN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0l9bi4zkb96iog743di.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RWOzAQJN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0l9bi4zkb96iog743di.png" alt="Image description" width="750" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;t&lt;/code&gt; second, the callback is pushed to the &lt;strong&gt;callback queue&lt;/strong&gt;, waiting in it to be executed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aBDfFsBC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6x2ja5unerg7qugzin2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aBDfFsBC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6x2ja5unerg7qugzin2.png" alt="Image description" width="796" height="621"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Event loop is constantly checking the callstack: if the callstack is empty, the callback queue will dequeue it, bringing the instance waiting in the queue to the callstack and then execute it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C3V9UMx1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6loe9npw0mozds4zdmnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C3V9UMx1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6loe9npw0mozds4zdmnf.png" alt="Image description" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, the &lt;code&gt;console.log('Time is up!')&lt;/code&gt; is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c45QIJNc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlxiyzusuql71xnyxovv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c45QIJNc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlxiyzusuql71xnyxovv.png" alt="Image description" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;From the snippet above, we can see that if we use the special functions (&lt;code&gt;setTimeout, setTimeInterval&lt;/code&gt;) from the Web API kit, the comprised instances (callbacks) in these function must go to the Web API, then go to the waiting queue from the callback queue, waiting for the callstack to be empty and then dequeue to the callstack for execution.&lt;/li&gt;
&lt;li&gt;In the meanwhile, the JS native functions (&lt;code&gt;console&lt;/code&gt;) will take place in the callstack, no waiting for the &lt;code&gt;setTimeout&lt;/code&gt;. This is called &lt;strong&gt;Asynchronous/Non-blocking&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scope &amp; Closure in Javascript - Army of functions challenge</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Thu, 20 Apr 2023 05:09:41 +0000</pubDate>
      <link>https://dev.to/ngquan/scope-closure-in-javascript-army-of-functions-challenge-dmg</link>
      <guid>https://dev.to/ngquan/scope-closure-in-javascript-army-of-functions-challenge-dmg</guid>
      <description>&lt;p&gt;While researching about scope and closure, I came across &lt;a href="https://javascript.info/task/make-army"&gt;this challenge from javascrip.info&lt;/a&gt;, The question is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;makeArmy&lt;/span&gt;&lt;span class="p"&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;shooters&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&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;shooter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// create a shooter function,&lt;/span&gt;
            &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// that should show its number&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="nx"&gt;shooters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shooter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// and add it to the array&lt;/span&gt;
        &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;shooters&lt;/span&gt;&lt;span class="p"&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;army&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;makeArmy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// all shooters show 10 instead of their numbers 0, 1, 2, 3...&lt;/span&gt;
&lt;span class="nx"&gt;army&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// 10 from the shooter number 0&lt;/span&gt;
&lt;span class="nx"&gt;army&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// 10 from the shooter number 1&lt;/span&gt;
&lt;span class="nx"&gt;army&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// 10 ...and so on.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, what is really happening?&lt;/p&gt;

&lt;p&gt;As we can see, each iteration of the &lt;code&gt;while&lt;/code&gt; loop we initialized and assigned the &lt;code&gt;shooter&lt;/code&gt; function and then store them in the &lt;code&gt;shooters&lt;/code&gt; array. Each instance has the reference to the &lt;code&gt;i&lt;/code&gt; variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's dive in the action of function.
&lt;/h3&gt;

&lt;p&gt;When we invoke &lt;code&gt;army[0]()&lt;/code&gt;, it firstly search the &lt;code&gt;i&lt;/code&gt; variable in its local memory (which is the function's inner scope). When it cannot find &lt;code&gt;i&lt;/code&gt; in its local scope, it continues to populate to the outside scope and searching for &lt;code&gt;i&lt;/code&gt;, the current scope is now in the &lt;code&gt;while&lt;/code&gt; loop (inner the &lt;code&gt;{...}&lt;/code&gt;). In this scope there's still not any &lt;code&gt;i&lt;/code&gt;. It continues to search in the broader scope, which is the &lt;code&gt;makeArmy&lt;/code&gt; 's environment, now there's one &lt;code&gt;i&lt;/code&gt; holding the value of &lt;code&gt;10&lt;/code&gt;, and yet the &lt;code&gt;10&lt;/code&gt; is logged in the &lt;code&gt;alert(i)&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3JtXbIGi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o2l0dbx6zie63fawkpa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3JtXbIGi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o2l0dbx6zie63fawkpa.png" alt="Image description" width="685" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to make the &lt;code&gt;i&lt;/code&gt; unique value as we wanted?
&lt;/h3&gt;

&lt;p&gt;Simply we will create a copied local variable of &lt;code&gt;i&lt;/code&gt; in the iteration scope (&lt;code&gt;while&lt;/code&gt; loop's scope)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;makeArmy&lt;/span&gt;&lt;span class="p"&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;shooters&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&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;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;👈&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;shooter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="err"&gt;👈&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="nx"&gt;shooters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shooter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;shooters&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;j&lt;/code&gt; is now living in the iteration scope and each iteration it holds a new value of &lt;code&gt;i&lt;/code&gt; incremented. And each instance of &lt;code&gt;shooter&lt;/code&gt; has its own unique &lt;code&gt;j&lt;/code&gt; but not using the outer &lt;code&gt;i&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r0FMxUsI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ykxxftz8wdhfjt7us1w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r0FMxUsI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ykxxftz8wdhfjt7us1w.png" alt="Image description" width="731" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;j&lt;/code&gt; is encapsulated in each instance of &lt;code&gt;shooter&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Be careful when using the shared variable between instances of function.&lt;/li&gt;
&lt;li&gt;Each block scope has it own scope
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;scopedContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello scope&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scopedContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// ReferenceError: scopedContent is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Javascript scope and closure</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Wed, 12 Apr 2023 05:06:27 +0000</pubDate>
      <link>https://dev.to/ngquan/javascript-scope-and-closure-4lca</link>
      <guid>https://dev.to/ngquan/javascript-scope-and-closure-4lca</guid>
      <description>&lt;p&gt;To understand closure, let's take a look at the term &lt;strong&gt;Lexical scope&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Lexical scope of a function comprises of &lt;strong&gt;inner scope&lt;/strong&gt; of function and &lt;strong&gt;ability to access its nearest surrounding environment&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The access ability is a hidden feature that cannot be access normally as object in JS (some says it's optimization engine from V8)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&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;counter&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="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;counter&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// - the inc()'s lexical scope is its inner scope &lt;/span&gt;
&lt;span class="c1"&gt;// (within the {...}) and the ability to &lt;/span&gt;
&lt;span class="c1"&gt;// bound out and search for the counter;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Once the &lt;code&gt;inc()&lt;/code&gt; is returned to an instance, it encapsulates the &lt;code&gt;counter&lt;/code&gt; value and bring it along - to a permanant memory, which means any of it instance from the same invocation can access to &lt;code&gt;counter&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;newFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 1&lt;/span&gt;
&lt;span class="nx"&gt;newFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Different invocations can not share the same reference to &lt;code&gt;counter&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;newFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 1&lt;/span&gt;
&lt;span class="nx"&gt;newFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 2&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;anotherFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;anotherFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 1, not 3&lt;/span&gt;
&lt;span class="nx"&gt;anotherFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// counter = 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;anotherFunc&lt;/code&gt; creates new Execution context, and the &lt;code&gt;counter&lt;/code&gt; is set to &lt;code&gt;0&lt;/code&gt; in that new EC.&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;When a function is invoked, it firstly search for its inner local memory (inner scope), then if not found it will populate to its encapsulated memory, then if not found, it gradually bounce back to the broader nearest scope.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---DqY9wtF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0exi3citbeqd995kk1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---DqY9wtF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0exi3citbeqd995kk1z.png" alt="Function lexical ability" width="800" height="171"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Source: &lt;a href="https://javascript.info/closure"&gt;Closure - Javascript.info&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  In short:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Lexical scope: the inner scope and &lt;strong&gt;ability to access surrounding scopes (this is called Closure)&lt;/strong&gt; ;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascript.info/closure"&gt;Variable scope, closure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;JS the hard parts - Frontend master course&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Lazy Initialization in React</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Wed, 12 Apr 2023 03:45:47 +0000</pubDate>
      <link>https://dev.to/ngquan/lazy-initialization-in-react-56me</link>
      <guid>https://dev.to/ngquan/lazy-initialization-in-react-56me</guid>
      <description>&lt;p&gt;I read about lazy init lately when enrolled in FrontendMaster course from Brian Holt. It is said that lazy init is used when you want to perform some &lt;strong&gt;heavy calculation actions rather than primitive values&lt;/strong&gt; in the &lt;code&gt;useState&lt;/code&gt; hook. For example calculating some complex formula, creating some DOM elements,...&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentDOM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrentDOM&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&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;Keep in mind that &lt;code&gt;useState&lt;/code&gt;'s value is init-ed only once when the component is loaded up, after that it will behave naturally with its inner state. So, with the heavy actions or results of complex calculation, we would like to calculate - or init them once until further next changes to save some performance by not re-calculating these costly actions. This can be improve by using &lt;strong&gt;Lazy Initialization&lt;/strong&gt;: passing the return value in a callback 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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentDOM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrentDOM&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&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;



</description>
      <category>react</category>
    </item>
    <item>
      <title>Currying in Javascript</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Wed, 12 Apr 2023 03:44:23 +0000</pubDate>
      <link>https://dev.to/ngquan/currying-in-javascript-45ce</link>
      <guid>https://dev.to/ngquan/currying-in-javascript-45ce</guid>
      <description>&lt;p&gt;Currying in Javascript It can be understood as function caching. Consider the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// utils.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;submitForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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;submittedFormData&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;const&lt;/span&gt; &lt;span class="nx"&gt;currentForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formName&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;submit&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;validateForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentForm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;submittedData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;callSubmitFormAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submittedFormData&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;Firstly, we gonna get the form object from &lt;code&gt;formName&lt;/code&gt;; Let's say if we have multiple formData waiting for submission (with diffrerent &lt;code&gt;submittedFormData&lt;/code&gt;), we can use this function and not having to re-call the &lt;code&gt;getForm&lt;/code&gt; function to get the same form:&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;submitForm&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utils&lt;/span&gt;&lt;span class="dl"&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;registerForm1Data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;someFormData1&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;registerForm2Data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;someFormData2&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// return the function `submit` function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formSubmission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;submitForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;REGISTER_FORM&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;isSomeFlag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// can be seen as submitForm('REGISTER_FORM')(registerFormData1)&lt;/span&gt;
    &lt;span class="nx"&gt;formSubmission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registerFormData1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;formSubmission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registerFormData2&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;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://blog.logrocket.com/understanding-javascript-currying"&gt;Understand Currying - blogrocket&lt;/a&gt;;&lt;br&gt;
&lt;a href="https://javascript.info/currying-partials"&gt;Javascript Currying - javascript.info&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>My quick note on Redux</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Wed, 12 Apr 2023 03:43:09 +0000</pubDate>
      <link>https://dev.to/ngquan/my-quick-note-on-redux-46d3</link>
      <guid>https://dev.to/ngquan/my-quick-note-on-redux-46d3</guid>
      <description>&lt;p&gt;Redux is a state management library that helps us to solve the problem of state usage from many places in app and passing props to a deep nesting component.&lt;/p&gt;

&lt;p&gt;Redux ensures 3 pillars in its pattern: 1-way binding data &amp;amp; single source of truth &amp;amp; immutability;&lt;/p&gt;

&lt;p&gt;Everything in Redux is communication with actions. Action is an object that describe what to do and return the new state from reducer&lt;/p&gt;

&lt;p&gt;Actions Creator is a function returning a specific action object =&amp;gt; prevents wrong typing when typing action objects again and again.&lt;/p&gt;

&lt;p&gt;Reducer: A pure function takes in the current state object and a callback function which defines logic and returns new states; the idea of reducer comes from Array.reduce method.&lt;/p&gt;

&lt;p&gt;Redux toolkit: compact action creators, reducer, and selectors into a slice - opinionated architecture.&lt;/p&gt;

&lt;p&gt;We can create multiple stores in redux but it will violate the single source of truth.&lt;/p&gt;

&lt;p&gt;thunk: a middleware that listens to dispatched events and sees if the arguments passed is a function or not. If it's a function then it will be executed (asynchronously)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Redux with normal action:
UI dispatch ---&amp;gt; Actions ---&amp;gt; Reducer receive action and return new state;

&amp;gt; Redux with thunk (passing a function to dispatch instead of object):
UI dispatch ---&amp;gt; thunk ---&amp;gt; middleware executes thunk ---&amp;gt; dispatch action to reducer ---&amp;gt; reducer returns new state.

&amp;gt; Redux with thunk (passing a function to dispatch instead of object):
UI dispatch ---&amp;gt; thunk ---&amp;gt; middleware executes thunk ---&amp;gt; dispatch action to reducer ---&amp;gt; reducer returns new state.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ref:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://daveceddia.com/what-is-a-thunk/"&gt;What the heck is a thunk?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://redux.js.org/faq/actions#how-can-i-represent-side-effects-such-as-ajax-calls-why-do-we-need-things-like-action-creators-thunks-and-middleware-to-do-async-behavior"&gt;"why do we use middleware for async?"&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://redux.js.org/tutorials/essentials/part-5-async-logic#thunks-and-async-logic"&gt;Redux with async logic&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://daveceddia.com/react-redux-immutability-guide/#what-is-immutability"&gt;React-redux immutability &amp;amp; side effects&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>My notes on Javascript functions and related things.</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Tue, 28 Mar 2023 12:27:47 +0000</pubDate>
      <link>https://dev.to/ngquan/my-notes-on-javascript-functions-and-related-things-4k3g</link>
      <guid>https://dev.to/ngquan/my-notes-on-javascript-functions-and-related-things-4k3g</guid>
      <description>&lt;p&gt;I write some writeup thoughts while learning on frontendmaster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution context
&lt;/h3&gt;

&lt;p&gt;When a function is invoked, keep in mind that that function will attach to 2 things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;thread: the ability to run the code in the function from top to the bottom;&lt;/li&gt;
&lt;li&gt;local memory: where the function saves their local variables &amp;amp; inner reference values.
That 2 things is called execution context.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Only the functions can create its own execution context, other code block, loops,... don't create.&lt;br&gt;
So all the variables and values once involved in that function will be locked in and can't be accessed outside.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closure
&lt;/h3&gt;

&lt;p&gt;The ability of accessing values from outer scope. To deeply understand closure you should have a notion of what execution context is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Higher order function
&lt;/h3&gt;

&lt;p&gt;HOF is an idea of passing function as an argument to another function. This concept give us an idea of freely modification make our code more reusable.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>`useState` and `prevState`</title>
      <dc:creator>Hihi</dc:creator>
      <pubDate>Tue, 28 Mar 2023 12:09:19 +0000</pubDate>
      <link>https://dev.to/ngquan/usestate-and-prevstate-kjh</link>
      <guid>https://dev.to/ngquan/usestate-and-prevstate-kjh</guid>
      <description>&lt;p&gt;There's a way to update your React state more efficiently using the &lt;code&gt;prevState&lt;/code&gt; with a callback in &lt;code&gt;setState&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="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;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setAddress&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPhone&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// we have to update them separately in handlers&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleNameChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleAddressChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handlePhoneChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above case, if we have more fields that means the handlers will grow in a repetitive way. To solve this problem, I have a clever way to nest them in an object and make use of &lt;code&gt;prevState&lt;/code&gt; in &lt;code&gt;setState&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFormData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formProps&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;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setFormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;formProps&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;&lt;span class="nx"&gt;newValue&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;



</description>
      <category>react</category>
    </item>
  </channel>
</rss>
