<?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: Branden Kim</title>
    <description>The latest articles on DEV Community by Branden Kim (@bkbranden).</description>
    <link>https://dev.to/bkbranden</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%2F176374%2Fe384bed7-517b-473a-b072-81647eb60963.jpg</url>
      <title>DEV Community: Branden Kim</title>
      <link>https://dev.to/bkbranden</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bkbranden"/>
    <language>en</language>
    <item>
      <title>TIL - Generating Permutations and API Level Testing</title>
      <dc:creator>Branden Kim</dc:creator>
      <pubDate>Tue, 08 Sep 2020 15:29:37 +0000</pubDate>
      <link>https://dev.to/bkbranden/til-generating-permutations-and-api-level-testing-395m</link>
      <guid>https://dev.to/bkbranden/til-generating-permutations-and-api-level-testing-395m</guid>
      <description>&lt;p&gt;Today I learned...&lt;/p&gt;

&lt;p&gt;A method for generating permutations and patterns for API level testing!&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating Permutations
&lt;/h2&gt;

&lt;p&gt;While trying to solve a new programming problem, I recognized that it involved generating all permutations of an array. While I wasn't able to solve it completely as I missed permutations in the generation sequence, I dove deep in the solutions and internalized the patterns.&lt;/p&gt;

&lt;p&gt;This method involves Divide and Conquer, Swapping, and Backtracking.&lt;/p&gt;

&lt;p&gt;Namely this method is for generating all permutations of an array by swapping elements that you haven't seen so far with ones that you have.&lt;/p&gt;

&lt;p&gt;The divide and conquer part comes from dividing the length of the array into two parts, a prefix from 0 -&amp;gt; i and a postfix from i -&amp;gt; end. The postfix essentially becomes a smaller subproblem of the original as we consider the prefix part done, hence making it divide and conquer.&lt;/p&gt;

&lt;p&gt;The swapping is used to swap elements in the prefix and postfix to generate different permutations as an array of &lt;code&gt;[1, 2, 3]&lt;/code&gt; has a valid permutation &lt;code&gt;[2, 1, 3]&lt;/code&gt; which is out of order. The swapping creates that out of order.&lt;/p&gt;

&lt;p&gt;The backtracking is to go back to a previous state after a swap since you can swap the first index with the second, but want to reverse that process so you can also swap the first with the third.&lt;/p&gt;

&lt;p&gt;The base case is when the current index is the same as the length of the array as it means your prefix has become the entire array, indicating that you have finished that path of generating permutations.&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%2Fbcflsjkxdsat09helgrz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbcflsjkxdsat09helgrz.png" alt="Generating Permutations"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Quick example of generating permutations from Leetcode image&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The pseudo-code looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def outerFunction():
    initialize vars needed

    helperFunction()

def helperFunction(array, start_index):
    if (start_index == len(array)):
        Do base case work here
        return;

    for (i = start_index; i &amp;lt; len(array); i++):
        swap(start_index, i)
        helperFunction(array, start_index + 1)
        swap(start_index, i)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  API level testing patterns
&lt;/h2&gt;

&lt;p&gt;I also looked into API level testing patterns for my productivity tracker. I learned a lot of information such as the cases you need to test for API's (status code, response body, response headers, etc) as well as what to mock in API level testing.&lt;/p&gt;

&lt;p&gt;In my case, I am trying to automate the API level testing from end-to-end so I learned in general you should only mock anything external to what you are trying to test including external libraries / APIs.&lt;/p&gt;

&lt;p&gt;Furthermore, different test cases should be handled for security groups, authentication, headers, cookies depending on the API implementation.&lt;/p&gt;

&lt;p&gt;I also found out that a common pattern people use to develop APIs is having a combination of a real-api backend and mocked apis. The real-api backends are kept/utilized once the APIs are implemented while the mocked ones are for new features and made quickly so that it doesn't block any frontend workers.&lt;/p&gt;

&lt;p&gt;This paradigm reduces the amount of time spent doing nothing as much as possible.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>algorithms</category>
      <category>testing</category>
      <category>learning</category>
    </item>
    <item>
      <title>TIL - Tarjan's Algorithm</title>
      <dc:creator>Branden Kim</dc:creator>
      <pubDate>Tue, 01 Sep 2020 19:00:31 +0000</pubDate>
      <link>https://dev.to/bkbranden/til-tarjan-s-algorithm-2p42</link>
      <guid>https://dev.to/bkbranden/til-tarjan-s-algorithm-2p42</guid>
      <description>&lt;p&gt;While practicing some competitive programming, I came across an interesting subsection of Graph Theory - Strongly Connected Components (SCC).&lt;/p&gt;

&lt;p&gt;In a directed graph, strongly connected components are nodes that have edges such that each node in the SCC is able to have a path to every other node in the SCC. To help you find these SCCs one can use something called Tarjan's Algorithm!&lt;/p&gt;

&lt;p&gt;Tarjan's algorithm is an algorithm that can find SCCs in a directed graph with one DFS pass O(V+E) time and using just a stack O(V) space, where V is the number of nodes and E is the number of edges.&lt;/p&gt;

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

&lt;p&gt;The way it works is that it keeps track of two lists &lt;code&gt;discovery&lt;/code&gt; and &lt;code&gt;low_link&lt;/code&gt; that are the length of the number of nodes. Tarjan's algorithm works correctly because it makes use of a stack to remember what nodes are counted in the SCC. Without this it is possible to run the DFS on the graph in a order such that you locate less SCCs than what actually is on the graph.&lt;/p&gt;

&lt;p&gt;The stack is used to keep track of nodes that are already included in the SCC so far so if you encounter a node that is on the stack, it must be part of the current SCC.&lt;/p&gt;

&lt;p&gt;A high level overview of the algorithm is as follows:&lt;/p&gt;

&lt;p&gt;Traverse through all the nodes with a for loop, running DFS on the node if it is not visited.&lt;/p&gt;

&lt;p&gt;As you visit nodes mark the node as visited, and have a counter that marks each node with ids and sets that node's &lt;code&gt;discovery&lt;/code&gt; and &lt;code&gt;low_link&lt;/code&gt; equal to the node id. Also, push the node onto the stack and remember that it is on the stack.&lt;br&gt;
This algorithm uses the idea that a node's discovery time and low_link value is equal to the order that it got visited as this order is how the algorithm finds the lowest common ancestor.&lt;/p&gt;

&lt;p&gt;Loop through all of the node's neighbors if and run DFS on the unvisited nodes.&lt;/p&gt;

&lt;p&gt;After coming back from the recursion, set the &lt;code&gt;low_link&lt;/code&gt; value of the current node equal to the min between itself and the neighbor.&lt;/p&gt;

&lt;p&gt;Also, check if the neighbor is on the stack and if it is, it means it is part of the SCC. Set the &lt;code&gt;low_link&lt;/code&gt; value to the min between itself and the discovery time of the neighbor.&lt;/p&gt;

&lt;p&gt;After checking all neighbors, check if the current node's &lt;code&gt;discovery&lt;/code&gt; value is equal to the &lt;code&gt;low_link&lt;/code&gt; value as this means its the root node to the SCC. Pop of all the nodes up to and including that current node.&lt;/p&gt;

&lt;p&gt;At the end, your &lt;code&gt;low_link&lt;/code&gt; list should have all of the SCCs!&lt;/p&gt;
&lt;h2&gt;
  
  
  Theoretical Properties
&lt;/h2&gt;

&lt;p&gt;Apparently, this algorithm works off the fact that SCCs are maximal in a way, meaning if you find an SCC, it is guarenteed that there won't be another SCC within it. Another useful property of the algorithm is that no SCC will be identified before any of its successors.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Here is my implementation of Tarjan's Algorithm!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TarjansGraph&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
        &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;UNVISITED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;adj_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;TarjansGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;adj_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;addEdge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;adj_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;at&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="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;iter&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;adj_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;UNVISITED&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                    &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                    &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iter&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;])&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="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stack_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;stack_node&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;stack_node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;

                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;at_stack_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;at_stack_node&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;at_stack_node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&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="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;findSCCs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TarjansGraph&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;UNVISITED&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&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="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myStack&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TarjansGraph&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;UNVISITED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myStack&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="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n\n&lt;/span&gt;&lt;span class="s"&gt; LOW LIST VALUES: "&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;low_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&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;



</description>
      <category>learning</category>
      <category>todayilearned</category>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>PC Building! My first experience building a desktop</title>
      <dc:creator>Branden Kim</dc:creator>
      <pubDate>Mon, 31 Aug 2020 19:00:32 +0000</pubDate>
      <link>https://dev.to/bkbranden/pc-building-my-first-experience-building-a-desktop-cep</link>
      <guid>https://dev.to/bkbranden/pc-building-my-first-experience-building-a-desktop-cep</guid>
      <description>&lt;p&gt;Last weekend, I set out to do something that I had been itching to do for a while... Building my own PC!&lt;/p&gt;

&lt;p&gt;I just wanted to share some of my experiences during the build process and point out some difficulties that may aid those that are looking to get into PC building!&lt;/p&gt;

&lt;h2&gt;
  
  
  PCPartPicker
&lt;/h2&gt;

&lt;p&gt;Probably the most useful site that everyone needs to use when building a pc is &lt;a href="https://pcpartpicker.com/"&gt;pcpartpicker&lt;/a&gt;. This website nicely lays out all the parts that are required to build a new PC as well as a compatibility checker that lets you know if there are any compatibility issues between the pieces of hardware you check out.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JmhjtIOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pxnezzi3djuje821v7z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JmhjtIOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pxnezzi3djuje821v7z6.png" alt="pcpartpicker compatability"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;PCPartPicker pc building tool&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The website nicely lays out the necessary components as well as some compatibility issue information. This website made the building process very manageable since it broke down and listed exactly what parts I needed as well as compatibility information between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking out my parts for my build
&lt;/h2&gt;

&lt;p&gt;As a newcomer, finding all the parts to build a PC with a theme was really overwhelming especially when considering all of the compatibility issues between the hardware pieces.&lt;br&gt;
I ended up opting to look at the parts of that someone else had already made and then I substituted pieces that I wanted from that already made build.&lt;br&gt;
To me, this was the way to teach myself about the options I had for each PC part and how to find the compatibility between the different parts since I could focus on just one part at a time. Looking back on it, this method is not really necessary because PCPartPicker's compatibility check is super helpful, but it was the method that made the most sense for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking out the parts
&lt;/h2&gt;

&lt;p&gt;I, by no means, am an expert on the technicalities on different PC parts and the exact compatibility knowledge between every part, but throughout this experience, I picked up a few tips that might be helpful! Also, I think PCPartPicker warns you of most of the compatibility issues I am listing.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Motherboard
&lt;/h3&gt;

&lt;p&gt;The motherboard was one of the most important parts of the whole process as it interacts with all of the other parts of the PC. This is where a majority of compatibility issues will arise.&lt;br&gt;
Here are some of the compatibility tips that I learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Make sure that the model of the CPU is compatible with the motherboard that you pick&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Often Intel CPUs have models that only work with certain motherboards while AMD have their own respective compatible models&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Double check if your motherboard comes with features like a wifi adapter and the number of PCIe slots for your video card and other expansions that you put in the motherboard&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you buy an M.2 SSD, make sure that your Motherboard supports that, I think most modern ones do&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  PC Case Dimensions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The size of your PC case should be big enough to fit all your components! Often the video card is the biggest part so make sure the case's width, height, and depth are big enough to fit the video card&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Power Supply Unit
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure that your power supply unit covers has more than enough wattage than what is utilized by the parts. This can easily be found on PCPartPicker's building service. My build said it uses around 513W, so I got a 850W PSU just to be safe&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The build process!
&lt;/h2&gt;

&lt;p&gt;Building the PC was quite a process. Since I spent so much money on the parts, I really really didn't want to mess anything up and accidentally break any pins or scratch my motherboard. I watched &lt;a href="https://www.youtube.com/watch?v=v7MYOpFONCU"&gt;Linus Tech Tips POV video&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=IhX0fOUYd8Q"&gt;Bitwit's Step by Step PC building video&lt;/a&gt; over and over and over again following every step carefully.&lt;/p&gt;

&lt;p&gt;It was probably a good thing that I followed the videos meticulously, but also the creators of the parts did such a good job in abstracting a lot of the details out. Assembling things together was really simple and error-prone, I genuinely think that anyone can just pick it up and do it.&lt;/p&gt;

&lt;p&gt;The easiest part was probably connecting the parts to the motherboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M468yhMK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/256opk4umeho6c6gg76b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M468yhMK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/256opk4umeho6c6gg76b.jpg" alt="Finished Motherboard"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Motherboard with CPU, SSD, Memory, and Cooler installed&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I won't go through all the details because the Linus and Bitwit cover the details so much better than I can.&lt;/p&gt;

&lt;p&gt;The parts that they recommended to install first on the motherboard were: CPU, Memory, M.2 SSDs, and the CPU cooler.&lt;/p&gt;

&lt;h2&gt;
  
  
  The PC Case and the dreaded Wire Management
&lt;/h2&gt;

&lt;p&gt;Where I really had trouble and the part of the building that took the most time was assembling all the parts onto the PC case.&lt;/p&gt;

&lt;p&gt;The PC case itself has a bunch of wires that connect to the front of the PC and the manual has no explanation on what any of the wires are for. Because it was my first time and I wanted to be super careful, I spent a lot of time trying to figure out where the wires go and what they were for.&lt;/p&gt;

&lt;p&gt;Hopefully, I can save you some time. The wires are basically to connect the buttons on the front of the case with the motherboard. Generally, your motherboard manual will layout the specific pins that should connect to the front of the case and what each one does.&lt;/p&gt;

&lt;p&gt;These buttons include turning the PC on and off, reset, LED control, and connection to USB ports.&lt;/p&gt;

&lt;p&gt;More connections mean more wires and sooner or later they will start to get overwhelming as the number of connections to your motherboard increases.&lt;/p&gt;

&lt;p&gt;Managing all of the wires was very overwhelming at first, but I think the most simple and manageable way is to use zip ties and bands to group up wires for similar purposes. I grouped all the front case wires together, the fan connections together, and the ones coming from the power supply unit.&lt;/p&gt;

&lt;p&gt;Also, bring the wires from the back to the front from a hole closest to the place it should connect to. It is the most efficient and cleanest way to connect wires to the PC.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't connect the video card until the end!
&lt;/h2&gt;

&lt;p&gt;One of the most annoying mistakes that I made was connecting the video card too early. The video card took up much more space than I thought and I connected the video card before connecting the front wires to the bottom of the motherboard.&lt;/p&gt;

&lt;p&gt;Since I connected the video card early, it was like defusing wires trying to connect the other wires to the bottom of the motherboard without damaging any of the pins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting the big boy wires from the PSU
&lt;/h2&gt;

&lt;p&gt;Figuring out what wires connect to the PSU was also sort of ambiguous to me. The manuals didn't explain what parts needed to connect to the PSU so I was a bit worried.&lt;/p&gt;

&lt;p&gt;I think generally any wire that can't connect to your motherboard and requires power is going to be connected to the PSU. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU&lt;/li&gt;
&lt;li&gt;GPU&lt;/li&gt;
&lt;li&gt;Fans&lt;/li&gt;
&lt;li&gt;CPU Cooler&lt;/li&gt;
&lt;li&gt;External Storage Devices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With everything connected, I finally finished my PC!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ub9RxjoM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b0thuiced4ddza2m5527.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ub9RxjoM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b0thuiced4ddza2m5527.jpg" alt="Finished Build"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;My Finished Build!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Windows!
&lt;/h2&gt;

&lt;p&gt;Finally, I got to the step of installing Windows! Unfortunately, I had a lot of issues installing because the other machines that I had available were MacBooks and the file systems for windows and mac were different.&lt;/p&gt;

&lt;p&gt;I had to format my USB to FAT32 format, but this format doesn't allow any files greater than 4GB and the latest windows iso image has a file install.wim that's bigger than 4GB.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://www.freecodecamp.org/news/how-make-a-windows-10-usb-using-your-mac-build-a-bootable-iso-from-your-macs-terminal/"&gt;guide by Quincy Larson on FreeCodeCamp&lt;/a&gt; worked like a charm for me and I was able to end up booting windows! For some reason, I kept failing to install Windows since I think something was wrong with my memory partitioning. However, after multiple tries of formatting my USB and redownloading Windows, it just started working randomly; I am still not sure what the cause of the error was. After booting I followed &lt;a href="https://www.youtube.com/watch?v=RYYoCXh2gtw"&gt;this video by JayzTwoCents&lt;/a&gt; and it helped me walkthrough step-by-step what I needed.&lt;/p&gt;

&lt;p&gt;In general, I figured out that the right drivers need to be installed for all of the hardware parts (Asus had a Q-Installer program that did it all for me). Also, I configured my memory speed to run at the advertised speed in the BIOS which apparently a lot of people forget to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gaming and Linux!
&lt;/h2&gt;

&lt;p&gt;This whole process was super fun as it was a really cool beginner hands-on project on something that is very useful to me. I would highly recommend building a PC for everyone. It's more approachable than it looks like and feels super rewarding! &lt;/p&gt;

&lt;p&gt;I can't wait to max out the graphics settings on Final Fantasy 14 and install Arch Linux on it to customize my development workflow!&lt;/p&gt;

&lt;p&gt;Please let me know if I messed up any information or if something could be improved in my build or explanation!&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>workstations</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Gamify! - A Gamified Approach to Named vs Arrow Functions</title>
      <dc:creator>Branden Kim</dc:creator>
      <pubDate>Fri, 21 Aug 2020 03:09:12 +0000</pubDate>
      <link>https://dev.to/bkbranden/gamify-a-gamified-approach-to-named-vs-arrow-functions-4nc</link>
      <guid>https://dev.to/bkbranden/gamify-a-gamified-approach-to-named-vs-arrow-functions-4nc</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;This is one part in a series called &lt;strong&gt;Gamify!&lt;/strong&gt; where I try to create Gamified versions of the typical tutorial. I try to gamify learning since I believe its the best way for all skill and passion levels to get what they want out of the tutorial as well as been fun and informative. When going through this tutorial, there is a &lt;strong&gt;level&lt;/strong&gt; that corresponds to how much and how in-depth you want to learn about the topic. If you just want to know what the topic is about &lt;strong&gt;Level 0&lt;/strong&gt; should be enough, but if you care about the nitty gritty details, &lt;strong&gt;Level 4&lt;/strong&gt; might be of interest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Levels&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Level 0 🕹️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Level 1 🎲&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Level 2 🃏&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Level 3 🎮&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Level 4 🎯&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Within Javascript you have probably seen something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fun&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="c1"&gt;// statements...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When encountering this syntax for the first time, it can really confuse you (it did for me) and it took me a while to get used to what it mean and why it was used. &lt;/p&gt;




&lt;p&gt;Well fret no further because I am going to demystify this for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 0
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are "Arrow Functions"?
&lt;/h3&gt;

&lt;p&gt;Arrow functions are another syntactical method to declare functions in Javacript (and Typescript). Basically its another form of function declarations with the following syntax:&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...,&lt;/span&gt; &lt;span class="nx"&gt;paramN&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;statements&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However with arrow functions, they must be assigned to a variable.&lt;/p&gt;

&lt;p&gt;Here is an example:&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;// Declaration&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// invocation&lt;/span&gt;
&lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns 100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This as opposed to the regular function declaration:&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;// Declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&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;a&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Invocation&lt;/span&gt;
&lt;span class="nf"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns 100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the two functions had the exact same result with the same input! Basically, whenever you encounter this syntax, just read it as a regular function in your head!&lt;/p&gt;

&lt;p&gt;If you want to learn more, progress to the next level!&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%2Foyx68dvyhjfm95ytskf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foyx68dvyhjfm95ytskf8.png" alt="drawing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 1
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Differences between Named vs Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Out of all the differences, there is one really important difference between Named and Arrow functions:&lt;/p&gt;

&lt;h3&gt;
  
  
  "This" context
&lt;/h3&gt;

&lt;p&gt;Arrow functions do not redefine the context of the &lt;code&gt;this&lt;/code&gt; keyword when created. This is different from that of named functions which do redefine the &lt;code&gt;this&lt;/code&gt; context based on what scope it is in.&lt;/p&gt;

&lt;p&gt;Back when I first encountered arrow functions and read about their differences, I still didn't understand what the difference was. To help you avoid frustration and understand better, I have created a quick analogy:&lt;/p&gt;

&lt;p&gt;Think of &lt;strong&gt;Named functions&lt;/strong&gt; (ie. when using "function" keyword) as &lt;strong&gt;Mario&lt;/strong&gt; and &lt;em&gt;Arrow functions&lt;/em&gt; (ie. "() =&amp;gt;" syntax) as &lt;em&gt;Luigi&lt;/em&gt;. &lt;strong&gt;Named functions&lt;/strong&gt; and &lt;em&gt;Arrow functions&lt;/em&gt; have the same end goal: defining a function similar to how &lt;strong&gt;Mario&lt;/strong&gt; and &lt;em&gt;Luigi&lt;/em&gt; have the same end goal of defeating Bowser and saving Princess Peach. However, &lt;strong&gt;Mario's&lt;/strong&gt; fireball ability and &lt;em&gt;Luigi's&lt;/em&gt; fireball ability differ in that &lt;strong&gt;Mario's&lt;/strong&gt; fireball adheres to the rules of gravity while &lt;em&gt;Luigi's&lt;/em&gt; fireball does not and is independent of the rules of gravity. &lt;strong&gt;Named functions&lt;/strong&gt; and &lt;em&gt;Arrow functions&lt;/em&gt; have a similar pattern. &lt;strong&gt;Named functions&lt;/strong&gt; always follow the rule of defining the "this" context to its outer scope, while &lt;em&gt;Arrow functions&lt;/em&gt; do not follow this rule. Basically, &lt;strong&gt;Named functions&lt;/strong&gt; similar to &lt;strong&gt;Mario's&lt;/strong&gt; fireballs follow rules while &lt;em&gt;Arrow functions&lt;/em&gt; and &lt;em&gt;Luigi's&lt;/em&gt; fireballs do not follow the rules, even though the overall goals for both are the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  How "this" changes
&lt;/h3&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%2Fo0zwilq85weij70sj90z.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%2Fo0zwilq85weij70sj90z.gif" alt="Basic Bindings Asset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above is a basic demonstration of the &lt;code&gt;this&lt;/code&gt; binding in action. At a high level, we can see that when &lt;code&gt;this&lt;/code&gt; is returned within the arrow function, it is not pointing to the &lt;code&gt;level1obj&lt;/code&gt; but rather to the global window context. On the other hand, the named function return &lt;code&gt;this&lt;/code&gt; points to &lt;code&gt;level1obj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa10dj8lfe086mcemk7n4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa10dj8lfe086mcemk7n4.png" alt="Level 1 Basic This 001"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see here that calling the named function and returning the &lt;code&gt;this&lt;/code&gt; value results in &lt;code&gt;this&lt;/code&gt; referring to our &lt;code&gt;level1obj&lt;/code&gt; which allows us to do things like:&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%2Fzaj05ozy8wz51djzeyp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzaj05ozy8wz51djzeyp7.png" alt="Level 1 Basic This 002"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This allows us to access members of the &lt;code&gt;level1obj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2s0ofwun50txgp1m2ejl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2s0ofwun50txgp1m2ejl.png" alt="Level 1 Basic This 003"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, when we access the arrowFunctions &lt;code&gt;this&lt;/code&gt; that gets returned, we actually get the global &lt;code&gt;window&lt;/code&gt; object. This is because the arrow function does not change the &lt;code&gt;this&lt;/code&gt; context.&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%2Fon7mjwuepsugt6ars9hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fon7mjwuepsugt6ars9hw.png" alt="Level 1 Basic This 004"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hence, accessing &lt;code&gt;testParam&lt;/code&gt; with &lt;code&gt;this&lt;/code&gt; will not work.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use Named vs Arrow
&lt;/h3&gt;

&lt;p&gt;Now that you know some basic on how the Arrow function changes &lt;code&gt;this&lt;/code&gt;, here are some general guidelines for when to use Named functions vs Arrow functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Don't use arrow functions as members of an object
&lt;/h3&gt;

&lt;p&gt;For reasons that we can see above. In the example given above, if for some reason within the function we have to access the members of the object (&lt;code&gt;level1obj&lt;/code&gt; in the example), then we can't access them from within the function which will make things quite difficult.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Use arrow functions within callbacks
&lt;/h3&gt;

&lt;p&gt;There is a deeper explanation for why this rule should be adhered to in the higher levels, but as a general guideline, arrow functions should be used in callbacks as you will be able to preserve your &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use arrow functions within dynamic contexts
&lt;/h3&gt;

&lt;p&gt;By dynamic contexts, I mean anytime you are trying access or modify an object and its methods after the program runs. This usually appears when using events with some kind of event handler. When a callback function is passed to the event handler, the &lt;code&gt;this&lt;/code&gt; reference points to the object that is listening for the event rather than the object that created the callback. Most times, it is advantageous to have the &lt;code&gt;this&lt;/code&gt; reference point to the object that created it to modify its member variables or state. This is a common problem in React that arises when developers first learn about passing functions as props.&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%2F190ai2t1bn3pyjo4lpyr.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%2F190ai2t1bn3pyjo4lpyr.gif" alt="Level 1 Dynamic Context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see that when the Named Function is called within the class, the &lt;code&gt;this&lt;/code&gt; context is not pointing to the class but rather the global window. &lt;/p&gt;

&lt;p&gt;On the other hand, the arrow function preserves the &lt;code&gt;this&lt;/code&gt; context and can access the &lt;code&gt;Dynamic&lt;/code&gt; classes's member variables within the callback function.&lt;/p&gt;

&lt;p&gt;If you want to go more in-depth in the differences go to the next level!&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%2Foyx68dvyhjfm95ytskf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foyx68dvyhjfm95ytskf8.png" alt="drawing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 2
&lt;/h2&gt;

&lt;p&gt;Arrow functions have more differences than just the &lt;code&gt;this&lt;/code&gt; context and for simplification, I spared the explanation on why the differences occur.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arguments Binding
&lt;/h3&gt;

&lt;p&gt;Named functions have this feature called arguments binding. Utilizing the &lt;code&gt;new&lt;/code&gt; keyword, you can create an instance of a function and store the arguments to a function within a variable.&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%2Fb6fwnxowwm4q1prp8nnp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb6fwnxowwm4q1prp8nnp.png" alt="Arguments Binding Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see that when utilizing a named function, we are able to bind the arguments by utilizing the &lt;code&gt;arguments&lt;/code&gt; keyword within the function.&lt;/p&gt;

&lt;p&gt;However, in the arrow function, it doesn't keep that reference to the &lt;code&gt;arguments&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constructable and Callable
&lt;/h3&gt;

&lt;p&gt;Named functions are constructable and callable meaning that they are able to be called utilizing the &lt;code&gt;new&lt;/code&gt; keyword, creating a new instance of the function, and are able to be called as regular functions.&lt;/p&gt;

&lt;p&gt;Arrow functions, on the other hand, are only callable. This means that arrow functions cannot be called utilizing the &lt;code&gt;new&lt;/code&gt; keyword.&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%2Fynfev91l2cp786zeoq42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fynfev91l2cp786zeoq42.png" alt="Constructable and Callable Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above, we can see that &lt;code&gt;new&lt;/code&gt; could be used with named functions to create a new object. However, when utilized with the arrow function, the compiler gives an error: "TypeError: y is not a constructor".&lt;/p&gt;

&lt;h3&gt;
  
  
  Generators
&lt;/h3&gt;

&lt;p&gt;Named functions have access to a special keyword &lt;code&gt;yield&lt;/code&gt;. This keyword along with a special syntax on the function declaration allows the function to become a &lt;code&gt;Generator function&lt;/code&gt;. A generator function is one that can be exited and later re-entered where the information within the function context is saved even after exiting the function. If this sounds a bit confusing don't worry! What generator functions are, how they work, and use cases will be covered in another Gamify! series post.&lt;/p&gt;

&lt;p&gt;While named functions have access to &lt;code&gt;yield&lt;/code&gt;, arrow functions do not, meaning arrow functions cannot be generator functions.&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%2Fxlexngpcpze9dhlawlu4.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%2Fxlexngpcpze9dhlawlu4.gif" alt="Generator Functions Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above we can see that when using the named function, we were able to create generator functions and utilize them with &lt;code&gt;yield&lt;/code&gt;. However, when the same syntax was made the arrow function, the parser couldn't figure out what &lt;code&gt;yield&lt;/code&gt; was.&lt;/p&gt;

&lt;h3&gt;
  
  
  In-depth explanation of "this" context
&lt;/h3&gt;

&lt;p&gt;In the previous level, we found several use cases of named and arrow functions based on how the &lt;code&gt;this&lt;/code&gt; context changes. While I explained the "what" I haven't yet explained the "why".&lt;/p&gt;

&lt;p&gt;When generalized, the rules of how the &lt;code&gt;this&lt;/code&gt; context switches are fairly simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; keyword&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword changes the context of the outermost &lt;code&gt;this&lt;/code&gt; context for everything within that scope. This means that any functions defined within the object that gets created using &lt;code&gt;new&lt;/code&gt; will have its &lt;code&gt;this&lt;/code&gt; reference pointing to that new object. Let's see a very simple example of how this changes.&lt;/p&gt;

&lt;p&gt;Normally in the global scope, &lt;code&gt;this&lt;/code&gt; refers to either the window or undefined. If we were to create a new object with &lt;code&gt;new&lt;/code&gt;, then if any of the functions within that new object reference &lt;code&gt;this&lt;/code&gt;, they will point to the new object that was created.&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%2F5x2fnckve7k6glscfu25.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%2F5x2fnckve7k6glscfu25.gif" alt="New Context Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see that we create a new &lt;code&gt;obj1&lt;/code&gt; that logs its &lt;code&gt;this&lt;/code&gt; reference and it points to itself. Within its member functions, it creates a new instance of &lt;code&gt;obj2&lt;/code&gt; which logs it own &lt;code&gt;this&lt;/code&gt; reference which points to its own member variables in both the named function and arrow function. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword changes all of the &lt;code&gt;this&lt;/code&gt; contexts of functions (both named and arrow) defined in its scope to point to instance of the newly instantiated object.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Callbacks makes things a bit messy. When encountering a  function declaration to find the &lt;code&gt;this&lt;/code&gt; context, the outer scope needs to be identified. While the scope of normal variables are determined by the lexical scope, the &lt;code&gt;this&lt;/code&gt; scope is determined by where it is called. Generally, the way callbacks work is that the compiler stores the context of where the callback function was passed as the callback'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;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;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;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&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="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, this it will print out "Hi undefined". In this case, the callback "obj.cb" was defined in the global scope and as such the &lt;code&gt;this&lt;/code&gt; reference will be lost and not set to &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unlike named functions, arrow functions are treated as variables and thus are subject to the compiler's lexical scope. This means that within callbacks, there will be a difference in functionality with the &lt;code&gt;this&lt;/code&gt; keyword.&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%2Foyle3qfvs9cvg50rxui2.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%2Foyle3qfvs9cvg50rxui2.gif" alt="Callbacks Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see in the above example that when a named function is used within the callback, the &lt;code&gt;this&lt;/code&gt; context becomes global as when setTimeout is invoked, where the callback gets defined and run is in the global context not in &lt;code&gt;obj&lt;/code&gt;, hence the &lt;code&gt;this&lt;/code&gt; context points to the window.&lt;/p&gt;

&lt;p&gt;On the other hand, when an arrow function is used, since it is treated as a variable, it doesn't redefine the &lt;code&gt;this&lt;/code&gt; context which is why it still points to &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Nested objects within classes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The simplest way to handle how named and arrow functions differ is to treat named functions as redefining &lt;code&gt;this&lt;/code&gt; to the parent context of where it is defined and arrow functions as not redefining &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuqj6qu4pylposk030pw3.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%2Fuqj6qu4pylposk030pw3.gif" alt="Nested Objs Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this nested objects example, the named function &lt;code&gt;this&lt;/code&gt; reference points to the innermost nested object while the arrow function &lt;code&gt;this&lt;/code&gt; reference points to the outermost object. &lt;/p&gt;

&lt;p&gt;Thats all for this level, in the next one we will cover different instances and common patterns for fixing losing &lt;code&gt;this&lt;/code&gt; context.&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%2Foyx68dvyhjfm95ytskf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foyx68dvyhjfm95ytskf8.png" alt="drawing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 3
&lt;/h2&gt;

&lt;p&gt;Here I wanted to cover several examples of using named vs arrow functions and explaining the results of each example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Asynchronous Functions&lt;/li&gt;
&lt;/ol&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%2F0fflvpquo6csp8ribve1.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%2F0fflvpquo6csp8ribve1.gif" alt="Asynchronous Functions Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With asynchronous functions, the binding of &lt;code&gt;this&lt;/code&gt; follows the same rules as for regular functions and callbacks. In the example above, we can see that when using named functions for the callback to the Promise, we lose the context of &lt;code&gt;this&lt;/code&gt; and it gets sent to the window. However, when we use arrow functions, we retain our context to the object. One aspect to note is that because our "arrowFunction" member variable is a named function, the &lt;code&gt;this&lt;/code&gt; context within it points to the &lt;code&gt;obj&lt;/code&gt;. If we had used an arrow function instead, it &lt;code&gt;this&lt;/code&gt; would point to the window instead.&lt;/p&gt;

&lt;p&gt;A takeaway we can note here is that, asynchronous functions don't change any differences between named and arrow functions, they both retain the same rules when used as regular functions and callbacks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Classes&lt;/li&gt;
&lt;/ol&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%2F4ecn7c0c9fglk2745wc8.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%2F4ecn7c0c9fglk2745wc8.gif" alt="Classes Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Within classes, the context of &lt;code&gt;this&lt;/code&gt; changes due to the use of the &lt;code&gt;new&lt;/code&gt; keyword. Because &lt;code&gt;new&lt;/code&gt; is an identifier for detecting the start of a new context, both &lt;code&gt;namedFunction&lt;/code&gt; and &lt;code&gt;arrowFunc&lt;/code&gt; have their &lt;code&gt;this&lt;/code&gt; context pointing to &lt;code&gt;class Testing&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Following the rule for callbacks mentioned previously, when we call &lt;code&gt;namedFunction&lt;/code&gt; because of the use of named functions within the callbacks, the &lt;code&gt;this&lt;/code&gt; context is lost within the Promise.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;arrowFunc&lt;/code&gt; uses arrow functions in the callback handlers, so the &lt;code&gt;this&lt;/code&gt; context is kept.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object.create() and Prototypes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prototypes are the method in which Javascript objects inherit base and additional features from one another. Using &lt;code&gt;Object.create()&lt;/code&gt; syntax, you are able to create the equivalent of &lt;code&gt;classes&lt;/code&gt; using prototypes in Javascript with Objects.create().&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%2Fbzyx0j6urpfrp6y91n4g.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%2Fbzyx0j6urpfrp6y91n4g.gif" alt="Objects Prototypes Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, using the prototype of the object &lt;code&gt;proto&lt;/code&gt; I created a new object using &lt;code&gt;Object.create()&lt;/code&gt;. Here it just creates a new object with the prototype that gets passed in meaning, &lt;code&gt;p&lt;/code&gt; is a new object with the member variables and methods of &lt;code&gt;proto&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this scenario, &lt;code&gt;namedFunc&lt;/code&gt; has a &lt;code&gt;this&lt;/code&gt; reference to the member variables of &lt;code&gt;proto&lt;/code&gt; but just returning &lt;code&gt;this&lt;/code&gt; by itself shows an empty object. This is probably due to the fact that Javascript cannot determine whether &lt;code&gt;this&lt;/code&gt; is referring to &lt;code&gt;proto&lt;/code&gt; or the prototype for objects as Object.create() creates an object with the existing object as the prototype of the newly created object. &lt;/p&gt;

&lt;p&gt;When using &lt;code&gt;arrowFunc&lt;/code&gt; there is no &lt;code&gt;new&lt;/code&gt; keyword used here, even though we are creating a new object. This combined with the rules for arrow functions never change the &lt;code&gt;this&lt;/code&gt; context, thus not changing it from pointing to the window.&lt;/p&gt;

&lt;h3&gt;
  
  
  Patterns for fixing losing &lt;code&gt;this&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;So how do we not lose &lt;code&gt;this&lt;/code&gt; (nice pun)?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using arrow functions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrow functions in Javascript are actually treated as variables that are bound to the lexical scope as opposed to functions (more on this in the next level). This is why arrow functions don't change the &lt;code&gt;this&lt;/code&gt; context when created.&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;arrowFunc&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;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="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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;higherOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;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;some new object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;

    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;namedFunction&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="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="nf"&gt;higherOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;higherOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrowFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think is going to be printed to the console in both cases?&lt;/p&gt;

&lt;p&gt;Here &lt;code&gt;namedFunction&lt;/code&gt; actually prints the &lt;code&gt;obj&lt;/code&gt; that was defined within the &lt;code&gt;higherOrder&lt;/code&gt; function while &lt;code&gt;arrowFunc&lt;/code&gt; prints the global window. &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%2Fjqjxaxde7jt1wd8oyqp0.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%2Fjqjxaxde7jt1wd8oyqp0.gif" alt="Callback This Binding Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason this happens is because when &lt;code&gt;arrowFunc&lt;/code&gt; was defined, it was treated as a &lt;strong&gt;variable&lt;/strong&gt; meaning where &lt;code&gt;this&lt;/code&gt; was referring to was already known as the lexer was able to scope the variable to the outermost scope.&lt;/p&gt;

&lt;p&gt;However, with &lt;code&gt;namedFunction&lt;/code&gt;, it is treated as a function and when it got passed into &lt;code&gt;higherOrder&lt;/code&gt;, there was no way it could know what &lt;code&gt;this&lt;/code&gt; was referring to until it was tied as a member function to &lt;code&gt;obj&lt;/code&gt; within &lt;code&gt;higherOrder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Because of this effect within callbacks, it is generally preferred to pass arrow functions within callbacks as the &lt;code&gt;this&lt;/code&gt; context doesn't change as much and cause confusion.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, or &lt;code&gt;apply()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When using &lt;code&gt;bind()&lt;/code&gt; on a function, this returns a copy of the function with &lt;code&gt;this&lt;/code&gt; pointing to the object passed into the bind 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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;aProp&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 is a property&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="nf"&gt;namedFunction&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="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;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;When passed to bind, this object will be referenced by 'this'&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;funcBind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns obj&lt;/span&gt;

&lt;span class="nf"&gt;funcBind&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns obj2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that by using &lt;code&gt;bind()&lt;/code&gt; we were able to bind the &lt;code&gt;this&lt;/code&gt; reference to another object. When using &lt;code&gt;bind()&lt;/code&gt; it expects a parameter that is an object to bind the &lt;code&gt;this&lt;/code&gt; reference to and then returns a copy of the function with the &lt;code&gt;this&lt;/code&gt; reference changed. Also, the original function is not changed as above, &lt;code&gt;obj.namedFunction()&lt;/code&gt; still has its &lt;code&gt;this&lt;/code&gt; pointing to itself.&lt;/p&gt;

&lt;p&gt;A common pattern is for an object to pass itself in &lt;code&gt;bind()&lt;/code&gt; so that its member function can be passed to another function as a callback, but still modify properties in the original object.&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;ChangeMe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;handleChange&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Commonly used in React components, if &lt;code&gt;handleChange()&lt;/code&gt; is passed as a prop to a child component without calling &lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; will point towards the child component and will change the child state not the parent.&lt;/p&gt;

&lt;p&gt;Using bind, however, we can fix this!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChangeMe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindHandleChange&lt;/span&gt; &lt;span class="o"&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;handleChange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;handleChange&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two other functions: &lt;code&gt;apply()&lt;/code&gt; and &lt;code&gt;call()&lt;/code&gt; that have a similar functionality as &lt;code&gt;bind()&lt;/code&gt; except, they call and run the function immediately.&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;aProp&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 is a property&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="nf"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param2&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;param1&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;param2&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="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;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;When passed bind, this object will be referenced by 'this'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&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="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;namedFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test2&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;Both apply and call takes the object to bind &lt;code&gt;this&lt;/code&gt; to as the first parameter and run the function immediately. However, &lt;code&gt;apply()&lt;/code&gt; takes an array of parameters, while &lt;code&gt;call()&lt;/code&gt; takes parameters separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, and &lt;code&gt;apply()&lt;/code&gt; all bind &lt;code&gt;this&lt;/code&gt; to the object that gets passed in. In common cases, the class that owns that function usually binds its own &lt;code&gt;this&lt;/code&gt; reference to the function in case that the function is used in a callback.&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%2Foyx68dvyhjfm95ytskf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foyx68dvyhjfm95ytskf8.png" alt="drawing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 4
&lt;/h2&gt;

&lt;p&gt;I know what some of you are thinking at this level, exactly why does Javascript treat named and arrow functions differently?&lt;/p&gt;

&lt;p&gt;In this level lets take a look at the AST that gets generated from Javascript compilers, specifically &lt;strong&gt;Node&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Parser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;acorn&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;namedAst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function namedFunction() { return 1}&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;namedAst&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;arrowAst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;const arrowFunction = () =&amp;gt; {return 1}&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;arrowAst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am just passing a very simple named function and arrow function in the form of a string to a package called &lt;strong&gt;acorn&lt;/strong&gt; which is a package for a small Javascript parser that can generate the AST for a given Javascript program (for those that are not familiar, AST is &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;abstract syntax tree&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc5zeezolmciblukqznrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc5zeezolmciblukqznrs.png" alt="Named Function AST"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the AST node output for a named function, we can see that it is of type &lt;strong&gt;FunctionDeclaration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fib69rksjv190urtcxdjo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fib69rksjv190urtcxdjo.png" alt="Arrow Function AST"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, an arrow function is treated as a node of type &lt;strong&gt;VariableDeclaration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FunctionDeclaration&lt;/strong&gt; and &lt;strong&gt;VariableDeclaration&lt;/strong&gt; types are interesting, but we don't know what they are yet. After digging into the source code for the Node compiler, &lt;br&gt;
I was able to pin down some files where these types were referenced.&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%2F45s6zhpwc6tax6rw7ybt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F45s6zhpwc6tax6rw7ybt.png" alt="Declare Function Source Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the Node compiler, this is the source code within &lt;strong&gt;scopes.cc&lt;/strong&gt; to generatee the scope for default function variables.&lt;/p&gt;

&lt;p&gt;Highlighted is a function within the same file that checks if the function is derived from an object and then assigns the &lt;code&gt;this&lt;/code&gt; variable as a function local variable. &lt;/p&gt;

&lt;p&gt;In addition there is a function called &lt;code&gt;DeclareDynamicGlobal&lt;/code&gt; that gets used within the declaration of the scope that references &lt;code&gt;this&lt;/code&gt;, most likely to change it dynamically based on the current scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxvb5t4pwn943btcru5pz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxvb5t4pwn943btcru5pz.png" alt="Declare Scope Source Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand for variable declarations, there is no changing of the &lt;code&gt;this&lt;/code&gt; variable within its declaration.&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%2F3nlba2y8jrshdzzkh8wl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3nlba2y8jrshdzzkh8wl.png" alt="Declare Variable Source Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is more to this function, however, there was nothing reeferencing the two methods, &lt;code&gt;DeclareThis&lt;/code&gt; and &lt;code&gt;DeclareDynamicGlobal&lt;/code&gt; within this function for declaring the scope of variables.&lt;/p&gt;

&lt;p&gt;While I am not too familiar with this source code as I haven't written or contributed to it, I think I was able to make a reasonable assumption as to why functions reassign &lt;code&gt;this&lt;/code&gt; but variables do not.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  If you made it this far congrats! 🎉
&lt;/h3&gt;

&lt;p&gt;This was a part in the series of &lt;strong&gt;Gamify!&lt;/strong&gt; where I try to write gamified tutorials that go in-depth (to the best of my ability) into a topic while also providing simplifications and steps towards learning more advanced knowledge within the topic. This time we covered Named vs Arrow functions, specifically, how they are the same, but also how they differ as well as providing solutions to common problems faced when handling those differences. Furthermore, we went in-depth into the AST of a Javascript  compiler to figure out why and how the compiler made those differences happen.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>javascript</category>
      <category>gamification</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>TIL - Spread and Copying Objects in Javascript</title>
      <dc:creator>Branden Kim</dc:creator>
      <pubDate>Tue, 11 Aug 2020 12:57:48 +0000</pubDate>
      <link>https://dev.to/bkbranden/til-spread-and-copying-objects-in-javascript-1i7j</link>
      <guid>https://dev.to/bkbranden/til-spread-and-copying-objects-in-javascript-1i7j</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;This is a part of a series of writing a tutorial of what I learn everyday. I try to learn something new related to CS and programming everyday and I believe that writing some sort of post, report, or tutorial from memory really solidifies the understanding and makes it stick in your brain.&lt;/p&gt;

&lt;h1&gt;
  
  
  Today I learned...
&lt;/h1&gt;

&lt;p&gt;How to utilize the spread operator and how copying objects in Javascript work. &lt;/p&gt;

&lt;p&gt;What do you think the code below will output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;prim&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="na"&gt;anotherObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;truck&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;obj&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;truck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;anotherObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&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;truck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;anotherObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;anotherObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It turns out that the "val" within "anotherObj for both truck and obj will be "blue". This is a bit confusing since shouldn't the two objects be separate since they are stored in separate variables?&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep vs Shallow Copy
&lt;/h2&gt;

&lt;p&gt;In Javascript, all primitive types are assigned and passed by value, but all objects are assigned and passed by reference. This explains why in the previous code block changing the value of a property of an object resulted in the copy of the object to have its property updated as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;prim&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="na"&gt;anotherObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;truck&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;obj&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;truck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prim&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123123&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;truck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 123123&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case since we are modifying the "prim" property which is a primitive type it is not reflected across the other object since in Javascript, primitive types are assigned by value not reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does by reference mean?
&lt;/h2&gt;

&lt;p&gt;Passing or assigning by reference means that when copied the new variable holds a reference or "points" to the space in memory where the original object lies. This means that any changes to either the original object or anything that references it changes the values within the original object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shallow copies
&lt;/h2&gt;

&lt;p&gt;Using the spread operator or Object.assign() you can create shallow copies of objects!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;testing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;testing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="na"&gt;nestedObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;nestedTesting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nestedTesting&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&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;obj1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As seen above, the spread operator is "...".&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep copies
&lt;/h2&gt;

&lt;p&gt;When another object is created with a deep copy, any nested objects are newly created so they don't share the same reference. This means that changes to the copy of the object are not reflected in the original object since a new object is created for the copy.&lt;/p&gt;

&lt;p&gt;A way to perform a deep copy is to use the &lt;a href="https://www.npmjs.com/package/lodash.clonedeep"&gt;lodash clonedeep&lt;/a&gt; package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merging Objects
&lt;/h2&gt;

&lt;p&gt;Merging objects can also be performed with the spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj1&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;obj1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;testing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="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;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;obj2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;testing2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// obj3 has all of the properties in both obj1 and obj2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A thing to note is that if there are properties with the same name in the objects, the value of the last object with that property gets assigned.&lt;/p&gt;

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