<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Tejan Singh - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Tejan Singh - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 11:46:49 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/TejanSingh/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Type Error vs Reference Error in JavaScript – What's the Difference? ]]>
                </title>
                <description>
                    <![CDATA[ As a JavaScript developer, you've likely encountered different types of errors while coding. Most of the time these will be type errors or reference errors. But have you ever wondered what they mean? Have you ever tried reading about the error type s... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/type-error-vs-reference-error-javascript/</link>
                <guid isPermaLink="false">66ba2a4b4ae6042994edfa2e</guid>
                
                    <category>
                        <![CDATA[ error ]]>
                    </category>
                
                    <category>
                        <![CDATA[ error handling ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tejan Singh ]]>
                </dc:creator>
                <pubDate>Wed, 15 Feb 2023 17:12:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/pexels-kim-stiver-909256.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>As a JavaScript developer, you've likely encountered different types of errors while coding. Most of the time these will be type errors or reference errors. But have you ever wondered what they mean?</p>
<p>Have you ever tried reading about the error type specified by the interpreter before solving these errors? If not, you should, since knowing about these errors can help you solve your problems.</p>
<p>For example, when you encounter an error, you probably Google it. You copy and paste the whole error and try to find out the solution, by visiting various websites and forums. It's a trial and error method of finding and solving bugs.</p>
<p>But what if you already knew why the error generally occurs? This would save you time you'd otherwise spend searching for solutions online. Instead, you could start looking for solutions on your own just by looking at the error type.</p>
<p>In this guide, you will learn about the two mostly encountered error types, which are type error and reference error. You'll learn why they occur, the difference between them, and how to avoid getting these errors.</p>
<p>Note: There are other types of errors in JavaScript like syntax errors, internal errors, range errors, and eval errors. But the scope of this article is limited to the two most commonly occurring errors.</p>
<h2 id="heading-what-is-a-type-error">What is a Type Error?</h2>
<p>Type errors occur when you use something that is not intended to be used in that particular way. For example, using a screwdriver to hammer in a nail, instead of using a hammer.</p>
<p>Let's understand this using an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(a()) 

<span class="hljs-comment">//output</span>
Uncaught <span class="hljs-built_in">TypeError</span>: a is not a <span class="hljs-function"><span class="hljs-keyword">function</span></span>
</code></pre>
<p>Here <code>a</code> is a variable initialized with a value. You encountered an error because you tried to call a function with the variable name. A variable cannot be called as a function. Functions and variables work differently. So in this case, you got a type error. You used the <code>let</code> variable differently from its <em>type.</em> </p>
<p>This gives us a type error.</p>
<p>Solution: To resolve this error, you must refer to the variable in the console, as it is intended to be used. In this case, you pass <code>a</code> as a variable instead of a function.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(a)

<span class="hljs-comment">//output</span>
<span class="hljs-number">1</span>
</code></pre>
<p>Let's take another example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> a = <span class="hljs-number">1</span>
a = <span class="hljs-number">2</span> <span class="hljs-comment">// you reassign a const type variable again</span>

<span class="hljs-comment">//output</span>
<span class="hljs-attr">TypeError</span>: Assignment to constant variable.
</code></pre>
<p>Here, we reassigned the <code>const</code> type variable <code>a</code> to a new value. But you can't change const variables like this, so in this case you get a type error</p>
<p>Solution: never reassign a <code>const</code> type variable once you've defined it.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> a = <span class="hljs-number">1</span>
<span class="hljs-keyword">const</span> b = <span class="hljs-number">2</span>
<span class="hljs-built_in">console</span>.log(a, b)

<span class="hljs-comment">//output</span>
<span class="hljs-number">1</span> <span class="hljs-number">2</span>
</code></pre>
<p>Here's another example using an array:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> myArray = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
myArray = myArray.push(<span class="hljs-number">5</span>) <span class="hljs-comment">// reassign array</span>

<span class="hljs-comment">//output</span>
<span class="hljs-attr">TypeError</span>: Assignment to constant variable.
</code></pre>
<p>In this case, we reassigned the <code>const</code> type <code>myArray</code>. This gives us a type error because again, this kind of reassignment is against the properties of <code>const</code>. </p>
<p>Solution:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> myArray = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
myArray.push(<span class="hljs-number">5</span>) <span class="hljs-comment">// you can push new values</span>

<span class="hljs-comment">//output</span>
[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
</code></pre>
<p>You can easily push new values to the array without reassigning it. This will not give you any error. Pushing values into an array is allowed. This way you can avoid getting the error.</p>
<h3 id="heading-how-to-avoid-getting-a-type-error">How to avoid getting a type error</h3>
<p>The easiest way to prevent type errors is to avoid using the same names for different variables and functions. </p>
<p>If you use different names, you will not get confused or replace one with another, and you can easily avoid getting this error.</p>
<p>Another way is to make sure you use variables as they're intended to be used. Instead of using <code>const</code> when you need to reassign a value, use <code>let</code> (which allows this kind of change).</p>
<h2 id="heading-what-is-a-reference-error">What is a Reference Error?</h2>
<p>Reference errors occur when you are trying to refer to or use something that does not exist. For example, looking for a screwdriver in your toolbox, but it's not there.</p>
<p>Let's understand this using an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(b) <span class="hljs-comment">// undefined variable used</span>

<span class="hljs-comment">//output</span>
Uncaught <span class="hljs-built_in">ReferenceError</span>: b is not defined
</code></pre>
<p>Here, <code>a</code> is a variable initialized with a value. We encountered an error because we tried to console log the variable <code>b</code> that does not exist. We hadn't yet declared any such variable, so we got a reference error here.</p>
<p>Solution: only use a variable that you've declared to avoid getting an error.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(a) <span class="hljs-comment">// used defined variable</span>

<span class="hljs-comment">//output</span>
<span class="hljs-number">1</span>
</code></pre>
<p>Here's another example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>){
    <span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
}

<span class="hljs-built_in">console</span>.log(a)

<span class="hljs-comment">//output</span>
<span class="hljs-attr">ReferenceError</span>: a is not defined
</code></pre>
<p>In this example, we're trying to access the <code>a</code> variable of type <code>let</code> outside its block. The interpreter cannot find it outside the block. This gives us an error.</p>
<p>Solution:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>){
    <span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
    <span class="hljs-built_in">console</span>.log(a)
}


<span class="hljs-comment">//output</span>
<span class="hljs-number">1</span>
</code></pre>
<p>Using the variable inside its block or scope will not give you any error.</p>
<p>Let's take one more example, but this one can be difficult to understand.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>){
    <span class="hljs-built_in">console</span>.log(a)
    <span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
}


<span class="hljs-comment">//output</span>
<span class="hljs-attr">ReferenceError</span>: Cannot access <span class="hljs-string">'a'</span> before initialization
</code></pre>
<p><code>a</code> is still inside its scope. But we get an error. Why? Because we're trying to use the variable before we've defined it. This is not allowed and goes against the properties of the <code>let</code> variable. </p>
<p>Solution: use the <code>let</code> variable only after defining it. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>){
    <span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>
    <span class="hljs-built_in">console</span>.log(a)

}

<span class="hljs-comment">//output</span>
<span class="hljs-number">1</span>
</code></pre>
<h3 id="heading-how-to-avoid-getting-a-reference-error">How to avoid getting a reference error</h3>
<p>The easiest way to avoid getting reference errors is to refer to or access only defined variables. Only use variables that exist. </p>
<p>You can also use conditional statements and error handling to avoid running code if a variable or a function does not exist.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>You can easily debug your code when you already know how to resolve your errors. It's good to know commonly occurring errors and how to avoid them. This will save you time searching for solutions online and wasting hours trying to find a solution when a little bit of knowledge and awareness about these things can help.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Does a VPN Work? Tutorial for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ Do you worry about online security while using public Wi-Fi? Or has someone told you that you might get hacked when using an insecure connection and someone will steal all your details? Well, you might have also heard that you should use a VPN to pro... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-does-a-vpn-work/</link>
                <guid isPermaLink="false">66ba2a44c346e93df556afea</guid>
                
                    <category>
                        <![CDATA[ encryption ]]>
                    </category>
                
                    <category>
                        <![CDATA[ information security ]]>
                    </category>
                
                    <category>
                        <![CDATA[ privacy ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Security ]]>
                    </category>
                
                    <category>
                        <![CDATA[ vpn ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tejan Singh ]]>
                </dc:creator>
                <pubDate>Tue, 24 Jan 2023 00:08:44 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/01/privecstasy-CXlqHmQy3MY-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Do you worry about online security while using public Wi-Fi? Or has someone told you that you might get hacked when using an insecure connection and someone will steal all your details?</p>
<p>Well, you might have also heard that you should use a VPN to protect your online privacy. But do you have any idea what it is and how it works?</p>
<p>Don’t worry – in this article, we will go through everything that you need to know to about what and when to use a VPN and when to avoid using it. So, without any further delay, let’s get started.</p>
<h2 id="heading-what-is-a-vpn">What is a VPN?</h2>
<p>VPN stands for Virtual Private Network. It is a type of network you can connect to which will help you protect your online security and privacy.</p>
<p>A VPN acts as a tunnel through which all your data goes from your location to your destination. It's all properly encrypted and secure so that any outside party can’t see what data you are transferring.</p>
<p>There are many advantages to using VPNs, such as:</p>
<ul>
<li>Privacy</li>
<li>Anonymity</li>
<li>Security</li>
<li>Encryption</li>
<li>Masking or changing your original IP address, so others can’t track you</li>
</ul>
<p>We'll discuss these advantages and more further down in this article, but first you need to understand how a VPN works so you can use it properly.</p>
<h2 id="heading-how-does-a-vpn-work">How Does a VPN Work?</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-223.png" alt="Image" width="600" height="400" loading="lazy">
<em><a target="_blank" href="https://www.cactusvpn.com/beginners-guide-to-vpn/vpn-encryption/">Image source</a></em></p>
<p>A VPN works by routing / forwarding all your data from your laptop or phone through your VPN to the internet, rather than directly through your ISP. </p>
<p>When you use a VPN, it encrypts all your data on the client side. Then after the data is encrypted, it's passed through a VPN tunnel which others can’t access, and then it reaches the internet.</p>
<p>But before going through the VPN tunnel, the request is first sent to your ISP, but as it's encrypted, ISP can’t figure out what you are trying to access. So it forwards your request to your VPN server. Then the VPN sends the request to your desired IP address or website.</p>
<h2 id="heading-advantages-of-using-a-vpn">Advantages of Using a VPN</h2>
<p>Now let's discuss some of the advantages in more detail.</p>
<h3 id="heading-unblock-websites-amp-bypass-filters">Unblock websites &amp; bypass filters</h3>
<p>There might be scenarios where you won’t be able to access certain websites which are blocked by your office or school or college department, but you still want or need to access them. </p>
<p>These websites may include social networking sites, movie downloading websites, or any kind of media streaming websites. </p>
<p>In these cases, a VPN will help you bypass all the blocking filters and let you access the websites that you wish to access without anyone’s help and others will have no idea what you're accessing.</p>
<h3 id="heading-bypass-regional-restrictions">Bypass regional restrictions</h3>
<p>People in certain countries cannot access any websites outside their country like YouTube or Google because their government doesn't want them to use any other websites. </p>
<p>If you're in one of these places and still want to access these blocked websites, then a VPN can help by bypassing all the regional restrictions. You'll be able to access all the restricted or blocked content without letting the government know about your activity.</p>
<h3 id="heading-access-geo-blocked-websites">Access geo-blocked websites</h3>
<p>There are several websites, special offers, and services which are available for specific countries or regions. But what if you also want to take advantage of that opportunity, but it’s not accessible in your region?</p>
<p>A VPN can help you by changing your IP address which will change your location on the internet. Then you will seem to be a user from that country and you can also have all the benefits that people in that particular region are enjoying.</p>
<h3 id="heading-change-your-ip-address">Change your IP address</h3>
<p>Your ISP is tracking your every move on the internet – which websites you are visiting, the amount of time you are spending there, and when you log in and log out from a website. </p>
<p>But sometimes you may need to hide your browsing history/activity from your local network/ISP. In that case, using a VPN can help you keep all your records encrypted, and your ISP will have no idea what you are doing with your internet. All your internet browsing activity will be masked by the VPN.</p>
<h3 id="heading-online-anonymity-and-privacy">Online anonymity and privacy</h3>
<p>Everything on the internet is tracking you. Website and web servers that you use or visit know your IP and location. That can be used to their advantage and every time you visit the same website, they will know that it’s you, and they will track your usage and your behavior. This isn't necessarily a good thing since you are giving them a lot of information without knowing what. </p>
<p>A VPN can help keep your identity anonymous so you don't need to worry about identity leakage or any kind of tracking activity.</p>
<h3 id="heading-enhanced-security">Enhanced security</h3>
<p>As discussed above, using VPN can keep your identity safe and also keeps your data encrypted while you browse the internet. As a result, it enhances security and the chances that someone might hack you will be lower. </p>
<p>So, using VPN will keep you safe when you are using any public Wi-Fi or browsing websites which are not secure.</p>
<h2 id="heading-disadvantages-of-vpn">Disadvantages of VPN</h2>
<p>There are some downsides to using a VPN as well:</p>
<h3 id="heading-slows-your-connections">Slows your connections</h3>
<p>VPNs tend to slow your internet connection. As the VPN servers might be located far away from you (might be in some other geographic location or country), your data will need to travel farther across the internet and will slow your connection speed.</p>
<h3 id="heading-vpns-log-your-activities">VPNs log your activities</h3>
<p>VPNs keep logs of your activities. You heard right. Regardless of what policies they have, even if they say that they don’t keep any logs, they do. Governments have taken action against VPNs, and the VPN companies tend to deliver all the activity logs of a user in cases of international crime, terrorist activity, or hacking. </p>
<p>So – it goes without saying – make sure you don’t use VPNs for any illegal activities. Use it instead to protect yourself and your identity from malicious hackers.</p>
<h3 id="heading-specific-blockades-of-vpn-services">Specific blockades of VPN services</h3>
<p>There are many websites and streaming services like Netflix which will not allow any unusual VPN users to access their content. So, there might be many cases where your VPN will help, but there are many websites and servers which won't allow you to access them using a VPN.</p>
<h3 id="heading-cost">Cost</h3>
<p>Although there are many free VPN services which you can use, if you are planning to use VPN on regular basis then you might need to purchase a paid version. Free VPNs don’t provide good speed and the amount of data usage is also limited on a daily basis. VPNs cost around $10 to $15 per month for the premium services.</p>
<h2 id="heading-how-a-vpn-can-help-you-protect-your-online-identity">How a VPN Can Help You Protect Your Online Identity</h2>
<p>When you use the internet, the data you send or request through a web browser to any server (for example, when Google searching), along with your request, IP address (for example, your laptop or mobile) and destination IP address (like Google) first reaches your ISP. </p>
<p>The ISP monitors all your activity and then forwards your request to the destination IP address and also gets back the information in the same way.</p>
<p>All your information travels through a middle station, your ISP. They have all your history of using the internet and how you are using the internet. But when you are using a VPN, that's not the case.</p>
<p>Whenever you send any request to any website or server, instead of connecting directly to the server, it first reaches the VPN server. There, all your requests and information are encrypted and then sent forward to your desired website.</p>
<p>Your ISP is still there to monitor things. But if you're using VPN, it will automatically change the IP address of your destination to a different IP address and encrypt the destination IP address. This way, your ISP won’t be able to read it and will assume that all your requests were going to the IP address of the VPN. So it will forward all your requests to the VPN.</p>
<p>When your request or information reaches your VPN, it will be decrypted, and it will forward your request to the website you wish to access. The website or server will get the VPN request and will assume that the request is coming from that VPN server. It will allow the VPN to access the website and you'll be able to visit the website without letting your ISP know.</p>
<p>Similarly, when you download a file, all the traffic or information flows from a web server to the VPN. The VNP encrypts all the information and then forwards it to your ISP – which will still have no idea what’s going on, as the information is encrypted.</p>
<p>Finally, the info gets forwarded to your laptop or mobile. When it reaches your device, it will be decrypted, and you will be able to view the website as it's available to others.</p>
<h2 id="heading-frequently-asked-vpn-questions">Frequently Asked VPN Questions</h2>
<h3 id="heading-is-vpn-traffic-encrypted">Is VPN traffic encrypted?</h3>
<p>YES! As explained above, all the traffic passed through VPN is encrypted through various encryption algorithms like the RSA (Rivest–Shamir–Adleman) algorithm, AES (Advanced Encryption Standard), and others.</p>
<h3 id="heading-what-is-an-always-on-vpn-what-is-a-kill-switch">What is an always-on VPN? What is a kill switch?</h3>
<p>I will try to explain this concept in approachable terms. Always on VPN is a service which allows you to automatically connect to a VPN whenever you are connected to the internet. These kinds of services are used by companies which don’t want outside users to access their data and only want their employees to access their data from an outside, remote location.</p>
<p>Whenever an employee, company, or user who has access to the resources tries to access, then they need to enter valid credentials to automatically connect to the VPN. This also allows them to access all their work and resources present inside the company from an outside or remote location.</p>
<p>A VPN kill switch is another major feature offered by VPN service providers. Whenever there is a sudden or accidental loss of a VPN connection, in that case, your information might get exposed.</p>
<p>To deal with that, a VPN kill switch is used to terminate your internet connection when there is no VPN connection. This is a very useful feature for protecting your data from outside users.</p>
<p>So, when the kill switch is ON, internet connections will be terminated. But when the kill switch is OFF, then the internet will not be terminated when there is a loss of VPN connection.</p>
<h3 id="heading-is-a-vpn-necessary">Is a VPN necessary?</h3>
<p>A VPN is not strictly necessary depending on your needs and activities, but it's useful. </p>
<p>Using VPN helps protect your online security, privacy, and anonymity. It will also protect you from malicious threats and trackers when you are using an unsecured website or using any unknown wi-fi connection which might be public.</p>
<h3 id="heading-is-a-vpn-100-safe">Is a VPN 100% safe?</h3>
<p>Nothing on the internet is 100% secure. There are and will always be ways to expose services like VPNs. But using a VPN will typically help you more than it'll harm you.</p>
<h3 id="heading-is-vpn-legal-in-india">Is VPN legal in India?</h3>
<p>Yes! VPNs are legal in India and can be used freely to access any content on the internet without any restrictions. Just remember that you should not use it for any illegal activity, as there are always ways to track you regardless of what VPN service you use.</p>
<h3 id="heading-do-vpns-log-or-store-my-data">Do VPNs log or store my data?</h3>
<p>VPNs log all your data and store all information, and it might be able to share your data with government authorities. There have been many cases where VPNs say they have a no logs policy but still keep logs of users and shared them with authorities.</p>
<h3 id="heading-what-is-the-main-difference-between-a-firewall-and-a-vpn">What is the main difference between a firewall and a VPN?</h3>
<table>
 <tbody><tr>
  <td>
  <p><span>&nbsp;&nbsp;Firewall</span></p>
  </td>
  <td>
  <p><span>VPN</span></p>
  </td>
 </tr>
 <tr>
  <td>
  <ul>
   <li><span>&nbsp;&nbsp;Software or hardware
       device&nbsp;</span></li>
  </ul>
  <p><span>&nbsp;</span></p>
  </td>
  <td>
  <ul>
   <li><span>Service or Server</span></li>
  </ul>
  <p><span>&nbsp;</span></p>
  </td>
 </tr>
 <tr>
  <td>
  <ul>
   <li><span>&nbsp;&nbsp;acts as a filter to allow
       and block websites and users to access a particular website</span></li>
  </ul>
  </td>
  <td>
  <ul>
   <li><span>it encrypts the IP address and
       information from both source and destination and allows users to access
       all blocked or restricted websites and even private network information.</span></li>
  </ul>
  </td>
 </tr>
</tbody></table>

<h2 id="heading-conclusion">Conclusion</h2>
<p>VPNs definitely have their advantages and disadvantages. Organizations use them to protect their private networks and information. You can also use one to access blocked content, and to protect your privacy, anonymity and security. Using a VPN for legal activities is beneficial and adds extra security.</p>
<p>When you are not sure about using or accessing any unknown (public/private) wi-fi or unsecured untrusted website, then you should always use a VPN (free/paid). Although paid VPNs have their advantages, occasionally using free VPNs won’t harm you and will still serve the purpose.</p>
<p>And just remember – don't ever try to use a VPN to perform any illegal activities.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Event Loops in NodeJS – Beginner's Guide to Synchronous and Asynchronous Code ]]>
                </title>
                <description>
                    <![CDATA[ NodeJS is an asynchronous event-driven JavaScript runtime environment designed to build scalable network applications.  Asynchronous here refers to all those functions in JavaScript that are processed in the background without blocking any other requ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/nodejs-eventloop-tutorial/</link>
                <guid isPermaLink="false">66ba2a47c346e93df556afec</guid>
                
                    <category>
                        <![CDATA[ asynchronous programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tejan Singh ]]>
                </dc:creator>
                <pubDate>Mon, 30 Aug 2021 15:11:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/oliver-hale-2cYueJxEDz8-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>NodeJS is an asynchronous event-driven JavaScript runtime environment designed to build scalable network applications. </p>
<p>Asynchronous here refers to all those functions in JavaScript that are processed in the background without blocking any other request.</p>
<p>In this article, you will learn and understand how NodeJS works and handles all functions or requests sent to a server either <em>synchronously</em> or <em>asynchronously</em>. </p>
<h2 id="heading-what-is-an-event-loop">What is an Event Loop?</h2>
<p>You might have guessed it right – Node handles requests using an <strong>Event loop</strong> inside the NodeJS environment. But first, let's understand some basic terms which will help us understand the whole mechanism.</p>
<p>An event loop is an <strong>event-listener</strong> which functions inside the NodeJS environment and is always ready to listen, process, and output for an <em>event</em>.</p>
<p>An event can be anything from a mouse click to a keypress or a timeout.</p>
<h2 id="heading-what-are-synchronous-and-asynchronous-programming">What are Synchronous and Asynchronous programming?</h2>
<p><strong>Synchronous programming</strong> means that the code runs in the sequence it is defined. In a synchronous program, when a function is called and has returned some value, only then will the next line be executed.</p>
<p>Let's understand with this an example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> listItems = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">items</span>) </span>{
  items.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">item</span>) </span>{
    <span class="hljs-built_in">console</span>.log(item)
  })
}

<span class="hljs-keyword">const</span> items = [<span class="hljs-string">"Buy milk"</span>, <span class="hljs-string">"Buy coffee"</span>]

listItems(items)
</code></pre>
<pre><code>The output will look like <span class="hljs-built_in">this</span>:

<span class="hljs-string">"Buy milk"</span>
<span class="hljs-string">"Buy coffee"</span>
</code></pre><p>In this example, when the <code>listItems(items)</code> function is called, it will loop through the array of items. The <code>console.log(item)</code> function gets called first for the first item of the array and it prints <code>"Buy milk"</code>. Then again <code>console.log(item)</code> gets executed and this time it passes the second item of the array and prints <code>"Buy coffee"</code>.</p>
<p>So you can say that the function was executed in the <strong>sequence</strong> it was defined.</p>
<p><strong>Asynchronous programming</strong>, on the other hand, refers code that doesn't execute in sequence. These functions are performed not according to the sequence they are defined inside a program but only when certain conditions are met. </p>
<p>For example, <code>setTimeOut()</code> performs a task after a delay of a certain predefined number of miliseconds.</p>
<pre><code class="lang-js">setTimeOut(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">return</span>( <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World!"</span>) )
}, <span class="hljs-number">3000</span>)
</code></pre>
<p>These functions do not run line by line but only when they are required to run, irrespective of the function's declaration. In this case, the function  runs automatically after 3 seconds when all synchronous functions have been executed.</p>
<p><em>Note: Asynchronous functions will run and execute only after all the synchronous functions have been executed. Until then, they will be processed in the background.</em></p>
<p>If you want to learn more about NodeJS and asynchronous programming, you can refer to this <a target="_blank" href="https://www.freecodecamp.org/news/node-js-what-when-where-why-how-ab8424886e2/">article</a></p>
<p>But, how does NodeJS handle asynchronous functions in the background and run all synchronous functions first? All these mechanisms can be easily explained with the NodeJS event loop.</p>
<h2 id="heading-how-does-an-event-loop-work">How Does an Event Loop Work?</h2>
<p>Now let's see how NodeJS event loops can execute a simple synchronous program using a Nodejs event loop diagram. Then we'll examine how Node executes the program line by line.</p>
<p>As we go through this section you'll start to understand what you are seeing here:
<img src="https://www.freecodecamp.org/news/content/images/2021/08/1.PNG" alt="1" width="600" height="400" loading="lazy"></p>
<p>In the top-left corner, you have a Node file that is going to get executed. At the bottom left, you have an output terminal for the program. Then you have <em>Call stack, Node APIs and Callback queue.</em> All these together constitute the NodeJS environment.</p>
<p>For synchronous programming, you only need to focus on the call stack. This is the only part of the NodeJS environment that will be working in this case.</p>
<p>A callback stack is a data structure that you use to keep track of the execution of all functions that will run inside the program. This data structure has only one open end to add or remove top items.</p>
<p>When the program starts executing, it first gets wrapped inside an anonymous <code>main()</code> function. This is automatically defined by NodeJS. So <code>main()</code> gets pushed first to the callback stack.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/2.PNG" alt="2" width="600" height="400" loading="lazy"></p>
<p>Next, the variables <code>a</code> and <code>b</code> are created and their sum is stored in a variable <code>sum</code>. All these values are stored in memory. </p>
<p>Now, the <code>console.log()</code> is a function that is called and pushed inside the callback stack. It gets executed and you can see the output on the terminal screen.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/3.PNG" alt="3" width="600" height="400" loading="lazy"></p>
<p>After this function gets executed, it is removed from the callback stack. Then the <code>main()</code> is also removed as nothing is left to be called from the program. This is how a synchronous program gets executed.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/4.PNG" alt="4" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/5.PNG" alt="5" width="600" height="400" loading="lazy"></p>
<p>Now, let's see how asynchronous functions or programs get executed inside NodeJS. We need the callback stack, Node APIs and callback queue all together to process an asynchronous function.</p>
<p>Let's start by looking at this example:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/1-1.PNG" alt="1-1" width="600" height="400" loading="lazy"></p>
<p>As usual, when the program starts executing, first the <code>main()</code> function gets added to the callback stack. Then <code>console.log("Start")</code> is called and added to the callback stack. After processing, the output is visible on the terminal and then it gets removed from the callback stack. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/2-1.PNG" alt="2-1" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/3-1.PNG" alt="3-1" width="600" height="400" loading="lazy"></p>
<p>Now the next is the <code>setTimeOut(...Zero...)</code> function which gets added to the callback stack. </p>
<p>As this is an asynchronous function, it will <strong>not</strong> get processed in the callback stack. It is then added from the callback stack to the Node APIs where an event is registered and a callback function is set to get processed in the background. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/4-1.PNG" alt="4-1" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/5-1.PNG" alt="5-1" width="600" height="400" loading="lazy"></p>
<p>Next is the <code>setTimeOut(...Two..)</code> which also gets added to the Node API from the callback stack as it is an asynchronous function. Then another callback function gets set to be processed after a 2 second timeout in the background. Up until this point other functions can be performed. </p>
<p>This is called <strong>non-blocking</strong> behaviour where all the synchronous functions are processed and executed first and asynchronous functions are processed in the background while waiting their turn to get executed.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/6.PNG" alt="6" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/7.PNG" alt="7" width="600" height="400" loading="lazy"></p>
<p>Next, the <code>console.log("End")</code> function is called at last in the callback stack and gets processed here. You can see the output on the terminal. Now, all the synchronous functions are processed, and <code>main()</code> is removed from the callback stack.</p>
<p>In the background, all the asynchronous functions get processed and their callbacks are stored in the callback queue. The one which is processed first will be added first in the queue for execution in the callback stack.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/8.PNG" alt="8" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/9.PNG" alt="9" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2021/08/10.PNG" alt="10" width="600" height="400" loading="lazy"></p>
<p><em>Note: Asynchronous functions cannot run inside a callback stack until it gets emptied. That means that after <code>main()</code> is removed from call stack, only then can all asynchronous functions start executing.</em></p>
<p>Now, one by one they are pushed to the callback stack using the <strong>event loop</strong> and finally get executed. Each of the callback functions will print the value with the <code>console.log()</code> function getting called each time.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/11.PNG" alt="11" width="600" height="400" loading="lazy"></p>
<p>At last, these are also removed after being executed and now the callback stack is empty.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/12.PNG" alt="12" width="600" height="400" loading="lazy"></p>
<p>This is how NodeJS will execute synchronous and asynchronous funtions inside the environment and how the event loop manages to call asynchronous functions.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned the internal working of NodeJS and saw how asynchronous programs get executed. </p>
<p>Now you should understand why the two second time delay function does not block the rest of the program from executing. You also know why the zero second delay function prints the value at last after "End" prints.</p>
<p>That’s all! I hope you enjoyed reading this article and learned something new. Do share this article if you find it useful.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
