<?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: Osama Mohamed Ammar</title>
    <description>The latest articles on DEV Community by Osama Mohamed Ammar (@osamamammar).</description>
    <link>https://dev.to/osamamammar</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%2F521082%2F228225a5-3ae0-44ea-ad34-8700500ec5ed.jpg</url>
      <title>DEV Community: Osama Mohamed Ammar</title>
      <link>https://dev.to/osamamammar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/osamamammar"/>
    <language>en</language>
    <item>
      <title>JavaScript The Hard Parts-Part 1</title>
      <dc:creator>Osama Mohamed Ammar</dc:creator>
      <pubDate>Thu, 30 Dec 2021 03:49:42 +0000</pubDate>
      <link>https://dev.to/osamamammar/javascript-the-hard-parts-part-1-210a</link>
      <guid>https://dev.to/osamamammar/javascript-the-hard-parts-part-1-210a</guid>
      <description>&lt;p&gt;In the beginning, we need to be sure that we know a lot of things&lt;br&gt;
imagine, the sheet that you write in JavaScript is wrapped with a big thing called execution context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what is execution context?&lt;/strong&gt;&lt;br&gt;
Execution context simply is created to run the code of a function and has 2 parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Thread of execution&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How does JavaScript code run simply?&lt;/strong&gt;&lt;br&gt;
Line-by-line runs or ’executes’, each line — &lt;strong&gt;known as the thread of execution&lt;/strong&gt; and saves ‘data’ like strings and arrays in the memory, so we can use that data later.&lt;/p&gt;

&lt;p&gt;Another thing, we should know the &lt;strong&gt;difference between parameter and argument.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we called &lt;strong&gt;argument&lt;/strong&gt;, we mean the actual number or value that gets passed in, and the &lt;strong&gt;parameter&lt;/strong&gt; is the placeholder that awaits it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Call Stack?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript keeps track of what the function is currently running &lt;br&gt;
(where’s the thread of execution) there are two things that can happen for call stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run a function →add to call stack&lt;/li&gt;
&lt;li&gt;Finish running the function →JS removes it from the call stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the &lt;strong&gt;&lt;em&gt;three main cores in the JS engine&lt;/em&gt;&lt;/strong&gt; Now we can start reviewing all these concepts clearly all together.&lt;/p&gt;

&lt;h2&gt;
  
  
  For example:
&lt;/h2&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;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;multiplyBy2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputNumber&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputNumber&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&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;result&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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;newOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy2&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;😉 let’s play around with that and show what the JavaScript engine will do.&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%2Fcdn-images-1.medium.com%2Fmax%2F4586%2F1%2AL_OZCVopBaMgGTj-JAoVAw.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%2Fcdn-images-1.medium.com%2Fmax%2F4586%2F1%2AL_OZCVopBaMgGTj-JAoVAw.png" alt="Image 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we say, imagine the code we have written is wrapped with something called execution context like a big rectangle, so we should have 2 parts&lt;br&gt;
memory(global memory) and thread of execution.&lt;/p&gt;

&lt;p&gt;When you look at the code example in the first line, we have a constant variable initialized with the number 3 so that will save in global memory.&lt;/p&gt;

&lt;p&gt;In the next line, we have a declaration function called multiplyBy2 this function body we are gonna take, bundle, store it in the computer memory like a string of characters, so we will represent it with a little box that will add to memory without running because we do not call this function yet.&lt;/p&gt;

&lt;p&gt;Now we wave down to where the next line we declare a constant output that stores a call function, so we can’t store that in memory.&lt;br&gt;
We can only store values that fixed final things. We can’t store instructions that tell go to do some work, so the something that tells me the right-hand side is a command can’t store now is a parenthesis, so, for now, the constant output variable it’s gonna remain uninitialized, and we will go running this command.&lt;br&gt;
This command will create an execution context containing two parts that we said above thread and memory (local to this function) look at the picture above.&lt;/p&gt;

&lt;p&gt;So before go to inside multiblyBy2 function we first handle the parameter in the memory(local memory), and it will take a value of the argument which is number 3, then after that, we can grab result hit result, and assign it to 6&lt;/p&gt;

&lt;p&gt;The final line inside a function is the interesting one, we have got a return result, so what is result mean JavaScript doesn't know automatically, so it goes hunting for it in our local memory, finds it's a number 6, and then takes that number and ships it returns it to output which means evaluate output to number 6, &lt;strong&gt;note&lt;/strong&gt; after that execution context will be deleted.&lt;/p&gt;

&lt;p&gt;Next line, we declare constant newOutput we put it in memory, but we don't know what is stored in newOutput yet, because we are going to do another execution context for the calling this function again as we did before, the image below illustrates this.&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%2Fcdn-images-1.medium.com%2Fmax%2F4258%2F1%2Adp8KMe0uBDLQ0TCeIWRUPg.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%2Fcdn-images-1.medium.com%2Fmax%2F4258%2F1%2Adp8KMe0uBDLQ0TCeIWRUPg.png" alt="Image 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the function finishes or returns a value, the execution context will be deleted.&lt;/p&gt;

&lt;p&gt;JavaScript also has something called the call stack, JavaScript keeps track of what function is currently running by using the final part of understanding our platform cores of the JavaScript engine called the call stack, it's like a traditional way of storing information in our computer, the image below illustrates this.&lt;/p&gt;

&lt;p&gt;This is the call stack at the beginning of our program until when we're starting to call a function.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2An7H2EnSbDRCKgu6Ak21FWQ.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2An7H2EnSbDRCKgu6Ak21FWQ.png" alt="Image 3"&gt;&lt;/a&gt;&lt;br&gt;
So when we call multiplyBy2(3) the call stack will look like that.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Ai2ZKq0Awt_-OpUuatGKCpg.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Ai2ZKq0Awt_-OpUuatGKCpg.png" alt="Image 4"&gt;&lt;/a&gt;&lt;br&gt;
After the return function happens, it popped up from the stack and returns to the same shape in the first image, According to our code example, the function after the first pop-up it will call again multiplyBy2(10).&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AlurbsnvrvesRj_Aj3mMc0w.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AlurbsnvrvesRj_Aj3mMc0w.png" alt="Call stack from right to left"&gt;&lt;/a&gt;&lt;br&gt;
After the function finish, the call stack will return to empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; if we have an inner function inside an outer function is already called that makes the inner function pushed to the call stack above outer function and after finished inner function, it will pop up from the stack and leave the outer function in the stack until it finishes and pop up from the call stack and make it’s empty.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you got here&lt;/em&gt;, &lt;strong&gt;&lt;em&gt;thank you&lt;/em&gt;&lt;/strong&gt;. &lt;em&gt;Finally, this article is part of a collection of articles about JavaScript hard parts and behind the scenes, don't forget to follow it when it comes out.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reference:
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Thanks for &lt;a href="https://www.twitter.com/willsentance" rel="noopener noreferrer"&gt;@willsentance&lt;/a&gt; course on &lt;a href="https://frontendmasters.com/courses/javascript-hard-parts-v2/" rel="noopener noreferrer"&gt;@FrontendMaster&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
