<?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: Hamza Nadeem</title>
    <description>The latest articles on DEV Community by Hamza Nadeem (@hamzanadeem1996).</description>
    <link>https://dev.to/hamzanadeem1996</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%2F448860%2F1d822147-ab58-47b3-80b3-88fae3e625a4.jpeg</url>
      <title>DEV Community: Hamza Nadeem</title>
      <link>https://dev.to/hamzanadeem1996</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamzanadeem1996"/>
    <language>en</language>
    <item>
      <title>Understanding Closures in JavaScript</title>
      <dc:creator>Hamza Nadeem</dc:creator>
      <pubDate>Thu, 17 Dec 2020 09:49:04 +0000</pubDate>
      <link>https://dev.to/hamzanadeem1996/understanding-closures-in-javascript-1d24</link>
      <guid>https://dev.to/hamzanadeem1996/understanding-closures-in-javascript-1d24</guid>
      <description>&lt;p&gt;JavaScript closures were always hard to understand because of the way they are explained. The closure is one of the most important core concepts of JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Writing in ECMAScript language without understanding closure is like writing Java without understanding classes” — Douglas Crockford, father of JSON&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In most of the examples, you will see people explaining this as a function inside another function but in reality, it is much more than this. &lt;/p&gt;

&lt;p&gt;Take a look at the code below:&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;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;addNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript is a lexical scoping language, the inheritance flows inwards. The above code uses a global scope and there is nothing to protect it against change. A variable declared outside the function is available for use within the function. You can not use a variable outside that's been declared inside a function.&lt;/p&gt;

&lt;p&gt;Now let me introduce you to closure. Look at 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="nx"&gt;closedFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;}&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;countMe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;closedFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//returns 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//returns 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scope of the function is created once and memory gets allocated for it. Processing happens until it reaches the end of the function and the memory gets released. There is no permanence gained in the exercise and whatever value created is forever lost.&lt;/p&gt;

&lt;p&gt;However, if you wrap your function within a function, it creates another scope — a sort of ring fence around your code that tells JavaScript not to destroy your function when it ends.&lt;br&gt;
The counter variable in the code above becomes the enclosed variable because it is outside the function being called (i.e. Increment). As a result, you can create an unlimited number of function instances with its own set of unique values.&lt;/p&gt;

&lt;p&gt;In a way, closures are just functions with preserved data. When you create a closure, you’re telling JavaScript to remember the state of things inside your function — and only the variables that are used are considered ‘closures’.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>What is the Prototypal Inheritance in JavaScript?</title>
      <dc:creator>Hamza Nadeem</dc:creator>
      <pubDate>Sat, 19 Sep 2020 12:15:18 +0000</pubDate>
      <link>https://dev.to/hamzanadeem1996/what-is-the-prototypal-inheritance-in-javascript-280</link>
      <guid>https://dev.to/hamzanadeem1996/what-is-the-prototypal-inheritance-in-javascript-280</guid>
      <description>&lt;p&gt;For understanding Prototypal Inheritance in JavaScript let's first understand what Inheritance is.&lt;/p&gt;

&lt;p&gt;Inheritance is one of the basic concepts of OOP (Object Oriented Programming). Inheritance is the capability of one class to inherit capabilities or properties from another class .let's take an example. We are humans, We inherit certain properties from the class 'Human' such as the ability to speak, breathe, eat, drink, etc.&lt;/p&gt;

&lt;p&gt;In JavaScript, objects have a special hidden property &lt;a href="https://dev.toas%20named%20in%20the%20specification"&gt;[Prototype]&lt;/a&gt;, that is either null or references another object. That object is called “a prototype”.&lt;/p&gt;

&lt;p&gt;When we want to read a property from an object, and it’s missing, JavaScript automatically takes it from the prototype, this is called “prototypal inheritance”.&lt;/p&gt;

&lt;p&gt;let animal = {&lt;br&gt;
  eats: true&lt;br&gt;
};&lt;br&gt;
let rabbit = {&lt;br&gt;
  jumps: true&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;rabbit.&lt;strong&gt;proto&lt;/strong&gt; = animal; &lt;/p&gt;

&lt;p&gt;alert( rabbit.eats ); // true&lt;br&gt;
alert( rabbit.jumps ); // true&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is an event loop in JavaScript?</title>
      <dc:creator>Hamza Nadeem</dc:creator>
      <pubDate>Sat, 19 Sep 2020 11:42:20 +0000</pubDate>
      <link>https://dev.to/hamzanadeem1996/what-is-an-event-loop-in-javascript-14o9</link>
      <guid>https://dev.to/hamzanadeem1996/what-is-an-event-loop-in-javascript-14o9</guid>
      <description>&lt;p&gt;The event loop is the core concept of JavaScript but for the people who just started writing code in JavaScript, they found it a bit confusing.&lt;/p&gt;

&lt;p&gt;So, what really an event loop is?&lt;/p&gt;

&lt;p&gt;The event loop is responsible for asynchronous programming in JavaScript. JavaScript is a single thread language but using some great data structures techniques, it gives us the ability of multi-threading. Let's take a look at how things work.&lt;/p&gt;

&lt;p&gt;JavaScript uses call stack to keep track of executions line by line. All the operations are pushed to the stack in order and whenever an operation is completed it is popped out of the stack.&lt;/p&gt;

&lt;p&gt;The event queue is responsible for sending new functions to the track for processing. It uses the queue data structure to maintain the correct sequence in which all operations should be sent for execution.&lt;/p&gt;

&lt;p&gt;Let's take an example of the setTimeout method. When a setTimeout operation is processed in the call stack, it is sent to the related Browser API which waits till the specified time to send this operation back in for processing to the event queue.&lt;/p&gt;

&lt;p&gt;The event loop facilitates this process; it constantly checks whether or not the call stack is empty. If it is empty, new functions are added from the event queue. If it is not, then the current function call is processed.&lt;/p&gt;

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