<?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: Musfiqua haque</title>
    <description>The latest articles on DEV Community by Musfiqua haque (@musfiquahaque).</description>
    <link>https://dev.to/musfiquahaque</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%2F940541%2Fd6afc00e-261b-4531-99d0-5ddcd716079d.jpeg</url>
      <title>DEV Community: Musfiqua haque</title>
      <link>https://dev.to/musfiquahaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/musfiquahaque"/>
    <language>en</language>
    <item>
      <title>Asynchronous Behaviour In JavaScript</title>
      <dc:creator>Musfiqua haque</dc:creator>
      <pubDate>Sun, 03 Mar 2024 19:33:06 +0000</pubDate>
      <link>https://dev.to/musfiquahaque/asynchronous-behaviour-in-javascript-4hg6</link>
      <guid>https://dev.to/musfiquahaque/asynchronous-behaviour-in-javascript-4hg6</guid>
      <description>&lt;p&gt;Hello everyone!&lt;br&gt;
Today I am describing the synchronous and asynchronous behavior of javascript. We all know about it but I want to discuss here the asynchronous behavior of javascript and JavaScript blocking behavior in the browser.&lt;br&gt;
Before knowing about that we need to know how Javascript code works line by line in the browser.&lt;br&gt;
Let's take 2 things that every developer needs to run the code. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code Editor&lt;/li&gt;
&lt;li&gt;Browser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the browser, there is a mechanism that converts the javascript code to machine language. In that case, browser contains two things. One is the &lt;strong&gt;Runtime environment&lt;/strong&gt; another is the &lt;strong&gt;browser's engine&lt;/strong&gt;. This runtime environment indicates some objects. &lt;br&gt;
For example window.alert(), console.log(), document.getElementById() and so on. These things are called runtime environments. And the engine that is responsible for converting the human-written code to machine language so that the machine can understand what to do.&lt;br&gt;
In the browser engine, there are two things one is call stack.&lt;br&gt;
let's talk about how call stack works in the code line by line.In the editor our code is&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88a4ech8hr713j1nyn10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88a4ech8hr713j1nyn10.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wrote a simple code where there is a function and some console log and I added a while loop that functionality works after 5 second later. What happened there? &lt;br&gt;
In the call Stack that I mentioned before it first calls a main function. You know every javascript starts from a main function.After that line by line, it called.&lt;br&gt;
JavaScript starts from here &lt;code&gt;console.log("process 1");&lt;/code&gt;&lt;br&gt;
so in the stack after calling the main function it calls &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;process 1&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;so process 1 is executed.&lt;br&gt;
After that, it called &lt;strong&gt;process() ** function. &lt;br&gt;
When javascript goes to the **process&lt;/strong&gt; function it finds another function which is  &lt;code&gt;console.log("process 2");&lt;/code&gt;.Then it calls&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;process 2&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After the next line, javascript sees that there is a while loop which is for 5 second .In that time, javascript stops all the browser's activity for 5 second. Even users can not do anything in the browser. This is the &lt;strong&gt;JavaScript blocking behavior&lt;/strong&gt;. After 5 second while the loop is finished, the next line is stored in the call stack &lt;code&gt;console.log("process 3 after 2 sec");&lt;/code&gt;&lt;br&gt;
 and then printed in the browser.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;process 3 after 2 sec&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After that the last line is execute &lt;code&gt;console.log("process completed");&lt;/code&gt; and print&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;process completed&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this code, the problem is this while loop that prevents the activity of the browser for a predefined second. To remove this problem we use asynchronous javascript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jgcf0brj1fuw06jmoy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jgcf0brj1fuw06jmoy6.png" alt="Image description" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now see the above code. Everything is the same here I just added the &lt;strong&gt;setTimeOut&lt;/strong&gt; function instead of a while loop. For debugging purposes, I write to console something so that we can understand the execution.&lt;br&gt;
Everything is the same as I described before. Firstly, in the call stack main function is called then the first console log is printed after that &lt;strong&gt;process&lt;/strong&gt; function is called. and it prints the second console log but in the next line, it appears the &lt;strong&gt;setTimeOut&lt;/strong&gt; function, and take it in the call stack but now the interesting thing is there. In&lt;a href="https://dev.tourl"&gt;&lt;/a&gt; previous, when javascript found the while loop it stopped the browser's activity and did not go forward. but now it doesn't do that because &lt;strong&gt;setTimeOut&lt;/strong&gt; is a built-in asynchronous function. In that time javascript passes this function to web API which can handle this function's execution. web API is a part of the runtime environment. javascript just goes forward and passes this &lt;strong&gt;setTimeOut&lt;/strong&gt; function to web API. It executes the next line &lt;code&gt;console.log("process 3");&lt;/code&gt; and prints&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;process 3&lt;br&gt;
The timer that I set in the function is going on inside the web API. After that last console log goes into the call stack and print&lt;/p&gt;

&lt;p&gt;process completed&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now another thing is coming which is &lt;strong&gt;callback queue&lt;/strong&gt; also a part of the runtime environment. Previously we know about the call stack that works like ** Last In First Come*&lt;em&gt;. Now this callback queue works as a queue like **First In First Come&lt;/em&gt;&lt;em&gt;.&lt;br&gt;
So callback means that called after some time. here the function that we set in the **setTimeOut&lt;/em&gt;* function is a callback function because it will call after the end of the period.&lt;br&gt;
So, when the &lt;strong&gt;setTimeOut&lt;/strong&gt; function calls it after 5 second, the function is stored in the callback queue.Then it goes to another line and execute the &lt;code&gt;console.log("process completed)&lt;/code&gt; and print&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Process completed &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, now the function that I write inside the setTimeOut function is stored from callback queue to call stack.This time browser is not block for doing the particular work rather it works and execute the another line. Now from the call stack the last code is executed &lt;code&gt;console.log("process called after 5 sec")&lt;/code&gt; and print&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Process called after 5 sec&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, this is the process about synchronous and asynchronous JavaScript.&lt;br&gt;
In conclusion, asynchronous JavaScript helps us to work without blocking the browser.Here I just write something inside the setTimeOut function but it could be data fetching,a large file read or to bring some data from the largest database. That time it need more time to bring this data. Asynchronous JavaScript help us to do this work using web API and go forward to execute the the code with blocking the browser.&lt;/p&gt;

&lt;p&gt;That's all for today.Thanks for your patience to stay with me. Please comments below if you like it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>"HOC" The Design Pattern</title>
      <dc:creator>Musfiqua haque</dc:creator>
      <pubDate>Mon, 27 Nov 2023 17:13:21 +0000</pubDate>
      <link>https://dev.to/musfiquahaque/hoc-the-design-pattern-14c0</link>
      <guid>https://dev.to/musfiquahaque/hoc-the-design-pattern-14c0</guid>
      <description>&lt;p&gt;Hello friends, today I am here to share another topic which name is Higher-Order-Component. In short, we name it HOC.&lt;br&gt;
HOC is nothing but all about the design pattern and how you organize your components so that they can used multiple times. This reduces the amount of writing code and creates a smooth data flow from parent to child.&lt;/p&gt;

&lt;p&gt;HOC is quite an advanced topic but we need to follow this pattern in our regular react code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvg2k6ufex1hwo4i2iik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvg2k6ufex1hwo4i2iik.png" alt="Image description" width="588" height="811"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here inside the src file, I take a folder named HOC for our understanding purpose. Inside the HOC folder, we take 2 files: Counter.jsx and Hover.jsx.&lt;/p&gt;

&lt;p&gt;let's create a class component in Counter.jsx. Add a button named "Click X times".&lt;/p&gt;

&lt;p&gt;`import React, { Component } from 'react'&lt;/p&gt;

&lt;p&gt;export default class Counter extends Component {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state = {
    count: 0,
};
handleClick =()=&amp;gt;{
    this.setState((prevState)=&amp;gt;({count:prevState.count + 1}))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;br&gt;
As it is a class component we take the state and initialize a value count = 0. To activate the button I called a function that is handleClick inside the button.&lt;/p&gt;

&lt;p&gt;the handleClick button is declared with an arrow function and inside the function, we take the previous value named &lt;strong&gt;prevState&lt;/strong&gt; as an anonymous function. To increment the count value we need the previous count value and add 1 with every increment.&lt;br&gt;
After that pass the count state inside the button using curly brackets.&lt;/p&gt;

&lt;p&gt;Another component we designed is Hover.jsx and the functionality is as same as the counter.jsx component.&lt;/p&gt;

&lt;p&gt;`import React, { Component } from 'react'&lt;/p&gt;

&lt;p&gt;export default class Counter extends Component {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state = {
    count: 0,
};
handleClick =()=&amp;gt;{
    this.setState((prevState)=&amp;gt;({count:prevState.count + 1}))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;br&gt;
These two component works when we over the mouse in the text and click on the button. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0l7r5nid7s7fa7c0mqn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0l7r5nid7s7fa7c0mqn.png" alt="Image description" width="561" height="313"&gt;&lt;/a&gt;&lt;br&gt;
you can find the output in the picture above.&lt;br&gt;
Up to this, all is working well but what happens if you have to design 10 or 20 components using the same functionality?&lt;br&gt;
What happens if you need to change the state or add some interactivity inside the function?&lt;/p&gt;

&lt;p&gt;It breaks down the react rules so that you can't write the same code again and again rather you can use the same code multiple times.&lt;/p&gt;

&lt;p&gt;So from this concept react introduced the HOC pattern. This pattern creates a function that passes the original component as a parameter and returns a new component. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can think like that by wearing a new dress you are becoming a pretty person.&lt;br&gt;
const LookingPretty = HigherOrderComponent(MeWithNewDress);&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's move on to using code. As we see in both components the functionality is the same. So we take another component named WithHoc.jsx. It's the naming convention that you start your component using (with).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const WithHoc(originalComponent){&lt;br&gt;
    return &amp;lt;NewComponent /&amp;gt;&lt;/code&gt;&lt;br&gt;
} &lt;br&gt;
From this, we can see that WithHoc is just a function that takes a component as a parameter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const WithHoc=(OriginalComponent)=&amp;gt;{&lt;br&gt;
    class NewComponent extends React. Component{&lt;br&gt;
        state = {&lt;br&gt;
            count: 0,&lt;br&gt;
        };&lt;br&gt;
        handleClick =()=&amp;gt;{&lt;br&gt;
            this.setState((prevState)=&amp;gt;({count:prevState.count + 1}))&lt;br&gt;
        }&lt;br&gt;
    render()&lt;br&gt;
    {&lt;br&gt;
        const {count} = this.state;&lt;br&gt;
        return &amp;lt;OriginalComponent count={count} handleClick={this.handleClick} /&amp;gt;&lt;br&gt;
    }&lt;br&gt;
};&lt;br&gt;
    return NewComponent;&lt;br&gt;
}&lt;br&gt;
export default WithHoc;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;WithHoc function just takes an original component as a prop and inside the component, it puts its state and handleClick function.&lt;br&gt;
As the original component, we use a class component so it renders a method and returns the original component. The returned component passes the state and the function using the props and sends to hover and counter components.&lt;br&gt;
and this OriginalComponent with pros (state value, function) is returned as a NewComponent of the WithHoc function.&lt;/p&gt;

&lt;p&gt;That's all about today. I just want to share the knowledge that I have so far and I gained my knowledge from the most efficient channel (LWS) which is conducted by Sumit Saha, sir. You can also follow this, he is an outstanding person and out of this world who continuously shares his knowledge in the Dev community. If you people take it as a beneficial thing and read the article then take my warm welcome.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>All About JavaScript Scope</title>
      <dc:creator>Musfiqua haque</dc:creator>
      <pubDate>Thu, 23 Nov 2023 09:07:29 +0000</pubDate>
      <link>https://dev.to/musfiquahaque/all-about-javascript-scope-1api</link>
      <guid>https://dev.to/musfiquahaque/all-about-javascript-scope-1api</guid>
      <description>&lt;p&gt;Hi there. It is easier to share knowledge than to learn anything from resources. I will therefore be sharing my knowledge on scope today, a crucial JavaScript topic.&lt;br&gt;
Now let's proceed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope dictates the level of visibility and accessibility. Because programming languages specify which parts of a variable can be accessed, that is how it works..&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Basically 3 scope we have but sometimes learners faces an contradictory situation in between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block Scope&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Function Scope&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Global Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can infer from the term "Block Scope" that access is restricted to a specific portion.Following the introduction of ES6, function scope and global scope are added to the block scope.&lt;br&gt;
In ES6, the terms "let" and "const" are introduced to help us grasp the block scope.&lt;/p&gt;

&lt;p&gt;Let's have a look.&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
   let language = "JavaScript";&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
   var language = "JavaScript";&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Do you notice a difference? Indeed, there are differences in the variable types; one uses let, while the other uses var.&lt;br&gt;
Since no one has the authority to call the let outside of the {} block, let is our block scope here rather than the var type.&lt;/p&gt;

&lt;p&gt;And var can be access able from the outside of the block so it doesn't create the block scope.&lt;/p&gt;

&lt;p&gt;Now let's discuss local scope. &lt;br&gt;
The variable that is granted permission just within the function is referred to as local scope, meaning that it can only be accessed from within its immediate surroundings.&lt;/p&gt;

&lt;p&gt;// Not allow to use the code&lt;br&gt;
function myFunction(){&lt;br&gt;
   let carName: "Toyota";&lt;br&gt;
   // allow to use the code//&lt;br&gt;
}&lt;br&gt;
// Not allow to use the code&lt;/p&gt;

&lt;p&gt;Local scope life time is very limited. With the end of function lifetime the local scope lifetime is also end.&lt;/p&gt;

&lt;p&gt;Time to learn about function scope. From the name it's quite simple to recognize that scope.&lt;br&gt;
Those variable have the permission to call them from inside the function only rather then outside of the function.&lt;/p&gt;

&lt;p&gt;In that case var, let and const are doing same thing. When they declared in the function the function scope is created and after end of the performance of the function the  scope is finished.&lt;/p&gt;

&lt;p&gt;For understand the global scope it's necessary to have knowledge about global variables.&lt;/p&gt;

&lt;p&gt;The variable that can decalear outside of the function is always a global variable. It's accessible from anywhere of your code.&lt;/p&gt;

&lt;p&gt;So where global variable is declare global scope is start from there and in that case the let,const and var are same.&lt;/p&gt;

&lt;p&gt;var fruit = "Apple";     // Global scope&lt;/p&gt;

&lt;p&gt;const fruit = "Apple";     // Global scope&lt;/p&gt;

&lt;p&gt;let fruit = "Apple";     // Global scope&lt;/p&gt;

&lt;p&gt;There another thing is that "Automatically Global"&lt;/p&gt;

&lt;p&gt;If you assign value in a variable without declare the variable that's consider automatically global variable rather if it stay inside the function.&lt;/p&gt;

&lt;p&gt;programming();&lt;br&gt;
// can accessible from anywhere&lt;/p&gt;

&lt;p&gt;function programming(){&lt;br&gt;
     language = "JavaScript";&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;The language is inside the function but it can accessible from anywhere of the script because it's not declarer using vat let const. so it declared automatically global.&lt;/p&gt;

&lt;p&gt;However, That's the end of today's discussion. Hopefully You will enjoy it.&lt;/p&gt;

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