<?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: Leonardo Cunha</title>
    <description>The latest articles on DEV Community by Leonardo Cunha (@lmscunha).</description>
    <link>https://dev.to/lmscunha</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%2F489874%2F86191b6f-115d-4fec-868b-129a6a47c19f.jpeg</url>
      <title>DEV Community: Leonardo Cunha</title>
      <link>https://dev.to/lmscunha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lmscunha"/>
    <language>en</language>
    <item>
      <title>Thread of Execution - One Thing at a time</title>
      <dc:creator>Leonardo Cunha</dc:creator>
      <pubDate>Fri, 11 Feb 2022 23:54:25 +0000</pubDate>
      <link>https://dev.to/lmscunha/thread-of-execution-one-thing-at-a-time-3odc</link>
      <guid>https://dev.to/lmscunha/thread-of-execution-one-thing-at-a-time-3odc</guid>
      <description>&lt;p&gt;OK, you just started learning a programming language and fell in love with JavaScript. However, do you still need help with the code? Guessing different scenarios until you achieve your goal?&lt;/p&gt;

&lt;p&gt;Please don't worry; this is normal. I just want you to know that you are not alone in this endeavour. &lt;/p&gt;

&lt;p&gt;Although, I want to help you. So you can get more control of your code. You can understand more about what is happening "under the hood".&lt;/p&gt;

&lt;h4&gt;
  
  
  One thing at the time
&lt;/h4&gt;

&lt;p&gt;The first thing you need to understand to gain more control of your code is to know more about the role of the Thread of Execution(TE). &lt;/p&gt;

&lt;p&gt;By design, JavaScript was created to execute one thing at a time(Single Thread) and in a specific order(top -&amp;gt; bottom). It's responsible for executing line by line of your JS code. &lt;/p&gt;

&lt;p&gt;"I see, but what do you mean by executing?" you might ask. Let's go through an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Suppose we wrote the following JavaScript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Leo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&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="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Spock&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;validateAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Alright, let's dive in:&lt;/p&gt;

&lt;p&gt;1- Where have all the constants gone?&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;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Leo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we declare a constant named author and assign the value 'Leo'.&lt;/p&gt;

&lt;p&gt;And not only that. We also store this constant in our memory. We can access the value 'Leo' using the variable author.&lt;/p&gt;

&lt;p&gt;2- "Sit tight and assess..."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&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="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Spock&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions" rel="noopener noreferrer"&gt;Functions&lt;/a&gt; are a &lt;em&gt;set of statements that performs a task or calculates a value&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Although, we still need to execute this function.&lt;br&gt;
And for that, we need the "()".&lt;/p&gt;

&lt;p&gt;For now, the TE stores this whole block in our memory. And like we see above, allowing us to use it later.&lt;/p&gt;

&lt;p&gt;3- "Call me"&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="nf"&gt;validateAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;At last, we are just calling/executing our function. The TE checks the label we use, looks into the memory and runs it. &lt;/p&gt;

&lt;p&gt;And not only that. Please take a look at the label author inside the parenthesis. This means that we wanna use the value of the label author('Leo'), which is already stored in our memory, as a parameter in our function.&lt;/p&gt;

&lt;p&gt;Therefore, we grab the value 'Leo' and check if the string equals 'Spock'.&lt;/p&gt;

&lt;p&gt;That's it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Practice!
&lt;/h3&gt;

&lt;p&gt;Of course, this is a simple example. However, it would be best for you to start to create a level of understanding of how your code will be executed. Thus avoiding the guessing habit.&lt;/p&gt;

&lt;p&gt;And the only way to improve that, it's with practice. The &lt;a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging" rel="noopener noreferrer"&gt;Rubber duck debugging&lt;/a&gt; method might be helpful.&lt;/p&gt;

&lt;p&gt;After that, you can search for what I mean by "memory" and so on. Curiosity is an excellent tool for us developers. &lt;/p&gt;

&lt;p&gt;Until the next JS topic! &lt;/p&gt;

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