<?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: Arjun Junna</title>
    <description>The latest articles on DEV Community by Arjun Junna (@arjunjunna).</description>
    <link>https://dev.to/arjunjunna</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%2F658363%2F7dc77afd-31e5-41c5-ac22-6a4fe0a8c58a.jpeg</url>
      <title>DEV Community: Arjun Junna</title>
      <link>https://dev.to/arjunjunna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arjunjunna"/>
    <language>en</language>
    <item>
      <title>How do Functions and Variable Environment work together in JavaScript?👀</title>
      <dc:creator>Arjun Junna</dc:creator>
      <pubDate>Sun, 15 Aug 2021 15:19:21 +0000</pubDate>
      <link>https://dev.to/arjunjunna/how-do-functions-and-variable-environment-work-together-in-javascript-4c4i</link>
      <guid>https://dev.to/arjunjunna/how-do-functions-and-variable-environment-work-together-in-javascript-4c4i</guid>
      <description>&lt;h3&gt;
  
  
  I know that you have worked with functions before too. But do you really know how a function works in JavaScript behind the scenes?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  How does JavaScript treats its functions and variable environment?
&lt;/h3&gt;

&lt;p&gt;Let's find that out together...&lt;/p&gt;

&lt;p&gt;Below we have a very small simple program with us. And yes this is gonna blow your mind in just a few minutes into reading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 1;
a();
console.log(x);

function a() {
  var x = 10;
  console.log(x);
}

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

&lt;/div&gt;



&lt;p&gt;Please open the developer console and use the keyboard shortcut Ctrl Shift J (on Windows) or Ctrl Option J (on Mac). In the console tab, you will be able to see this once the JavaScript runs the above program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Przf-33Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9z2c4jiazujockgy73u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Przf-33Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9z2c4jiazujockgy73u.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until now everything you have done could be things that you already knew. But the real fun is only starting now.&lt;/p&gt;

&lt;p&gt;Let's place a debugger on line 1. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NAoO7QcI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqzn4tr4cqd5ke8ydidg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NAoO7QcI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqzn4tr4cqd5ke8ydidg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over here JavaScript creates a global execution context for the program. It has been pushed into the call stack and the control is in line 1.&lt;/p&gt;

&lt;p&gt;Oh, wait!!! Don't you know what a Global Execution Context is? Then read on...&lt;/p&gt;

&lt;p&gt;The global execution context has two components.&lt;/p&gt;

&lt;p&gt;1 - Variable environment phase also called the memory phase.&lt;/p&gt;

&lt;p&gt;2- Code execution phase.&lt;/p&gt;

&lt;p&gt;In the variable environment phase, JavaScript allocates memory to the variables and functions. For the variables, it allocates the keyword 'undefined' and for the functions, it allocates its function body itself as the memory.&lt;/p&gt;

&lt;p&gt;And in the code execution phase, JavaScript executes the program lines in order.&lt;/p&gt;

&lt;p&gt;Now that this is cleared. Read on...&lt;/p&gt;

&lt;p&gt;In our program since the control is in line 1. JavaScript allocated 'undefined' to the variable 'x' and to the function a() it has allocated its function body.&lt;/p&gt;

&lt;p&gt;Now let's put the debugger to line 2 and click the debugger play button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K7oZiQuh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhc4hud3tme08p62dej7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K7oZiQuh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhc4hud3tme08p62dej7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that line 1 is executed JavaScript allocated the value 1 to variable x.&lt;/p&gt;

&lt;p&gt;Now let's put the debugger to line 6 and click the debugger play button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TJ9d5pkI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n8knr4h0pa5koou6xw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TJ9d5pkI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n8knr4h0pa5koou6xw7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here JavaScript created a new execution context for function a( ). This is been pushed to the call stack. And the control is now given to a(). This doesn't end here. JavaScript also created a new local memory and global memory just for function a( ).&lt;/p&gt;

&lt;p&gt;As for the memory phase, JavaScript allocated the keyword 'undefined' to the variable present in the local memory of the function a( ).&lt;/p&gt;

&lt;p&gt;Now let's put the debugger to line 7 and click the debugger play button.&lt;/p&gt;

&lt;p&gt;JavaScript looks for variable x in local memory and allocates the value '10' to variable x.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--USh0wbOr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2j1xavvrixr9rt8q0cle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--USh0wbOr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2j1xavvrixr9rt8q0cle.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's put the debugger to line 3 and click the debugger play button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--68Te_5N2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1a1p2fvjld6forc35sp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--68Te_5N2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1a1p2fvjld6forc35sp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can notice that in the call stack the execution context for function a( ) just got popped out and now the control is in line 3 back to the global execution context.&lt;/p&gt;

&lt;p&gt;If you open the console tab now, you will see the below line there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aYJeuScH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zkz5jgi3lxcaodc34lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYJeuScH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zkz5jgi3lxcaodc34lw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now for one last time click the debugger play button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_mIUBNqC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7oo37lib4pmq36vw6ve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_mIUBNqC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7oo37lib4pmq36vw6ve.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript ran the remaining program. It looked for variable x in global memory and allocates the value '1' to variable x. It now completed the global execution context in the call stack. And now the global execution context just got popped out of the stack. The call stack is now empty.&lt;/p&gt;

&lt;p&gt;Head over to the console tab and you will see the below lines.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s8-rCApS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrduummpq19iujswuwyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s8-rCApS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrduummpq19iujswuwyk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is how functions and variable environment work together in JavaScript.&lt;/p&gt;

&lt;p&gt;Were you not blown away?&lt;/p&gt;

&lt;p&gt;Haha!!! I know this was just amazing altogether.&lt;/p&gt;

&lt;p&gt;If you made it this far then congrats you just learned -&amp;gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to use a debugger in the console.&lt;/li&gt;
&lt;li&gt;How JavaScript works with the functions and the variable 
environment.&lt;/li&gt;
&lt;li&gt;How the call stack works.&lt;/li&gt;
&lt;li&gt;How the function invocation takes place behind the scenes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you learned anything from this blog then please leave a like and comment on what you felt about the blog.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>coding</category>
      <category>functions</category>
    </item>
    <item>
      <title>Variable and Function Hoisting in JavaScript</title>
      <dc:creator>Arjun Junna</dc:creator>
      <pubDate>Sat, 14 Aug 2021 16:16:04 +0000</pubDate>
      <link>https://dev.to/arjunjunna/variable-and-function-hoisting-in-javascript-59ng</link>
      <guid>https://dev.to/arjunjunna/variable-and-function-hoisting-in-javascript-59ng</guid>
      <description>&lt;h2&gt;
  
  
  Let's understand the concept of Hoisting in functions and variables
&lt;/h2&gt;

&lt;p&gt;This is beginner-friendly content. The only prerequisite we need would be a basic understanding of variables and functions.&lt;/p&gt;

&lt;p&gt;Before we begin let’s understand how JavaScript works. Before running any program JavaScript goes through the entire program and creates a global execution context for the program. The execution context is where the JavaScript is executed. In this global execution context, we will have 2 phases.&lt;/p&gt;

&lt;p&gt;The first phase is the memory phase or also called the variable environment. JavaScript allocates memory in the memory phase. To all the variables it allocates the placeholder 'undefined' and to all the functions it allocates the function body itself.&lt;/p&gt;

&lt;p&gt;The second phase is the code execution phase. Here each line of command is executed in order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping these things in mind let's dive into Hoisting
&lt;/h2&gt;

&lt;p&gt;Hoisting is a phenomenon where you can access the variables and functions without any errors even before you have initialized them.&lt;/p&gt;

&lt;p&gt;Let's walk through the below program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a);
console.log(welcome);
welcome();
var a = 21;
function welcome() {
  console.log('Hoisting in JavaScript...');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in this program, we are accessing the variable 'a', function named 'welcome', and then we have the function call 'welcome'. We are doing all these things before we have initialized them.&lt;/p&gt;

&lt;p&gt;In any other programming language, this would result in many errors. But this is not the case in JavaScript.&lt;/p&gt;

&lt;p&gt;In JavaScript, the following lines are what you will be seeing in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--opZGCTUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nu4fbc4vf2w436zyswr4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--opZGCTUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nu4fbc4vf2w436zyswr4.jpg" alt="Console Page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's why you will see these above lines.&lt;/p&gt;

&lt;p&gt;As I mentioned in the intro, JavaScript before executing the program it goes through the whole program and allocates memory to each variable, and functions in the memory phase.&lt;/p&gt;

&lt;p&gt;To our program in the global scope -&amp;gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript allocated the keyword 'undefined' to the variable 'a' as memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For function 'welcome' it allocated the function body itself.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;JavaScript let's you access functions and variables even before they are initialized. Only function declarations are hoisted but not function expressions. Use function declarations when you want to create a function on the global scope and make it available throughout your code. Use function expressions to limit where the function is available.&lt;/p&gt;

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