<?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: Roshan Kumar Badola</title>
    <description>The latest articles on DEV Community by Roshan Kumar Badola (@roshankbadola).</description>
    <link>https://dev.to/roshankbadola</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%2F649199%2Ffd3ec23c-2348-4ea5-a373-400298fe3c08.png</url>
      <title>DEV Community: Roshan Kumar Badola</title>
      <link>https://dev.to/roshankbadola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roshankbadola"/>
    <language>en</language>
    <item>
      <title>Javascript is NOT single threaded!!</title>
      <dc:creator>Roshan Kumar Badola</dc:creator>
      <pubDate>Mon, 22 Jul 2024 19:43:52 +0000</pubDate>
      <link>https://dev.to/roshankbadola/javascript-is-not-single-threaded-3b9a</link>
      <guid>https://dev.to/roshankbadola/javascript-is-not-single-threaded-3b9a</guid>
      <description>&lt;p&gt;NO! you have not learned wrong that JavaScript is a single threaded language. It is a single-threaded language it has access to a single main thread to execute the code. So when we talk about synchronous programming, we are talking about this lone thread doing all the heavy lifting and executing our code.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3vckf9mw7k3x6qnjqz4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3vckf9mw7k3x6qnjqz4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But in reality V8 engine and Node.js use a c library called libuv to get access to six extra threads. Two of these are used for doing garbage collection and the rest are used for doing background tasks like asynchronous programming. &lt;br&gt;
Do keep in mind that browsers are a completely different environment then Node.js and and handle this process in a different way.&lt;/p&gt;

&lt;p&gt;Yes. When we say that code is asynchronous or non-blocking what really happens is the async code is passed to these extra threads with the callback and the main thread keeps on doing its work without blocking the rest of the code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj7vibzdrudys6mtffy0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj7vibzdrudys6mtffy0.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
When the async code is finished, the callback function is pushed into the event queue with either error or the required data. Then the event loop pushes it into the call stack and boom we get our result, to understand this with some code. Let's look at the readfile method of fs module.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

fs.readFile("demo.text","utf8",(err,data)=&amp;gt;{
    if(error){       
        return error
    }
    console.log("output",data);
})


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

&lt;/div&gt;

&lt;p&gt;In the code above the readfile method is passed to the background threads. The reading to happens in the background, and when that gets finished the callback is pushed into the event queue with either error or data. &lt;/p&gt;

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

&lt;p&gt;Once in callstack the callback is executed and we are left with either error or the data as output.&lt;br&gt;
Thanks for reading I hope I was able to explain about the single threaded means in Javascript.&lt;/p&gt;

&lt;p&gt;TLDR : Yes JavaScript is a single threaded and synchronous programming language but we can achieve performance like a multi-threaded language by utilizing background threads for asynchronous programming.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>fullstack</category>
      <category>learning</category>
    </item>
    <item>
      <title>this in JavaScript.</title>
      <dc:creator>Roshan Kumar Badola</dc:creator>
      <pubDate>Sat, 27 Apr 2024 15:32:05 +0000</pubDate>
      <link>https://dev.to/roshankbadola/this-in-javascript-1p2i</link>
      <guid>https://dev.to/roshankbadola/this-in-javascript-1p2i</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; can be a confusing topic for a lot of us when it comes to JavaScript. And I am going to try to make it a bit more easier of understand and work with &lt;code&gt;this&lt;/code&gt; in JavaScript for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  this
&lt;/h2&gt;

&lt;p&gt;Ok, lets start with &lt;code&gt;this&lt;/code&gt; itself. In JavaScript, &lt;code&gt;this&lt;/code&gt; is a keyword refers to the context of the code. Now, that context is defined based on what we are calling &lt;code&gt;this&lt;/code&gt; and their execution context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution Context&lt;/strong&gt; refers to an environment in which JavaScript code is executed.&lt;/p&gt;

&lt;p&gt;There are several ways to use this, So, let's look at what we get when call this by itself.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(this)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;when we run this line of code we get the Window object as output, but it will be global object in Node.js.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Window {window: Window, self: Window, document: document, name: '', location: Location, …}&lt;/code&gt; &lt;br&gt;
this window object contains our window methods. We get this as output because in this case context for &lt;code&gt;this&lt;/code&gt; is global.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; in function
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; when called inside a function it refers to the global object by default even though function creates it's own execution context so,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function this_in_a_function() {
    console.log('this in a function', this);
}

this_in_a_function()

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

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Window {window: Window, self: Window, document: document, name: '', location: Location, …}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;However, if strict mode &lt;code&gt;"use strict"&lt;/code&gt; is used it will output undefined.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; in object
&lt;/h3&gt;

&lt;p&gt;When used with object we will see that &lt;code&gt;this&lt;/code&gt; will no longer refer to Window Object. That is because the value for &lt;code&gt;this&lt;/code&gt; in function also depends on how it's called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person ={
    firstName: "John",
    lastName : "Doe",
    id       : 1234,
    getThis : function() {
      return this;
    }
};

console.log(person.getThis());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  firstName: 'John',
  lastName: 'Doe',
  id: 1234,
  getThis: [Function: getThis]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and because of that we can also use &lt;code&gt;this&lt;/code&gt; to access object properties and methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
    firstName: "John",
    lastName : "Doe",
    id       : 1234,
    getFullName : function() {
      return this.firstName + ' ' + this.lastName;
    }
};

console.log(person.getFullName());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;code&gt;John Doe&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; in arrow functions
&lt;/h3&gt;

&lt;p&gt;In arrow functions, &lt;code&gt;this&lt;/code&gt; is set lexically. That's because the arrow function dose not make it's own execution context but rather inherits it from outer functions.&lt;/p&gt;

&lt;p&gt;Generally it will mean that &lt;code&gt;this&lt;/code&gt; also refers to Window Object if arrow function is defined in global scope and global object in Node.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const this_in_arrow = () =&amp;gt; this

console.log(this_in_arrow())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;code&gt;Window {window: Window, self: Window, document: document, name: '', location: Location, …}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;but a more accurate example would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function person(){
    name= 'Jhon'
    surname= 'doe'
    return (() =&amp;gt; this.name)()

}

console.log(person());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;code&gt;Jhon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example &lt;code&gt;this&lt;/code&gt; is scoped to the function &lt;code&gt;person&lt;/code&gt; hence the output.  &lt;/p&gt;

&lt;p&gt;Let's see what happens if we do this same thing with a objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
    firstName: 'Jhon',
    lastName: 'Doe',
    sayName: () =&amp;gt; this.firstName
}

console.log(person.sayName());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We get &lt;code&gt;undefined&lt;/code&gt;. Now, that's because object does not have it's own execution context and arrow function define &lt;code&gt;this&lt;/code&gt; based on their environment. To fix this we can wrap our arrow function in another regular function and use that to pass on the context for &lt;code&gt;this&lt;/code&gt; to our arrow function. Also this in an arrow function within an object refers to the global context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
    firstName: 'Jhon',
    lastName: 'Doe',
    sayName:function(){ 
            return () =&amp;gt; this.firstName
                      }
}

console.log(person.sayName()());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Jhon&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;I hope this cleared your doubts about &lt;code&gt;this&lt;/code&gt; in JavaScript and  helps you in journey to learn JavaScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>this</category>
      <category>methods</category>
      <category>guide</category>
    </item>
  </channel>
</rss>
