<?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: Nitin kalra</title>
    <description>The latest articles on DEV Community by Nitin kalra (@heynitin).</description>
    <link>https://dev.to/heynitin</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%2F871819%2F369074fd-e4a9-44f0-b513-13a7c54a52b6.png</url>
      <title>DEV Community: Nitin kalra</title>
      <link>https://dev.to/heynitin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/heynitin"/>
    <language>en</language>
    <item>
      <title>Closure and Scope in JavaScript</title>
      <dc:creator>Nitin kalra</dc:creator>
      <pubDate>Sat, 04 Jun 2022 07:11:04 +0000</pubDate>
      <link>https://dev.to/heynitin/closure-and-scope-in-javascript-503p</link>
      <guid>https://dev.to/heynitin/closure-and-scope-in-javascript-503p</guid>
      <description>&lt;p&gt;Just heard someone mentioning closure and scope and you've no idea what they are? I got you.&lt;/p&gt;

&lt;p&gt;Let's have a brief look over that they mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scope refers to the variables and functions within a block of code. &lt;/li&gt;
&lt;li&gt;Closure is when a function is able to access and use variables from outside its own scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key difference between scope and closure is that Scope refers to variables that are accessible to a certain portion of code, while closure is when a function remembers the variables from the scope in which it was created, even if that scope no longer exists.&lt;/p&gt;

&lt;p&gt;Confused? here is a simple example to show how closure works:&lt;/p&gt;

&lt;p&gt;Suppose we have a function called Add() that takes in a single number as an argument. This function returns another function that takes in a second number, adds the two numbers together, and returns the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Add(x) {
  return function(y) {
    return x + y;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we create a new variable called addFive and set it equal to the result of calling Add() with 5 as an argument, we can see that the addFive function has access to the x variable from the Add() function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addFive = Add(5);
console.log(addFive(2)); // 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because the addFive function has closure over the x variable from the Add() function. The x variable is not declared inside of the addFive function, but the function still has access to it.&lt;/p&gt;

&lt;p&gt;There are a few things to keep in mind with closures. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If a function has closure over a variable, that variable will stay in memory as long as the function is used. This can lead to memory leaks if the function is never called or is called but never removed from memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions with closure can access variables from any scope, not just the immediate scope in which they are defined. So, if a function has closure over a global variable, it can modify that variable even if it is not defined inside of the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because functions with closure can access variables from any scope, they can also access variables from any future scope. So, if a function is defined inside of another function, it will have closure over all of the variables from the outer function. This can be used to create closures that are variables in themselves:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Counter() {
  let count = 0;
  return function() {
    count++;
    return count;
  };
}

let counter = Counter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code we've a counter that can be incremented and accessed from any scope. The counter function has closure over the count variable, which means that the count variable will stay in memory as long as the counter function is used.&lt;/p&gt;

&lt;p&gt;Closures can be a powerful tool, but they should be used with caution. Because they can access variables from any scope, they can lead to unexpected results.&lt;/p&gt;

&lt;p&gt;So what are you waiting for, fire up the editor and try it all yourself.&lt;/p&gt;

&lt;p&gt;Feel free to comment with what you have learnt or any queries that you've.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is async and defer and how you can use them to load your web app faster</title>
      <dc:creator>Nitin kalra</dc:creator>
      <pubDate>Fri, 03 Jun 2022 18:34:56 +0000</pubDate>
      <link>https://dev.to/heynitin/what-is-async-and-defer-and-how-you-can-use-them-to-load-your-web-app-faster-27kd</link>
      <guid>https://dev.to/heynitin/what-is-async-and-defer-and-how-you-can-use-them-to-load-your-web-app-faster-27kd</guid>
      <description>&lt;p&gt;No one likes a slow website? do they? &lt;/p&gt;

&lt;p&gt;But the question is, how do we make our website more optimized, especially when it's dependent on large stylesheets?&lt;/p&gt;

&lt;p&gt;Well, this blog post has the answer.&lt;/p&gt;

&lt;p&gt;But first, let's understand how things work behind the scene...&lt;/p&gt;

&lt;p&gt;While loading a webpage, the browser sends a request for each and every external JavaScript file. If there are 3 external JavaScript files, then the browser will send 3 requests. This impacts the loading time of the webpage.&lt;/p&gt;

&lt;p&gt;And for that, in HTML5, we've the "async" and "defer" attributes for the "script" element.&lt;/p&gt;

&lt;p&gt;But how do they help us in improving the performance of our website?&lt;/p&gt;

&lt;p&gt;The async attribute tells the browser to load the JavaScript file asynchronously. That means, the browser does not have to wait for the file to finish downloading before it continues parsing the HTML.&lt;/p&gt;

&lt;p&gt;The defer attribute also tells the browser to load the JavaScript file asynchronously, but there's a difference. With the defer attribute, the browser downloads the file during HTML parsing, but waits to execute it until after the HTML parser has finished.&lt;/p&gt;

&lt;p&gt;Both async and defer have the same impact on the loading time of the webpage. So, which one should you use?&lt;/p&gt;

&lt;p&gt;If you use the async attribute, then the browser will execute the JavaScript code as soon as it's downloaded. This might be fine if the code is very simple. But if the code is more complex, there's a risk that it might impact the loading of other resources on the page.&lt;/p&gt;

&lt;p&gt;While if you use the defer attribute, then the browser will execute the JavaScript code only after the HTML parser has finished. So, if there are other resources that need to be loaded, the browser will finish loading those first before it executes the JavaScript code.&lt;/p&gt;

&lt;p&gt;In general, it's best to use the defer attribute. That way, you can be sure that your JavaScript code will not impact the loading of other resources on the page.&lt;/p&gt;

&lt;p&gt;Below is the syntax for both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Usual script tag
&amp;lt;script       src="myscript.js"&amp;gt;&amp;lt;/script&amp;gt;

//Script tag which is calling the resources with async
&amp;lt;script async src="myscript.js"&amp;gt;&amp;lt;/script&amp;gt;

//Script tag which is calling the resources with defer
&amp;lt;script defer src="myscript.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what are you waiting for, fire up the editor and try it all yourself.&lt;/p&gt;

&lt;p&gt;Feel free to comment with what you have learnt or any queries that you've.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>beginners</category>
      <category>html</category>
    </item>
    <item>
      <title>Session Storage vs Local Storage: what, why and how?</title>
      <dc:creator>Nitin kalra</dc:creator>
      <pubDate>Fri, 03 Jun 2022 08:25:03 +0000</pubDate>
      <link>https://dev.to/heynitin/session-storage-vs-local-storage-whats-the-difference-1g9j</link>
      <guid>https://dev.to/heynitin/session-storage-vs-local-storage-whats-the-difference-1g9j</guid>
      <description>&lt;p&gt;There are two types of storage in the web world: local storage and session storage. &lt;/p&gt;

&lt;h2&gt;But, what are they???&lt;/h2&gt;

&lt;p&gt;Local storage and session storage are two mechanisms for storing data on the client side. Local storage is similar to cookies, it stores data on the client side and persists even after the browser is closed. Session storage, on the other hand, only persists for the duration of the session (i.e., until the browser is closed).&lt;/p&gt;

&lt;p&gt;Both local storage and session storage are useful for storing small pieces of data on the client side. For example, local storage can be used to store user preferences, while session storage can be used to store data that should only be accessible for the duration of the session.&lt;/p&gt;

&lt;p&gt;Both have their own advantages and disadvantages. Let’s take a look at each one.&lt;/p&gt;

&lt;h3&gt;Local Storage&lt;/h3&gt;

&lt;p&gt;Local storage is a type of storage that is persistent. This means that the data stored in local storage will remain even after the browser is closed. The only way to remove data from local storage is to do it manually.&lt;/p&gt;

&lt;h4&gt;Advantages:&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Data is persistent, which means it can be used to store data that needs to be accessed at a later time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local storage is relatively secure since the data is stored locally and not on a server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data stored in local storage is available throughout the application, although any URL can access only and only its own local storage data &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Disadvantages:&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Since local storage is persistent, it can take up a lot of space if not used carefully. It has a data limit of 5MB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local storage is not accessible from other devices, which means it can’t be used to sync data across devices.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Session Storage&lt;/h3&gt;

&lt;p&gt;Session storage is a type of storage that is not persistent. This means that the data stored in session storage will be removed once the browser is closed.&lt;/p&gt;

&lt;h4&gt;Advantages:&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Session storage is not persistent, which means it doesn’t take up a lot of space. The data limit for this is of 5MB.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Session storage is relatively secure since the data is stored locally and not on a server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Disadvantages:&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Since session storage is not persistent, data that needs to be accessed at a later time needs to be stored in local storage or on a server. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Session storage is not accessible from other devices, which means it can’t be used to sync data across devices&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Session storage is tab bound, which means data stored in one tab would not be accessible to another tab.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's look at the syntax for each one of them.&lt;/p&gt;

&lt;p&gt;Below is the code snippet to use Session storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
const data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the code snippet to use Localstorage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Save data to localStorage
localStorage.setItem('myCar', 'Toyota');

// Get saved data from localStorage
const car = localStorage.getItem('myCar');

// Remove saved data from localStorage
localStorage.removeItem('myCar');

// Remove all saved data from localStorage
localStorage.clear();

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

&lt;/div&gt;



&lt;p&gt;So what are you waiting for, fire up the editor and try it all yourself.&lt;/p&gt;

&lt;p&gt;Feel free to comment with what you have learnt or any queries that you've.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>localstorage</category>
      <category>sessionstorage</category>
      <category>browser</category>
    </item>
  </channel>
</rss>
