<?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: Aarti</title>
    <description>The latest articles on DEV Community by Aarti (@aartiprabhu).</description>
    <link>https://dev.to/aartiprabhu</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%2F377026%2F0fd66c32-662c-4f4d-9fb7-f4f13a678c40.png</url>
      <title>DEV Community: Aarti</title>
      <link>https://dev.to/aartiprabhu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aartiprabhu"/>
    <language>en</language>
    <item>
      <title>Advanced Js recap [part 1]</title>
      <dc:creator>Aarti</dc:creator>
      <pubDate>Tue, 09 Jun 2020 18:43:23 +0000</pubDate>
      <link>https://dev.to/aartiprabhu/advanced-js-recap-part-1-1oa6</link>
      <guid>https://dev.to/aartiprabhu/advanced-js-recap-part-1-1oa6</guid>
      <description>&lt;p&gt;A quick read, to brush up javascript concepts. Read through this if it has been a while and you need a refresher of some of the js basics.&lt;br&gt;
In this part, I will be starting with the &lt;strong&gt;execution context&lt;/strong&gt; and how it relates to &lt;strong&gt;scope, closures, hoisting and 'this'&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The execution context is an environment in which the code is executed. All code runs in some execution context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The global execution context is created when code starts executing.
&lt;/li&gt;
&lt;li&gt;A function execution context is created when a function call is encountered. It is stacked on top of previous ones and removed once its execution is completed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Phases of execution context
&lt;/h3&gt;

&lt;p&gt;An execution context has 2 phases - creation and execution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;creation phase&lt;/em&gt; is the first parse of function code. This includes creation of scope, scope chain and determining the value of &lt;em&gt;this&lt;/em&gt;. Each of these have been elaborated below.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;execution phase&lt;/em&gt; involves assigning variables and running the code. If a function call is found, a new execution context is created.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  The lexical environment/scope variables
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Lexical environment depends on where the code is written i.e. &lt;br&gt;
the location of the words as opposed to where the function is called. It determines the available/accessible variables for the function. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;During creation, memory is allocated for variables, functions and arguments in the lexical environment.&lt;/li&gt;
&lt;li&gt;This leads to hoisting of variables(var declarations) and functions. Variables are partially hoisted as memory is allocated but value is only assigned during execution. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Hoisting is the action of moving all variable and function declarations to the top of their existing scope. It makes code unpredictable and can be avoided by using IIFE for functions and let/const for variables.&lt;br&gt;
&lt;em&gt;Note: Vars are function scoped, let and const are block scoped&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the classic example of setTimeout yielding unexpected results due to hoisting and how &lt;em&gt;let&lt;/em&gt; or &lt;em&gt;IIFE&lt;/em&gt; can fix it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Below loop prints '5' every time console.log is executed. 
This is because i is hoisted and gets assigned value 5 
before the callbacks of setTimeout execute */&lt;/span&gt; 
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;i&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="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Fix1: declaring i as let which keeps i within block scope&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;i&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="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* Fix2: using an IIFE for setTimeout that explicitly passes i 
to keep the correct value in scope */&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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="o"&gt;=&amp;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;i&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="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Scope chain
&lt;/h4&gt;

&lt;p&gt;A scope chain is a link to the function's parent environment variables. This enables closures to form.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is created when a variable is accessed from anywhere up the scope chain. It is kept in memory even if it leaves the enclosing scope. Some common uses of closures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid redeclaring large variables&lt;/li&gt;
&lt;li&gt;Encapsulation =&amp;gt; restrict access, have private variables&lt;/li&gt;
&lt;li&gt;Creating singletons - single instance of an object&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Determining the value of &lt;em&gt;this&lt;/em&gt; (a.k.a. context)
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;'This'&lt;/em&gt; refers to the object which has the function as a property and calls the function.&lt;br&gt;
eg. &lt;em&gt;obj.sayHi()&lt;/em&gt; - &lt;em&gt;this&lt;/em&gt; in &lt;em&gt;sayHi&lt;/em&gt; function would be &lt;em&gt;obj&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The value of &lt;em&gt;this&lt;/em&gt; depends on how the function was called (dynamic scope) and not where it was written (lexical scope)&lt;/li&gt;
&lt;li&gt;Only &lt;em&gt;Arrow functions&lt;/em&gt; have lexically bound &lt;em&gt;this&lt;/em&gt;. Before arrow functions were added to js, either the function &lt;em&gt;bind&lt;/em&gt; method was used or a variable &lt;em&gt;self&lt;/em&gt; was assigned to &lt;em&gt;this&lt;/em&gt; to get lexical bind.&lt;/li&gt;
&lt;li&gt;Context is object based while Scope is function/block based&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Uses of &lt;em&gt;this&lt;/em&gt; in objects&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;gives methods access to their object&lt;/li&gt;
&lt;li&gt;Avoid repetition by executing same function for multiple objects&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The value of &lt;em&gt;this&lt;/em&gt; can be modified by function properties &lt;em&gt;call&lt;/em&gt;, &lt;em&gt;apply&lt;/em&gt; and &lt;em&gt;bind&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Call&lt;/em&gt; and &lt;em&gt;apply&lt;/em&gt; can modify the &lt;em&gt;this&lt;/em&gt; of existing methods on objects. The only difference between them is &lt;em&gt;call&lt;/em&gt; takes separate arguments while &lt;em&gt;apply&lt;/em&gt; takes argument array. &lt;em&gt;Bind&lt;/em&gt; can be used to return a new function with a custom &lt;em&gt;this&lt;/em&gt; value.&lt;br&gt;
Eg. In the code below, &lt;em&gt;this&lt;/em&gt; in &lt;em&gt;sayHi&lt;/em&gt; function would be &lt;em&gt;newObj&lt;/em&gt; rather than &lt;em&gt;obj&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;a&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="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&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;newObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;p&gt;Here's a simple example of what the execution context would look like for the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//global execution context created&lt;/span&gt;
&lt;span class="nf"&gt;callSayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;//new function execution context created&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;//global variable hoisted in creation phase of global execution context and assigned on execution&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;callSayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//global func hoisted&lt;/span&gt;
  &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//new function execution context created&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;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//global func hoisted&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s2"&gt;`Hello there &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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//closure created&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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




&lt;p&gt;Thanks for reading my first ever post :)&lt;br&gt;
Hope this served as a useful reminder of these concepts. If you want to read about them in further detail, I liked the &lt;a href="https://codeburst.io/javascript-demystified-variable-hoisting-c3c4d2e8fd40" rel="noopener noreferrer"&gt;js demystified series&lt;/a&gt; from codeburst.&lt;/p&gt;

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