<?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: hariszulfiqar054</title>
    <description>The latest articles on DEV Community by hariszulfiqar054 (@hariszulfiqar054).</description>
    <link>https://dev.to/hariszulfiqar054</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%2F328447%2F406a6c05-3f37-41cc-ad24-815a3733df43.JPEG</url>
      <title>DEV Community: hariszulfiqar054</title>
      <link>https://dev.to/hariszulfiqar054</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hariszulfiqar054"/>
    <language>en</language>
    <item>
      <title>What the hell is 'this' in javascript?</title>
      <dc:creator>hariszulfiqar054</dc:creator>
      <pubDate>Fri, 05 Jan 2024 18:56:47 +0000</pubDate>
      <link>https://dev.to/hariszulfiqar054/what-the-hell-is-this-in-javascript-2mjg</link>
      <guid>https://dev.to/hariszulfiqar054/what-the-hell-is-this-in-javascript-2mjg</guid>
      <description>&lt;p&gt;JavaScript, with its reputation for complexity, often leaves developers scratching their heads. Today, let's tackle one of its puzzling aspects – the notorious this keyword. Despite being vital, it tends to confuse even seasoned developers. Fear not, as we're about to unravel the mystery and make sense of what the heck &lt;code&gt;this&lt;/code&gt; is all about in JavaScript.&lt;/p&gt;

&lt;p&gt;Enough talk let's see how it works with some coding examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aPQf9In7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc770qanpvjnw9kfei4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aPQf9In7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc770qanpvjnw9kfei4h.png" alt="Image description" width="694" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hmm so what will be the output of this code 🤔&lt;br&gt;
The answer is pretty simple the output will be a global object, but this pops another question that what is &lt;code&gt;global object&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;The value of &lt;code&gt;global object&lt;/code&gt; depends on the environment where the javascript code will run, for example if this code runs on the browser the &lt;code&gt;global object&lt;/code&gt; value will be &lt;code&gt;window object&lt;/code&gt; and if this code runs in nodejs the value will be different and vice versa this type of behavior is called default binding of this.&lt;/p&gt;

&lt;p&gt;let's take a look on few more examples to understand it more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uoVZCKRJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqdov30viyd43u5ailcq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uoVZCKRJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqdov30viyd43u5ailcq.png" alt="Image description" width="694" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a moment and think what will be the value of &lt;code&gt;this&lt;/code&gt;?&lt;br&gt;
Yes you got it right the value will be a &lt;code&gt;global object&lt;/code&gt;. But there is another mystery in this code example, what if we execute this code example in a strict mode of javascript? &lt;br&gt;
Note: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;To read more about strict and non-strict mode&lt;/a&gt;&lt;br&gt;
The output of &lt;code&gt;this&lt;/code&gt; will be &lt;code&gt;undefined&lt;/code&gt; but why the value of &lt;code&gt;this&lt;/code&gt; is a &lt;code&gt;global object&lt;/code&gt; in non-strict mode? The answer is pretty simple it's because of the &lt;code&gt;this&lt;/code&gt; substitution in javascript which happens during the run-time.&lt;/p&gt;

&lt;p&gt;Let's unfold how this works inside the methods.&lt;br&gt;
FYI : Methods are the function which are associated with an object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xaRjcIHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obayoi9m5e2sy4hvm1m2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xaRjcIHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obayoi9m5e2sy4hvm1m2.png" alt="Image description" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's execute the code see the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zphEZVyd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbxtp5q4adlk1a7redjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zphEZVyd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbxtp5q4adlk1a7redjm.png" alt="Image description" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the value of &lt;code&gt;this&lt;/code&gt; is referring to it's parent object because the execution context is changed from global object to a javascript object this process is called &lt;code&gt;implicit binding&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What if we replace the &lt;code&gt;printName&lt;/code&gt; function with an arrow function?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S-mfYLzY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jv346dv059235e06y3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S-mfYLzY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jv346dv059235e06y3y.png" alt="Image description" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case the output of &lt;code&gt;this&lt;/code&gt; will be a &lt;code&gt;global object&lt;/code&gt; as in arrow function javascript refers the value of &lt;code&gt;this&lt;/code&gt; from the enclosing lexical context in this case the enclosing lexical context is &lt;code&gt;global object&lt;/code&gt;. Let's take another example to clear the doubts about it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kjo6t3dT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z0vwcaqh0u4uu2u5yt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kjo6t3dT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z0vwcaqh0u4uu2u5yt7.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we know that in case of arrow functions the value of &lt;code&gt;this&lt;/code&gt; is the enclosing lexical context in this case the enclosing lexical context is the function &lt;code&gt;printName&lt;/code&gt; so as a result the value of this will be the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zphEZVyd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbxtp5q4adlk1a7redjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zphEZVyd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbxtp5q4adlk1a7redjm.png" alt="Image description" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is also one more topic where you can explicitly change the value of &lt;code&gt;this&lt;/code&gt; during the runtime execution using some functions for example &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;bind&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt;. Let's leave this for another discussion. I hope you find this article helpful. :)&lt;/p&gt;

&lt;p&gt;That's pretty much you can read more about &lt;code&gt;this&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Pop the bubble of JavaScript Closures.</title>
      <dc:creator>hariszulfiqar054</dc:creator>
      <pubDate>Sat, 15 Apr 2023 22:38:00 +0000</pubDate>
      <link>https://dev.to/hariszulfiqar054/pop-the-bubble-of-javascript-closures-482n</link>
      <guid>https://dev.to/hariszulfiqar054/pop-the-bubble-of-javascript-closures-482n</guid>
      <description>&lt;p&gt;As a beginner in the world of JavaScript, you might have heard a lot about closures - those mysterious and somewhat intimidating creatures that seem to lurk in the shadows of your code. You might have heard stories of developers pulling their hair out, spending hours upon hours trying to debug an issue caused by a closure. But fear not, my friend, for closures are not as scary as they may seem!&lt;/p&gt;

&lt;p&gt;In fact, closures are one of the most powerful features of JavaScript, allowing us to create functions that can retain access to variables that are defined outside of their own scope. This means that we can create functions that can access and manipulate variables from a higher scope, even after that higher scope has been closed.&lt;/p&gt;

&lt;p&gt;Think of it like a treasure chest - once you close the lid, you might think that the treasure inside is out of reach. But with closures, you can create a secret key that allows you to open the chest and access the treasure even after it's been closed. Pretty cool, right?&lt;/p&gt;

&lt;p&gt;Now, don't get me wrong - closures can be a bit tricky to wrap your head around at first. But once you understand how they work, you'll be able to create more powerful and flexible functions in your JavaScript code. So don't be afraid to dive in and explore the world of closures - who knows what kind of treasure you might find!&lt;/p&gt;

&lt;p&gt;Before diving deep into closure let's understand what is lexical scope and how it relates to closures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Scope :
&lt;/h2&gt;

&lt;p&gt;Lexical scope is the set of rules that determines how a variable is accessed within a program. In JavaScript, when a function is defined, it creates its own scope, which is known as the local scope. This local scope contains all of the variables and functions that are defined within that function.&lt;/p&gt;

&lt;p&gt;Let's see a code example of what the lexical scope of a variable looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a4MBlw5_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5osbmsyao0zj1u8dmfx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a4MBlw5_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5osbmsyao0zj1u8dmfx9.png" alt="Image description" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have some idea about the lexical scope let's deep dive into the closure world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closures :
&lt;/h2&gt;

&lt;p&gt;What the hell is a closure? The answer is very simple, function with its lexical scope forms a closure.&lt;/p&gt;

&lt;p&gt;Let's see a simple code example to understand the concept of closure better. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qWKpWy4H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wbhbxab3ki67oxv2z56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qWKpWy4H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wbhbxab3ki67oxv2z56.png" alt="Image description" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a deep breath and look at the code. What will be the output? Does the code throw an error that the variable is not found? Let me break that bubble for you. The output will be &lt;code&gt;hello world&lt;/code&gt; printed on the console. Pretty annoying right? No that's the beauty of javascript closures and you can utilize this pattern to write powerful functional programming code.&lt;/p&gt;

&lt;p&gt;Let me show a real-life example to understand it better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n_vqEIVu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrxwi7e3b7h9fpk64nzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n_vqEIVu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrxwi7e3b7h9fpk64nzc.png" alt="Image description" width="491" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How we will look for &lt;code&gt;room 5&lt;/code&gt;? We will go to the ground floor we will not find &lt;code&gt;room 5&lt;/code&gt; then we will go to the 1st floor we will not find &lt;code&gt;room 5&lt;/code&gt; then we will go to the 2nd floor where we will find &lt;code&gt;room 5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's map this on the code example&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U5L4RQwg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qaio70nk4qhtmj9of3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U5L4RQwg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qaio70nk4qhtmj9of3d.png" alt="Image description" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The output will be &lt;code&gt;room5&lt;/code&gt; printing on the console.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it's working ?&lt;/strong&gt;&lt;br&gt;
The room service function is looking for the &lt;code&gt;room5&lt;/code&gt; variable. It will start looking at its current local scope (&lt;code&gt;roomService function&lt;/code&gt;) if the variable is not found it will jump to the parent scope (&lt;code&gt;groundFloor function&lt;/code&gt;) if the variable is not found it will jump to one level more parent scope (&lt;code&gt;firstFloor function&lt;/code&gt;) if the variable is not found it will jump to one level more parent scope (&lt;code&gt;scopeBuilding function&lt;/code&gt;) where it will find the &lt;code&gt;room5&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Now you have a clear understanding of how closures work behind the scenes let's look at some advanced patterns of closures that you will see in the code that you have written but maybe don't know about it.&lt;/p&gt;

&lt;p&gt;Let's look at a code example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m4wmckMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxh5derewk9lwbl44m4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m4wmckMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxh5derewk9lwbl44m4e.png" alt="Image description" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see the code I created a function &lt;code&gt;adder&lt;/code&gt; which is returning another function which is utilizing the value of &lt;code&gt;adder&lt;/code&gt; function and adding that value to whatever value is passed to that function. I called the function &lt;code&gt;adder&lt;/code&gt; with &lt;code&gt;2&lt;/code&gt; and store the returned value of the &lt;code&gt;adder&lt;/code&gt; function which is also a function into a variable called &lt;code&gt;twoAdder&lt;/code&gt;. When I called the &lt;code&gt;twoAdder&lt;/code&gt; function with &lt;code&gt;5&lt;/code&gt; value the output printed on the console is &lt;code&gt;7&lt;/code&gt; but why is that? Because the &lt;code&gt;twoAdder&lt;/code&gt; function is creating a closure and holds the lexical scope of its parent function even after its execution is ended. Same case with the &lt;code&gt;fiverAdder&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Now you have a pretty good understanding of how closures are created and how it works now you can use and point out this pattern in your code pretty easily.&lt;/p&gt;

&lt;p&gt;Previous Article :&lt;br&gt;
&lt;a href="https://dev.to/hariszulfiqar054/javascript-hoisting-behind-the-scenes-5b6j"&gt;https://dev.to/hariszulfiqar054/javascript-hoisting-behind-the-scenes-5b6j&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript hoisting behind the scenes.</title>
      <dc:creator>hariszulfiqar054</dc:creator>
      <pubDate>Sat, 18 Mar 2023 16:43:37 +0000</pubDate>
      <link>https://dev.to/hariszulfiqar054/javascript-hoisting-behind-the-scenes-5b6j</link>
      <guid>https://dev.to/hariszulfiqar054/javascript-hoisting-behind-the-scenes-5b6j</guid>
      <description>&lt;p&gt;Do you ever wonder how the javascript engine works behind the scene and how it manages variable hoisting? If not no worries we will dive deep into it and see how all this magic happens behind the scene.&lt;/p&gt;

&lt;p&gt;First, let's understand how javascript code runs. Consider the following code to start things.&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%2Fuploads%2Farticles%2Fer8j0p3qmiz27ngxde4f.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%2Fuploads%2Farticles%2Fer8j0p3qmiz27ngxde4f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although there is a myth that javascript is interpreted language that's incorrect, Javascript compiles the code before the execution. Javascript engine is v8 in the case of chrome tokenized the code and converted it into the machine understandable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code execution&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the compiler sees &lt;code&gt;var a = 2;&lt;/code&gt; it creates memory space with the label &lt;code&gt;a&lt;/code&gt; in the call stack and assigned the value &lt;code&gt;undefined&lt;/code&gt; to it as a placeholder. After the compilation ends &lt;code&gt;undefined&lt;/code&gt; during the runtime value is replaced with &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When the &lt;code&gt;console.log(a);&lt;/code&gt; is executed during the runtime that's how the conversation between the engine and execution context looks like.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Engine : Hey context! do you know a variable named &lt;code&gt;a&lt;/code&gt;?&lt;br&gt;
Execution context : Hey! Yes I have a variable with &lt;code&gt;a&lt;/code&gt; name.&lt;br&gt;
Engine : Great! I have a LHS reference of the variable &lt;code&gt;a&lt;/code&gt;. Can you provide me with the RHS of it?&lt;br&gt;
Execution context : Yes no problem , there you go.&lt;/p&gt;

&lt;p&gt;Result in the console: Displays 2. &lt;/p&gt;

&lt;p&gt;Now you have a bit of an understanding of how javascript code run and how it interacts with the call stack.&lt;/p&gt;

&lt;p&gt;As you all know there are several ways you can declare and initialize the variables. Let's look at the difference and understand the difference between them.&lt;/p&gt;

&lt;p&gt;Let's look at the &lt;code&gt;var&lt;/code&gt; first.&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%2Fuploads%2Farticles%2F9fdfif4rob25k9k3duw5.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%2Fuploads%2Farticles%2F9fdfif4rob25k9k3duw5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider the above code what should be the expected output take a minute and think......... is it an error or it outputs the value &lt;code&gt;2&lt;/code&gt;?&lt;br&gt;
Surprisingly &lt;code&gt;undefined&lt;/code&gt; will be printed on the browser console but why is that? As we discussed above before the execution javascript engine tokenizes the code and compiles it so during the compilation time a namespace labeled &lt;code&gt;variable&lt;/code&gt; will be created in the call stack with the placeholder &lt;code&gt;undefined&lt;/code&gt;. When the javascript engine runs the code &lt;code&gt;console.log(variable);&lt;/code&gt; line occurs before the &lt;code&gt;variable&lt;/code&gt; declaration and at that time call stack has the placeholder value &lt;code&gt;undefined&lt;/code&gt; for the &lt;code&gt;variable&lt;/code&gt; so the console output that value &lt;code&gt;undefined&lt;/code&gt; and after that when the javascript engine reaches to &lt;code&gt;var variable = 2;&lt;/code&gt; line it replaces the &lt;code&gt;undefined&lt;/code&gt; value with the value &lt;code&gt;2&lt;/code&gt; but at that time &lt;code&gt;console.log(variable);&lt;/code&gt; is already executed.&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords to initialize and declare variables in javascript and let's look at these and how these are different from &lt;code&gt;var&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%2Fuploads%2Farticles%2Fh0ksol7r7w5yynpun7h0.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%2Fuploads%2Farticles%2Fh0ksol7r7w5yynpun7h0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider the above code what should be the expected output take a minute and think again......... is it an error or it outputs the value &lt;code&gt;hello world!!!&lt;/code&gt; or it outputs the value &lt;code&gt;undefined&lt;/code&gt;?&lt;br&gt;
So the wait is over the output will be &lt;code&gt;Reference error&lt;/code&gt;. But why is that? isn't should display &lt;code&gt;undefined&lt;/code&gt;? Javascript engine starts to tokenize the code during the compilation time when the javascript engine sees the variable declared with let keyword i.e &lt;code&gt;let helloworld = 'hello world!!!';&lt;/code&gt; it creates a memory space in the temporal dead-zone not in the call stack and the temporal dead-zone is not accessible to the execution context so instead of &lt;code&gt;undefined&lt;/code&gt; reference error is thrown by the engine. &lt;/p&gt;

&lt;p&gt;As you have heard that the let is blocked scope and var is function scope. Let's look at it with an example.&lt;/p&gt;

&lt;p&gt;Consider the two same code blocks the only difference is the variable initialization. &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%2Fuploads%2Farticles%2Fqbviw20ppyfy9ua06et9.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%2Fuploads%2Farticles%2Fqbviw20ppyfy9ua06et9.png" alt="Image description"&gt;&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%2Fuploads%2Farticles%2Fjy6g8rzhrttw3l1n8sf7.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%2Fuploads%2Farticles%2Fjy6g8rzhrttw3l1n8sf7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What will be the output of the first code block? Think about it.....Think hard....Alright so the output will be &lt;code&gt;10&lt;/code&gt; because the variable declared with &lt;code&gt;var&lt;/code&gt; is scoped with the function in which the variable is declared. And there is a drawback of using &lt;code&gt;var&lt;/code&gt; because as we see it is polluting the scope and it can create conflicts if we use variable &lt;code&gt;i&lt;/code&gt; in another place. And the execution of &lt;code&gt;for loop&lt;/code&gt; is completed so there is no need for the variable &lt;code&gt;i&lt;/code&gt; but due to the &lt;code&gt;var&lt;/code&gt; behavior, it's still there and accessible. &lt;/p&gt;

&lt;p&gt;If we look at the second code example what will be the output take a deep breath and think......Alright, I will tell you. The output will be a &lt;code&gt;Reference error&lt;/code&gt; but why is that ? As we see the &lt;code&gt;let&lt;/code&gt; variable limits itself within its scope. And after the &lt;code&gt;for loop&lt;/code&gt; execution the garbage collector frees up the memory so no &lt;code&gt;i&lt;/code&gt; variable exists after the &lt;code&gt;for loop&lt;/code&gt; block scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: The let and const are the same thing the only difference is that const cannot be reassigned.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep following I will be adding more blogs related to javascript.&lt;/p&gt;

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