<?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" version="2.0"><channel><title><![CDATA[Blog by Rishikesh]]></title><description><![CDATA[Hello, 👋  this is Rishikesh Shinde. I am Computer Engineering aspirant and web app enthusiast.]]></description><link>https://blog.rdshinde.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1630562196414/wA0ypBC8V.png</url><title>Blog by Rishikesh</title><link>https://blog.rdshinde.com</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 18:14:51 GMT</lastBuildDate><atom:link href="https://blog.rdshinde.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Use of Javascript Closures in React App]]></title><description><![CDATA[Introduction
Picture yourself enjoying your favourite latte at a coffee shop. Sitting at a corner table, you overhear two friends having a conversation. One of them shares a confidential secret with the other and asks them to keep it to themselves. L...]]></description><link>https://blog.rdshinde.com/use-of-javascript-closures-in-react-app</link><guid isPermaLink="true">https://blog.rdshinde.com/use-of-javascript-closures-in-react-app</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><category><![CDATA[closures in javascript]]></category><category><![CDATA[javascript closure in simepe terms]]></category><dc:creator><![CDATA[Rishikesh Shinde]]></dc:creator><pubDate>Sun, 24 Sep 2023 13:53:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1695563342082/802d8db2-2c2a-4801-b851-59f7be7b5c9c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>Picture yourself enjoying your favourite latte at a coffee shop. Sitting at a corner table, you overhear two friends having a conversation. One of them shares a confidential secret with the other and asks them to keep it to themselves. Later, when you talk to a friend who was also present during the conversation, they still remember the secret and keep it private. This scenario is similar to how closures work in JavaScript. Like how the friend remembers the secret even after the conversation ends, closures allow functions to access variables from their surrounding scope, even after that scope has finished executing. This powerful concept is essential in many JavaScript and React applications, enabling them to efficiently manage data, handle events, and create encapsulated functionality.</p>
<p>In the world of JavaScript and React, numerous concepts and techniques can make your code more efficient and flexible. One such concept that often puzzles developers but proves to be incredibly useful is <strong>JavaScript closures</strong>. In this blog post, we'll demystify closures, explore their real-world use cases in React, and illustrate their importance through practical examples.</p>
<h3 id="heading-understanding-javascript-closures"><strong>Understanding JavaScript Closures</strong></h3>
<p>To fully understand closures, it is crucial to comprehend the idea of lexical scope. In JavaScript, functions possess access to variables from their surrounding scope. If a function is defined within another function, it creates a closure, allowing the inner function to access the outer function's variables and parameters, even after the outer function has finished executing.</p>
<p>Here's a simple example:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">outer</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> outerVar = <span class="hljs-number">10</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">inner</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(outerVar); <span class="hljs-comment">// Accesses outerVar from the outer function</span>
  }

  <span class="hljs-keyword">return</span> inner;
}

<span class="hljs-keyword">const</span> closureFunc = outer();
closureFunc(); <span class="hljs-comment">// Outputs 10</span>
</code></pre>
<p>In this example, <code>inner</code> is a closure because it maintains access to the <code>outerVar</code> variable even after <code>outer</code> has finished executing. This is incredibly useful in React for various scenarios.</p>
<h3 id="heading-use-cases-of-closures-in-react"><strong>Use Cases of Closures in React</strong></h3>
<h4 id="heading-1-event-handlers-with-closures"><strong>1. Event Handlers with Closures</strong></h4>
<p>Closures can be used effectively in React to handle events with dynamic data. Consider a scenario where you want to create a list of buttons, each with its unique value:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ButtonList</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> values = [<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>];

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {values.map((value) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{value}</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> handleButtonClick(value)}&gt;
          Button {value}
        <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>In this example, the <code>onClick</code> handler creates a closure around <code>value</code>, ensuring that each button knows its corresponding value when clicked.</p>
<h4 id="heading-2-private-data-and-encapsulation"><strong>2. Private Data and Encapsulation</strong></h4>
<p>JavaScript closures are also useful for creating private variables and encapsulating data within React components. For instance, consider building a counter component with a private counter variable:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">return</span> {
    <span class="hljs-attr">increment</span>: <span class="hljs-function">() =&gt;</span> {
      count++;
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Count: <span class="hljs-subst">${count}</span>`</span>);
    },
    <span class="hljs-attr">decrement</span>: <span class="hljs-function">() =&gt;</span> {
      count--;
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Count: <span class="hljs-subst">${count}</span>`</span>);
    },
  };
}

<span class="hljs-keyword">const</span> myCounter = Counter();
myCounter.increment(); <span class="hljs-comment">// Outputs: Count: 1</span>
myCounter.decrement(); <span class="hljs-comment">// Outputs: Count: 0</span>
</code></pre>
<p>In React, you can achieve similar encapsulation by using closures to maintain the component state and hide it from the outside world.</p>
<h4 id="heading-3-memoization-for-performance-optimization"><strong>3. Memoization for Performance Optimization</strong></h4>
<p>Caching expensive function results using memoization is made possible with closures. In React, you can use closures within functional components to significantly improve performance.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ExpensiveCalculation</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> cache = {};

  <span class="hljs-keyword">return</span> <span class="hljs-function">(<span class="hljs-params">input</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (input <span class="hljs-keyword">in</span> cache) {
      <span class="hljs-keyword">return</span> cache[input];
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">const</span> result = <span class="hljs-comment">/* Perform expensive calculation */</span>;
      cache[input] = result;
      <span class="hljs-keyword">return</span> result;
    }
  };
}
</code></pre>
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>In React development, JavaScript closures can be a valuable asset. With closures, you can efficiently create encapsulated and maintainable code for a variety of real-world scenarios. This includes handling dynamic data in event handlers, encapsulating private data, and optimizing performance through memoization. By embracing closures, you can unlock their potential and enhance your React projects.</p>
]]></content:encoded></item><item><title><![CDATA[Event Delegation in Javascript]]></title><description><![CDATA[Introduction
In layman's language, event delegation can be defined as a way to add an event listener to multiple elements with the help of parent elements.
Let’s take an example in real life. We often hear the news that this country's delegation went...]]></description><link>https://blog.rdshinde.com/event-delegation-in-javascript</link><guid isPermaLink="true">https://blog.rdshinde.com/event-delegation-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[interview]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[js]]></category><dc:creator><![CDATA[Rishikesh Shinde]]></dc:creator><pubDate>Mon, 17 Oct 2022 17:23:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666027197935/Et0PVPpF5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In layman's language, event delegation can be defined as a way to add an event listener to multiple elements with the help of parent elements.</p>
<p>Let’s take an example in real life. We often hear the news that this country's delegation went to another country and made some agreements with different ministries. Now this delegation contains diplomats from different ministries that are our <code>multiple elements</code> and the delegation itself is the <code>parent element</code> 
Here multiple agreements are our <code>event listeners.</code> These multiple events are made using a single element that is <code>the delegation</code> which is the <code>parent element</code>. </p>
<h2 id="heading-explanation">Explanation</h2>
<p>Now, if you can relate our event delegation in javascript with a real-life delegation of countries, then it’s simple to understand the concept of <code>event delegation</code>. </p>
<p>Let’s dive into some code for practical understanding. 
Below is the HTML document with the parent element and its children elements.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"nav_links"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> Home <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> About <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> Contact <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav_login_btn"</span>&gt;</span>Login<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
</code></pre>
<p>In this example, our <code>nav_links</code> as the parent element and all <code>li</code> tags will be children. Now if we want to add event listeners to each <code>li</code> then we can simply give a <code>class</code> or an <code>id</code> to each <code>li</code> and can access it using <code>querySelector</code> and then can add an event listener explicitly. But we are going to make our life simple. We will only access the parent element and then apply an event listener on it directly. Like the delegation in the above example. And then based on some conditions, we can perform some operations separately on each child.</p>
<p>Let’s take a look at the code.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> list = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#nav_links"</span>);<span class="hljs-comment">// accessed parent element</span>

list.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>{  <span class="hljs-comment">// Added event listener on parent element</span>
    <span class="hljs-keyword">const</span> target = e.target;
    <span class="hljs-keyword">if</span>(target.matches(<span class="hljs-string">"li"</span>)){
            <span class="hljs-keyword">if</span>(target.innerText === <span class="hljs-string">"Home"</span>){
                    target.style.color = <span class="hljs-string">"red"</span>;
                }
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(target.innerText === <span class="hljs-string">"About"</span>){
                    target.style.color = <span class="hljs-string">"blue"</span>;
                }
            <span class="hljs-keyword">else</span>{
                    target.style.color = <span class="hljs-string">"green"</span>;
                }
        }
})
</code></pre>
<p>Here <code>matches</code> is the method on the target object which helps to find the child element. It simply returns <code>true or false.</code></p>
<p>So this is our delegation of the event which performs different operations based on conditions on different elements.</p>
<p>Have a look at the visuals of the above code.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666026789763/WAl706MR1.gif" alt="Screen Recording 2022-10-17 at 10.42.23 PM.gif" /></p>
<h2 id="heading-more-action">More Action</h2>
<p>We can also perform operations like appending or deleting the child element.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> list = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#nav_links"</span>); <span class="hljs-comment">// accessed parent element</span>

<span class="hljs-keyword">const</span> newElement = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">"li"</span>); <span class="hljs-comment">// Created new element</span>
newElement.innerText = <span class="hljs-string">"Privacy Policy"</span>;         <span class="hljs-comment">// Added inner text to element</span>

list.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>{   <span class="hljs-comment">// Added event listener on parent element</span>
    <span class="hljs-keyword">const</span> target = e.target;
    <span class="hljs-keyword">if</span>(target.matches(<span class="hljs-string">"li"</span>)){
            <span class="hljs-keyword">if</span>(target.innerText === <span class="hljs-string">"Home"</span>){
                    target.style.color = <span class="hljs-string">"red"</span>;
                }
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(target.innerText === <span class="hljs-string">"About"</span>){
                    target.style.color = <span class="hljs-string">"blue"</span>;
                } 
            <span class="hljs-keyword">else</span>{
                    target.style.color = <span class="hljs-string">"green"</span>;  
          list.appendChild(newElement); <span class="hljs-comment">// New Element inserted here.</span>
                }
        }
})
</code></pre>
<p>Here we are adding a new list element which is the <code>Privacy Policy.</code></p>
<p>Have a look at the visuals of the above code.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666026668386/O1CVBGXk4.gif" alt="Screen Recording 2022-10-17 at 10.40.01 PM.gif" class="image--center mx-auto" /></p>
<h2 id="heading-summary">Summary</h2>
<p>So, in this article, we saw how useful event delegation is. </p>
<p>It is basically :</p>
<ul>
<li>A way to add an event listener to multiple elements using a single parent element.</li>
<li>It can be used to add child elements or to remove child elements.</li>
<li>It has a single event listener on the parent element.</li>
<li>Based on different conditions it performs actions on child elements.</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[== Vs === in Javascript.]]></title><description><![CDATA[Many times people coming from a different programming language background get confused between == and === operators that we use in JavaScript. This also happens with != and !== operators also. So by the end of this post, you will be able to explain t...]]></description><link>https://blog.rdshinde.com/equality-oprators-in-javascript</link><guid isPermaLink="true">https://blog.rdshinde.com/equality-oprators-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[coding]]></category><category><![CDATA[basics]]></category><dc:creator><![CDATA[Rishikesh Shinde]]></dc:creator><pubDate>Fri, 03 Sep 2021 14:13:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630672038257/wYr4V8f8X.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Many times people coming from a different programming language background get confused between == and === operators that we use in JavaScript. This also happens with != and !== operators also. So by the end of this post, you will be able to explain the difference between these operators. So let's get started.</p>
<p>Let's first see, what are comparison operators?</p>
<blockquote>
<p>A comparison operator compares its operands and returns a logical value based on whether the comparison is true.</p>
</blockquote>
<p>Wait, but the definition doesn't stop here. It comes with some predefined conditions such as:</p>
<ul>
<li><p>The operands can be numerical, string, objects or logical values.</p>
</li>
<li><p>Strings are compared based on standard lexicographical ordering, using Unicode values.</p>
</li>
<li><p>If the two operands are different for example, one is numerical and another one is a string then JavaScript attempts to convert them to an appropriate type for the comparison. </p>
</li>
<li><p>And this behaviour generally results in comparing the operands numerically.</p>
</li>
</ul>
<p>And now the important one,</p>
<ul>
<li>The sole exceptions to type conversion within comparison involve the <code>===</code> and <code>!==</code> operators.</li>
</ul>
<p>So far we know that <code>===</code> and <code>!==</code> are the exceptions in comparison operators. Now, let's understand why they are exceptions and what is the difference between <code>==</code> and <code>===</code> of between <code>!=</code> and <code>!==</code>.</p>
<h3 id="understanding"><strong> Understanding === </strong></h3>
<p>Let's consider a variable,</p>
<pre><code><span class="hljs-keyword">const</span> age = <span class="hljs-number">18</span>;
<span class="hljs-keyword">if</span>(age === <span class="hljs-number">18</span>) <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`You are an adult. 🧑 `</span>);
</code></pre><p>Now, how does this equality operator here in if condition actually works?</p>
<p>Well, just like any other comparison operator it is also a comparison operator so, it will return a true or a false value, so a boolean value, as a result, in case both sides are the same.
Let's actually show that to you.
If I write in console,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630674623643/hF5LnSaFy.gif" alt="Screen Recording 2021-09-03 at 6.38.43 PM.gif" /></p>
<p>and that's what we use in if condition.</p>
<h3 id="understanding"><strong>Understanding == .</strong></h3>
<p>Now, besides <code>===</code> we also have <code>==</code> as an equality operator. And this also works as a comparison operator. Have a look.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630675194320/Ot9xYj03X.gif" alt="Screen Recording 2021-09-03 at 6.47.38 PM.gif" /></p>
<h3 id="difference-between-and"><strong>Difference between == and === .</strong></h3>
<p>So the difference is that this one here <code>===</code>  with triple equal is called the strict equality operator. It is strict because it does not perform type conversion.</p>
<p><strong>So, it returns the true value when both the values are the same.</strong></p>
<p>See the example here,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630675986629/ZX0ckR-Yg.gif" alt="Screen Recording 2021-09-03 at 7.01.15 PM.gif" /></p>
<p>On the other hand, there is a loose equality operator with only two <code>==</code> signs. And the loose equality operator actually does the type conversion. So let's see that again in console,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630676298789/--NEomV16.gif" alt="Screen Recording 2021-09-03 at 7.06.54 PM.gif" />
In this case, we can do <code>"18"</code> the string equal to the <code>18</code> the number and it will still give us <code>true</code>.</p>
<p>So, again the <code>==</code> does not perform type conversion. So what that means is that this <code>"18"</code>the string here will be converted to the number and then the number <code>18</code> is just the same as that of the number <code>18</code> on the left-hand side.</p>
<h3 id="difference-between-and"><strong> Difference between != and !== .</strong></h3>
<p>So, as we have talked about the equal-to operators, let's also talk about the inequality operators. </p>
<p>Here we have again two inequality operators one is <code>!=</code> and the other one is <code>!==</code>. So, <code>!=</code> is a loose inequality operator which supports type conversion and <code>!==</code> is a strict inequality operator which does support type conversion. Let's see it again in the console</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630751968015/B5aEgFHYP.gif" alt="Screen Recording 2021-09-04 at 4.00.47 PM.gif" /></p>
<p>I hope that's not too confusing to you right now. So for comparing values always use the <code>===</code> or <code>!==</code> operator to avoid bugs in your code.</p>
<h3 id="wrapping-up"><strong>Wrapping Up!</strong></h3>
<ul>
<li><p>The sole exceptions to type conversion within comparison involve the <code>===</code> and <code>!==</code> operators.</p>
</li>
<li><p><code>===</code> and <code>!==</code> are strict comparisons operators which do not support type conversion.</p>
</li>
<li><p><code>==</code> and <code>!=</code> are loose comparisons operators which support type conversion.</p>
</li>
<li><p>Using strict comparison operators in code always helps in avoiding errors.</p>
</li>
</ul>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[How do we define JavaScript?]]></title><description><![CDATA[On the internet, we come across many definitions of JavaScript. But today we are going to define JavaScript differently. 
I want to start with a definition that you might have learned, where it says that, 

JavaScript is a high-level, object-oriented...]]></description><link>https://blog.rdshinde.com/how-do-we-define-javascript</link><guid isPermaLink="true">https://blog.rdshinde.com/how-do-we-define-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Object Oriented Programming]]></category><dc:creator><![CDATA[Rishikesh Shinde]]></dc:creator><pubDate>Thu, 02 Sep 2021 11:43:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630571647143/5dSfBFljM.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>On the internet, we come across many definitions of JavaScript. But today we are going to define JavaScript differently. </p>
<p>I want to start with a definition that you might have learned, where it says that, </p>
<blockquote>
<p>JavaScript is a high-level, object-oriented, multi-paradigm programming language.</p>
</blockquote>
<p>Now, this is of course 100% correct, but this is just a surface, the tip of the iceberg. So what about this definition,</p>
<blockquote>
<p>JavaScript is a high-level, prototype-based object-oriented, multi-paradigm, interrupted or just-in-time compiler, dynamic, single-threaded, garbage-collected programming language with first-class functions and a non-blocking event loop concurrency model. </p>
</blockquote>
<p>You might think is this some kind of joke, but I just packed as much information as possible about JavaScript into one unreadable sentence just for fun. But besides fun, this also gives us a great opportunity to unpack all of these concepts to get the first peek into all this stuff that we are going to learn to define JavaScript. I just want you to get the big picture before really getting started and also introduced some topics that are just good to know because getting the big picture before diving deep is in my opinion is amazing learning technique. So with that being said let’s unpack this now a little bit. </p>
<h3 id="high-level-programming-language"><strong> High-level programming language.</strong></h3>
<p>Starting with a high-level part as you might already know every program that runs on your computer needs some hardware resources such as a memory card and CPU to do with work.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630572914205/nuB5R36rZ.png" alt="1.png" /></p>
<p> Now, there are low-level languages such as <code>C</code> where you have to manually manage these resources. For example, asking the computer for memory to create a new variable.
On the other side, you have high-level languages such as <code>JavaScript</code> and <code>Python</code> where we do not have to manage resources at all. Because these languages have so-called abstractions that take all of the work away from us. This makes the language easier to learn but the downside is the program will never be as fast or as optimised as the <code>C</code> program.</p>
<p>Now one of the powerful tools that take memory management away from us developers is garbage collection. This is basically an algorithm inside the JavaScript engine which automatically removes all unused objects from the computer memory in order not to clog it up with unnecessary stuff.</p>
<p>So it’s a little bit like JavaScript has a cleaning guy, who cleans our memory from time to time, so that we don’t have to do it manually in our code.</p>
<h3 id="interpreted-or-just-in-time-compiled-language"><strong> Interpreted or just in time compiled language.</strong></h3>
<p>Computer's processors only understand 0s and 1s, that’s right. Ultimately every single program needs to be written in zeros and ones which is also called machine code and since that is not really practical to write, is it? We simply write human-readable JavaScript code which is an abstraction over machine code. But this code eventually needs to be translated to the machine code and that step can be either compiling or interpreting.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630574016132/5BoJCtlKf.png" alt="2.png" /></p>
<p>This step is necessary for every single programming language because no one writes machine code manually. In the case of JavaScript, this happens inside the JavaScript engine.</p>
<h3 id="multi-paradigm"><strong> Multi-paradigm.</strong></h3>
<p>Now, one of the things that makes JavaScript so popular is the fact that it's a multi-paradigm language. In programming, a paradigm is an approach and an overall mindset of structuring our code, which will ultimately direct the coding style and technique in a project that uses a certain paradigm. And this definition sounds kind of abstract. But don’t worry it will become more clear as we move on. </p>
<p>Now, three popular paradigms are, <code>Procedural</code>, <code>Object-oriented</code>, <code>Functional programming</code>. </p>
<p>Procedural programming is basically just organizing the code in a very linear way and then with some functions in between. </p>
<p>About object-oriented programming and functional programming I will talk about them in a second. Also, we can classify the paradigm as imperative or as declarative.</p>
<p>Now, many languages are only procedural or only object-oriented or only functional but JavaScript does all of it, so it's really flexible and versatile. And so we can do really whatever we want with it. It’s our choice we can use whatever paradigm we want.</p>
<p>So about the object-oriented nature of JavaScript, it is a prototype-based object-oriented language. What does that mean? Well, first almost everything in JavaScript is an object except for primitive values such as numbers, strings, etc. But arrays for example are just objects.</p>
<p>Have you ever wondered why we can create an array then use the push method on it? Well, it’s because of prototypal inheritance. Basically, we create arrays from an array's blueprint, which is like a template. This is called the prototype.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630575148357/4wvaQNXOB.png" alt="3.png" /></p>
<p>This prototype contains all the array methods and the arrays that we create in our code then inherit the methods from the blueprint. So that we can use them on the arrays and this, what I just explained to you is actually a huge oversimplification which might still sound confusing.</p>
<h3 id="first-class-functions"><strong> First class functions. </strong></h3>
<p>JavaScript is a language with first-class functions, which simply means that functions are treated just as regular variables. So, we can pass functions into other functions and we can even return functions from functions. And this is extremely powerful because it allows us to use a lot of powerful techniques and allows for functional programming which is one of the paradigms that we just talk about. And in fact, we use them without knowing that they are first-class functions.
For example :</p>
<pre><code><span class="hljs-selector-tag">result</span><span class="hljs-selector-class">.addEventListener</span>(<span class="hljs-string">'click'</span>, clickHandler);
</code></pre><p>In this example, `clickHandler is actually a first-class function that we use without knowing its name actually.
And not all languages have first-class functions, but JavaScript has and it is amazing. Believe me, it's really really helpful.</p>
<h3 id="dynamic-language"><strong> Dynamic language. </strong></h3>
<p>Javascript is a dynamic language and dynamic actually means dynamically -typed. So, as we already know in javascript we don't assign data types to variables. Instead, they only became known when the javascript engine executes our code. Also, the type of variables can easily be changed as we reassign variables, and this is basically what dynamically-typed means.</p>
<p>The same is not true for most other languages, where we have to manually assign types to variables and this usually prevents bugs from happening which is the reason why many people say that javascript should be a strongly typed language as well. And if you yourself want to use javascript with type then you can always look into <code>typescript</code>.</p>
<h3 id="single-thread-and-non-blocking-event-loop-concurrency"><strong>Single-thread and non-blocking event-loop concurrency.</strong></h3>
<p>Let's now finally talk about the single-thread and non-blocking event loop concurrency model. Now, this is a really complex topic and probably the most complex one out of the whole javascript. So, I am not going to go deep but instead, I will just define some things here. </p>
<p>First, what is a concurrency model? Well, it's just a fancy term that means how the javascript engine handles multiple tasks happening at the same time. That's cool but why do we need that? Well, because javascript itself runs in one single thread which means that it can only do one thing at a time. And, therefore we need a way of handling multiple things happening at the same time and by the way, in computing, a thread is like a set of instructions that are executed in the computer CPU. So basically, the thread is where our code is actually executed in a machine's processors. </p>
<p>All right, but what if there is a long-running task like fetching data from a remote server? Well, it sounds like, that would block the signal thread where the code is running, right? But of course, we don't want that. What we want is so-called non-blocking behaviour and how do we achieve that? Well, by using the so-called event loop. The event loop takes long-running tasks executes them in the background and then puts them back in the main thread once they are finished. </p>
<p>And this is in a nutshell JavaScript non-blocking event loop concurrency model with a single thread.</p>
<p>It sounds like a mouthful for sure but in the end, it really just compresses to this. </p>
<blockquote>
<h3 id="wrapping-up"><strong>Wrapping Up! </strong></h3>
</blockquote>
<p>So to define what is javascript we saw these features of javascript</p>
<ol>
<li>High-level programming language.</li>
<li>Interpreted or just in time compiled language.</li>
<li>Multi-paradigm.</li>
<li>First-class functions.</li>
<li>Dynamic language.</li>
<li>Single-thread and non-blocking event-loop concurrency.</li>
</ol>
<p>So, I hope now you can not only be able to define javascript by yourself but also you can be able to elaborate on that definition using these features of javascript.</p>
<p>I know there is a ton of stuff to take in but I hope that you still learned a thing or two here.😊</p>
<p>Thank you.</p>
]]></content:encoded></item><item><title><![CDATA[What is DOM and DOM manipulation?]]></title><description><![CDATA[Before we see why the DOM manipulation is not a javascript. Let's first understand DOM and DOM manipulation.

What is DOM?

DOM stands for document 'Document Object Model'. It is a structured representation of an HTML document that allows javascript ...]]></description><link>https://blog.rdshinde.com/what-is-dom-and-dom-manipulation</link><guid isPermaLink="true">https://blog.rdshinde.com/what-is-dom-and-dom-manipulation</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[DOM]]></category><category><![CDATA[Web API]]></category><dc:creator><![CDATA[Rishikesh Shinde]]></dc:creator><pubDate>Wed, 01 Sep 2021 04:17:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630428386173/tauW_U0Ua.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before we see why the DOM manipulation is not a javascript. Let's first understand DOM and DOM manipulation.</p>
<blockquote>
<p><strong>What is DOM?</strong></p>
</blockquote>
<p>DOM stands for document <code>'Document Object Model'</code>. It is a structured representation of an HTML document that allows javascript to access HTML elements and styles to manipulate them and it is a structured representation of an HTML document. </p>
<p>The DOM allow us to use javascript to access HTML elements and styles to manipulate them, for example, we will be able to change the text in HTML attributes and also to change CSS styles from our javascript so we can say that the DOM is a connection point between HTML document and JavaScript. </p>
<p>The DOM is automatically created by the browser as soon as the HTML page loads and it is stored in a tree structure like this one.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630429223154/bL8LOLLk5t.png" alt="Blue and White Calm Education YouTube Thumbnail.png" /></p>
<p>In this tree, each HTML element is one object, so let’s now take a look at this DOM structure in a little bit more detail and to illustrate this here is a very simple document and its DOM tree. </p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello, this is <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>HTML<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre><p>So as I already mentioned this is a tree structure that looks a bit like a family tree and we use it to turn them, child element, parent element, sibling element and so more when we talk about DOM manipulation and DOM structure. </p>
<p>Anyway as you can see for each element in the HTML here is one element node in the DOM tree and we can access and manipulate each element using JavaScript.</p>
<p>So the DOM always starts with a document object right at the very top and this document is a special object that we have access to in JavaScript and this object serves as an entry point into the DOM. </p>
<p>Remember how we use the <code>document.queryselector()</code> method to select an element so that it means the <code>queryselector</code> method is available on the document of the object and that’s why we say that document is the entry point to the DOM. Because we need it to starts selecting elements.</p>
<p>The first child element of the document is usually the HTML element because that’s usually the root element in all HTML documents. Now moving ahead HTML elements have usually to child elements head and body and so, of course, you can also find them here in the DOM tree and as you can see they are adjacent elements so they are siblings in our DOM as well. As we keep going deeper into the nested HTML structure we keep adding more and more children to the DOM tree. So inside the head and body, you have more child elements.</p>
<p>But the DOM tree has more than just HTML element nodes, it also has the nodes for all the text itself, comments and others stuff. Because basically, the rule is that whatever is in the HTML document also has to be in the DOM. And so as you can see the DOM is a complete representation of the HTML document. So that we can manipulate it in complex ways.</p>
<p>And with this, you should have a good overview of how the DOM works and what it looks like.</p>
<blockquote>
<p><strong>What is DOM manipulation?</strong> </p>
</blockquote>
<p>Manipulating DOM is accessing the objects in the DOM tree and changing the values that belong to them. This also includes the styling of those elements. And how we access these objects that we have seen above.</p>
<blockquote>
<p><strong>Why DOM manipulation !== JavaScript?</strong></p>
</blockquote>
<p>Many beginners believe that the DOM and all the methods and properties that we can use to manipulate a DOM such as <code>document.queryselector()</code> and lots of other stuff are part of javascript, however, this is not the case. Remember the javascript is actually just a dialect of the ECMA script specification, and all this DOM related stuff is not there in JavaScript.</p>
<p>But now you might ask DOM is not a part of JavaScript then how does this all work?
To understand this we need to have no web APIs.</p>
<blockquote>
<p><strong>What are web APIs?</strong></p>
</blockquote>
<p>The DOM and the DOM methods are part of something called the web APIs. The web APIs are like libraries that the browser implements and we can access them from our JavaScript code.</p>
<p>For now, what you need to know is web APIs are the libraries that are also written in javascript and that are automatically available to us to use. So all this happens behind the scenes we don’t have to import all to do anything to use them. And there is an officially DOM specification that browsers implement which is the reason why DOM manipulation works the same on all browsers. </p>
<p>Now, besides the DOM there are tons of other APIs like timers and fetch and many more we will talk about them in upcoming posts.</p>
<blockquote>
<p><strong>Wrapping up.</strong></p>
</blockquote>
<p><strong>DOM</strong> </p>
<ol>
<li>Stands for Document Object Model.</li>
<li>Allows us to access HTML elements using javascript.</li>
<li>Browser creates it for us automatically when the page loads. </li>
</ol>
<p><strong>DOM Manipulation</strong> </p>
<p>Accessing objects in the DOM tree and changing values that belong to it.</p>
<p><strong>Why DOM Manipulation !== Javascript?</strong>  </p>
<p>The DOM and the DOM methods are part of something called the web APIs.</p>
<p>I hope 🤞  now you know why  DOM Manipulation !== Javascript.</p>
<p>Thank you!</p>
]]></content:encoded></item></channel></rss>