<?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: Sai Teja</title>
    <description>The latest articles on DEV Community by Sai Teja (@saiteja13427).</description>
    <link>https://dev.to/saiteja13427</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%2F491830%2F8017c8f0-4ece-43ea-87bd-8204b6bf90a3.jpeg</url>
      <title>DEV Community: Sai Teja</title>
      <link>https://dev.to/saiteja13427</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saiteja13427"/>
    <language>en</language>
    <item>
      <title>How Does JavaScript Even Work? Things Which 90% of JavaScript Developers Don't Know! (Part 1)</title>
      <dc:creator>Sai Teja</dc:creator>
      <pubDate>Tue, 04 Jan 2022 10:20:29 +0000</pubDate>
      <link>https://dev.to/saiteja13427/how-does-javascript-even-work-things-which-90-of-javascript-developers-dont-know-part-1-145b</link>
      <guid>https://dev.to/saiteja13427/how-does-javascript-even-work-things-which-90-of-javascript-developers-dont-know-part-1-145b</guid>
      <description>&lt;p&gt;JavaScript was initially developed as a frontend scripting language to fulfil the lacking dynamic behaviour of the web by Brendan Eich of Netscape for their Netscape Navigator browser. &lt;/p&gt;

&lt;p&gt;From there, Javascript has today grown into such a huge ecosystem that you can develop applications on the web, mobile and desktop with just javascript which is impossible with any other language.&lt;/p&gt;

&lt;p&gt;Now, if you have ever worked with JavaScript, you would have heard that Javascript is single-threaded, non-blocking and it is asynchronous as well. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Isn't that counter-intuitive? *&lt;/em&gt; How can something be single-threaded and asynchronous as well?&lt;/p&gt;

&lt;p&gt;Well, there are a lot of such things in JS and to understand it all, you need a deep dive into JS and its working.&lt;/p&gt;

&lt;p&gt;So, let us try and understand the working on javascript which 90% of JS developers don't know!! &lt;/p&gt;

&lt;p&gt;Firstly, let us see what is &lt;strong&gt;ECMA&lt;/strong&gt; and what do these specifications tell us about JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  ECMA Script
&lt;/h2&gt;

&lt;p&gt;ECMA basically stands for &lt;strong&gt;European Computer Manufacturer's Association&lt;/strong&gt; and it defines a trademark standard for scripting languages like JavaScript and the JScript (Developed by Microsoft).&lt;/p&gt;

&lt;p&gt;You would have heard ES6, ES7 while working with JS, the ES here stands for ECMAScript.&lt;/p&gt;

&lt;p&gt;ECMA puts a lot of specifications in place related to notations, operations, syntax, functions, classes etc.&lt;/p&gt;

&lt;p&gt;You can refer to all these specifications here: &lt;a href="https://tc39.es/ecma262/" rel="noopener noreferrer"&gt;ECMAScript&lt;/a&gt; &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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640411343039%2Fx9MG5HKz9.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640411343039%2Fx9MG5HKz9.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But ECMA doesn't have any documentation on the event loop, non-blocking, asynchronous JS.&lt;/p&gt;

&lt;p&gt;Hmm, so where do we go now? We go to the JS engines!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Note&lt;/code&gt;&lt;/strong&gt;: If you want to read more about the advent of JS and its whole history, here is a fantastic blog:  &lt;a href="https://itnext.io/why-are-we-creating-a-javascript-only-world-wide-web-db8c3a340b9" rel="noopener noreferrer"&gt;Why are we creating a JavaScript-only World Wide Web?&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Engines
&lt;/h2&gt;

&lt;p&gt;A JavaScript engine is basically a software that executes JavaScript code. There are various Javascript engines available. The most famous engines among the modern  &lt;a href="https://en.wikipedia.org/wiki/Just-in-time_compilation" rel="noopener noreferrer"&gt;Just in Time Compilation&lt;/a&gt;  engines are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://v8.dev/" rel="noopener noreferrer"&gt;V8&lt;/a&gt;&lt;/strong&gt; ( Developed by &lt;strong&gt;Google&lt;/strong&gt; | Used in chrome, Node.js runtime, Deno runtime)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://spidermonkey.dev/" rel="noopener noreferrer"&gt;Spider Monkey&lt;/a&gt;&lt;/strong&gt; (Developed by &lt;strong&gt;Mozilla&lt;/strong&gt; | Used in Firefox)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://webkit.org/" rel="noopener noreferrer"&gt;WebKit&lt;/a&gt;&lt;/strong&gt; (Developed by &lt;strong&gt;Apple&lt;/strong&gt; | Used in Safari)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chakra&lt;/strong&gt; (Developed by &lt;strong&gt;Microsoft&lt;/strong&gt; | Used in Internet Explorer)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Check out the list of all JS engines here:  &lt;a href="https://en.wikipedia.org/wiki/List_of_ECMAScript_engines" rel="noopener noreferrer"&gt;List of ECMAScript engines (Wikipedia)&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now, as V8 is the most used javascript engine, from now whatever we discuss will be its implementation. Though the other mentioned engines are also similar with a few differences.&lt;/p&gt;

&lt;p&gt;So, the V8 engine is what makes Javascript single-threaded as it comes with a single call stack (also called as the execution stack) or single thread.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640581401209%2FGsKemb3Uz.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640581401209%2FGsKemb3Uz.png" alt="V8 Architecture.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The V8 engine comes with two main components which are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Call stack&lt;/strong&gt;: This is where all the execution happens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heap&lt;/strong&gt;: This is where the memory allocation happens. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But even &lt;strong&gt;standalone V8 doesn't make JS asynchronous&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, how does it become asynchronous? Keep Reading.&lt;/p&gt;

&lt;p&gt;So, let us try to understand the single-threaded nature of JS first and what does it mean. Then we will see how does it become asynchronous and what problem does it solve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Javascript and the Single Thread
&lt;/h2&gt;

&lt;p&gt;Let us get this single thread concept right!&lt;/p&gt;

&lt;p&gt;Javascript being single-threaded means that there is only one call stack provided by the engine. All the execution happens one block after another in a sequential way. That means that all the upcoming code should wait until the current block of code is being executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One Thread == One Call Stack == One Thing at a Time&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is called the synchronous execution of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Let me tell you that, &lt;strong&gt;Javascript can be multithreaded as well&lt;/strong&gt; for example we can use  &lt;a href="https://blog.logrocket.com/node-js-multithreading-what-are-worker-threads-and-why-do-they-matter-48ab102f8b10/" rel="noopener noreferrer"&gt;multiple threads in Node.js&lt;/a&gt;  using a module called &lt;code&gt;worker_threads&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, why do people call JavaScript a single-threaded language? It is because of the implementation of JS engines like V8 in the browsers which makes JS single-threaded because these engines have got only one call stack as already mentioned.&lt;/p&gt;

&lt;p&gt;Now as you understood this, let us try a few programs with the main thread to understand the call stack and the problem with it.&lt;/p&gt;

&lt;p&gt;Continue Reading the Blog &lt;a href="https://lightmap.dev/how-does-javascript-even-work-1" rel="noopener noreferrer"&gt;Here&lt;/a&gt;&lt;/p&gt;

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