<?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: Ishan Srivastava</title>
    <description>The latest articles on DEV Community by Ishan Srivastava (@ishansrivastava).</description>
    <link>https://dev.to/ishansrivastava</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%2F395514%2F9c15c987-2d05-43eb-9c2e-5340f2ff627b.png</url>
      <title>DEV Community: Ishan Srivastava</title>
      <link>https://dev.to/ishansrivastava</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ishansrivastava"/>
    <language>en</language>
    <item>
      <title>Javascript engine : you beauty.</title>
      <dc:creator>Ishan Srivastava</dc:creator>
      <pubDate>Fri, 19 Jun 2020 21:01:58 +0000</pubDate>
      <link>https://dev.to/ishansrivastava/javascript-engine-you-beauty-52ko</link>
      <guid>https://dev.to/ishansrivastava/javascript-engine-you-beauty-52ko</guid>
      <description>&lt;h4&gt;
  
  
  ☝️ First things first, what's an environment?
&lt;/h4&gt;

&lt;p&gt;Can you recall the list of things that you consume in code, and haven't actually defined them anywhere, or ever wondered how this language runs asynchronous code despite being single thread?&lt;br&gt;
So that is provided to you by the runtime environment, things like the DOM, that beautiful setTimeout() function etc, are all provided by the environment.&lt;/p&gt;

&lt;p&gt;We call it the &lt;em&gt;javascript runtime environment&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Imagine it like a big container, and the engine we are interested in, is a part of this container&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Javascript runtime environment&lt;/em&gt;(JRE) is responsible for making JavaScript asynchronous. It is the reason JavaScript is able to add event listeners and make HTTP requests asynchronously.&lt;br&gt;
JRE consists of the following components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JS Engine&lt;/li&gt;
&lt;li&gt;Web API&lt;/li&gt;
&lt;li&gt;Callback Queue or message queue&lt;/li&gt;
&lt;li&gt;Event Table&lt;/li&gt;
&lt;li&gt;Event loop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For browsers (client side ): JRE differs from browser to browser although this difference used to be huge back in 2000s, but has greatly reduced now.&lt;/p&gt;

&lt;p&gt;Node.js according to the official documentations is a server side JavaScript runtime, which means it would differ from client side environments.So things like DOM won't be there in the Node.js environment.&lt;/p&gt;

&lt;p&gt;Also  💥&lt;strong&gt;Deno&lt;/strong&gt;💥, is a runtime environment for Typescript and Javascript. Remember that in other Javascript environments Typescript is supported, but in them (eg Node.js) Typescript is first transpiled into Javascript &lt;/p&gt;




&lt;h4&gt;
  
  
  The Javascript Engine
&lt;/h4&gt;

&lt;p&gt;JavaScript engines are inbuilt in all the modern browsers today. When the JavaScript file is loaded in the browser, JavaScript engine will execute each line of the file from top to bottom (to simplify the explanation we are avoiding hoisting in JS). JavaScript engine will parse the code line by line, convert it into machine code and then execute it.&lt;/p&gt;

&lt;p&gt;JavaScript engine consists of two components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Execution context stack (process data)&lt;/li&gt;
&lt;li&gt;Heap (save data)&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Execution context stack :
&lt;/h5&gt;

&lt;p&gt;Execution context stack is a stack data structure.As JavaScript engine has only one ECS, it can execute only one thing at a time which is at the top of the ECS. This is what makes JavaScript single threaded. If the same function is called twice like in recursion, it will have two different functional execution context in the ECS.&lt;/p&gt;

&lt;h5&gt;
  
  
  Heap :
&lt;/h5&gt;

&lt;p&gt;Heap is a large unstructured data structure which stores all the dynamic data like function definitions, objects, arrays etc. Execution context stack just contains their reference or in other words stores their memory addresses where these function definitions, objects and arrays are stored. The memory occupied in the heap continues to exist even after the JavaScript code execution has completed. They are removed by the JavaScript Garbage Collector.&lt;/p&gt;




&lt;p&gt;🔥  By default, at the bottom of the ECS, we have a &lt;em&gt;global execution context&lt;/em&gt; which deals with all the code in the global scope (the functions you can access anywhere). Also Each function has its own execution context called functional execution context (the data in this context can be only consumed by the function itself, and it's children functions) which gets inserted on the top of ECS as and when the function is called in the code.&lt;/p&gt;




&lt;h4&gt;
  
  
  🔥 Something from real life?
&lt;/h4&gt;

&lt;p&gt;Imagine a robot is putting out a fire:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The JavaScript code would be the instructions to the robot to put out a fire.&lt;/li&gt;
&lt;li&gt;The JavaScript engine would be the robot which can understand the instructions and act on it.&lt;/li&gt;
&lt;li&gt;The JavaScript runtime would be the fire truck, and the water gun.&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  How does the engine work?
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function functionOne() {
  console.log('function 1 called');
  functionTwo();
}

function functionTwo() {
  console.log('function 2 called');
}

functionOne();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When the browser loads the JS file, the JS engine will push the global execution context in the ECS and will start executing it. When JS engine reaches the function definition of functionOne, it stores the function definition in the heap memory and its reference in the global execution context. When functionOne is called by JS engine, it pushes functionOne execution context inside the ECS and starts executing functionOne, pausing the execution of the global execution context.&lt;/p&gt;

&lt;p&gt;👉 When JS engine calls functioninTwo inside functionOne, JS engine pushes functionTwo inside ECS and starts executing functionTwo, pausing the execution of functionOne. &lt;/p&gt;

&lt;p&gt;👉 Once all the code inside functionTwo is executed, JS engine pop out functionTwo execution context and restarts executing remaining code of functionOne.&lt;/p&gt;

&lt;p&gt;👉 Similarly, it removes the functionOne execution context once all the code of functionOne is executed. &lt;/p&gt;

&lt;p&gt;👉 It should be noted that even though functionOne has been removed from the ECS, objects and function definitions inside functionOne continue to occupy memory in the heap without being referred by any variable. They will be removed by the &lt;strong&gt;garbage collector&lt;/strong&gt; automatically, we don't have to remove it ourselves.&lt;/p&gt;




&lt;p&gt;I think that should have given you a good idea of the JS engines. Let me know if we should see what's inside Google's V8 engine, or the SpiderMonkey from Mozilla.Cheers  😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;References&lt;/em&gt; : &lt;br&gt;
&lt;a href="https://blog.bitsrc.io/javascript-internals-javascript-engine-run-time-environment-settimeout-web-api-eeed263b1617"&gt;JavaScript Internals&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@olinations/the-javascript-runtime-environment-d58fa2e60dd0#:~:text=Each%20browser%20has%20a%20JS%20Runtime%20Environment.&amp;amp;text=AJAX%2C%20the%20DOM%20tree%2C%20and,Engine%20that%20parses%20the%20code."&gt;The JS runtime environment&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>angular</category>
      <category>node</category>
    </item>
    <item>
      <title>Javascript modules, why?</title>
      <dc:creator>Ishan Srivastava</dc:creator>
      <pubDate>Tue, 26 May 2020 13:45:44 +0000</pubDate>
      <link>https://dev.to/ishansrivastava/javascript-modules-why-28gh</link>
      <guid>https://dev.to/ishansrivastava/javascript-modules-why-28gh</guid>
      <description>&lt;h1&gt;
  
  
  Why do we need modules anyway ?
&lt;/h1&gt;

&lt;p&gt;Javascript was not always as large as we see it right now — earlier it was used to majorily automate tasks, providing a bit of interactivity to your web pages where needed. So huge scripts were never in the picture. Fast forward a decade and we hardly have any webpage that does not use js, more over it has penitrated into other domains as well (Eg node.js, tensorflow.js).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hence it makes sense to split up our code into small chunks, something like components of&lt;br&gt;
a car, that each one of them serves a different purpose, can be used by other car&lt;br&gt;
models, and it can be replaced by other companies making the same component as well.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;A module is just a file. One script can be one module.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  The ECMAScript 5 module systems
&lt;/h1&gt;

&lt;p&gt;In that era , module systems were implemented via libraries, not built into the language. ES6 is the first time that JavaScript has built-in modules.Two popular ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CommonJS (targeting the server side)&lt;/li&gt;
&lt;li&gt;AMD (Asynchronous Module Definition, targeting the client side)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CommonJS&lt;/strong&gt;&lt;br&gt;
Originally CommonJS for modules was created for server platforms majorily. It achieved enormous popularity in the original Node.js module system. Contributing to that popularity were the npm package manager for Node and tools that enabled using Node modules on the client side (browserify, webpack, and others).This is an example of a CommonJS module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;// Imports
var importedFunc1 &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'./other-module1.js'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;.importedFunc1&lt;span class="p"&gt;;&lt;/span&gt;
var importedFunc2 &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'./other-module2.js'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;.importedFunc2&lt;span class="p"&gt;;&lt;/span&gt;

// Body
&lt;span class="k"&gt;function &lt;/span&gt;internalFx&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  // ···
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;exportedFx&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  importedFunc1&lt;span class="p"&gt;;&lt;/span&gt;
  importedFunc2&lt;span class="p"&gt;;&lt;/span&gt;
  internalFx&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

// Exports
module.exports &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  exportedFunc: exportedFunc,
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;AMD (Asynchronous Module Definition) modules&lt;/strong&gt;&lt;br&gt;
The AMD module system was created to be used in browsers than the CommonJS format. Its most popular implementation is &lt;a href="https://requirejs.org/"&gt;RequireJS&lt;/a&gt;. The following is an example of an AMD module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;define&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'./other-module1.js'&lt;/span&gt;, &lt;span class="s1"&gt;'./other-module2.js'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;,
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;otherModule1, otherModule2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    var importedFunc1 &lt;span class="o"&gt;=&lt;/span&gt; otherModule1.importedFunc1&lt;span class="p"&gt;;&lt;/span&gt;
    var importedFunc2 &lt;span class="o"&gt;=&lt;/span&gt; otherModule2.importedFunc2&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;function &lt;/span&gt;internalFunc&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      // ···
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;function &lt;/span&gt;exportedFunc&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      importedFunc1&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      importedFunc2&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      internalFunc&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      exportedFunc: exportedFunc,
    &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h1&gt;
  
  
  So, how to module in ES6 ?
&lt;/h1&gt;

&lt;p&gt;ECMAScript modules (ES modules or ESM) were introduced with ES6. They continue the tradition of JavaScript modules and have all of their aforementioned characteristics. Additionally:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With CommonJS, ES modules share the compact syntax and support for cyclic dependencies.&lt;/li&gt;
&lt;li&gt;With AMD, ES modules share being designed for asynchronous loading.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ES modules also have new benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The syntax is even more compact than CommonJS’s.&lt;/li&gt;
&lt;li&gt;Modules have static structures (which can’t be changed at runtime). That helps with static checking, optimized access of imports, dead code elimination, and more.&lt;/li&gt;
&lt;li&gt;Support for cyclic imports is completely transparent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is an example of ES module syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;import &lt;span class="o"&gt;{&lt;/span&gt;importedFunc1&lt;span class="o"&gt;}&lt;/span&gt; from &lt;span class="s1"&gt;'./other-module1.mjs'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
import &lt;span class="o"&gt;{&lt;/span&gt;importedFunc2&lt;span class="o"&gt;}&lt;/span&gt; from &lt;span class="s1"&gt;'./other-module2.mjs'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;function &lt;/span&gt;internalFunc&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  ···
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;exportedFunc&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  importedFunc1&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  importedFunc2&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  internalFunc&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Modules can load each other and use special directives &lt;strong&gt;export and import&lt;/strong&gt; to interchange functionality and  call functions of one module from another one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;export&lt;/em&gt; keyword labels variables and functions that should be accessible from outside the current module.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;import&lt;/em&gt; allows the import of functionality from other modules.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Named imports awesomeness
&lt;/h4&gt;

&lt;p&gt;You can import directly via name,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {square} from './lib/my-math.mjs';
assert.equal(square(3), 9);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or even by renaming it, if it conflicts it with some of your local declarations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {square as sq} from './lib/my-math.mjs';
assert.equal(sq(3), 9);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Remember that named import is not distructing !&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Although both named importing and destructuring look similar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {foo} from './bar.mjs'; // import
const {foo} = require('./bar.mjs'); // destructuring
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But they are quite different:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imports remain connected with their exports.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can destructure again inside a destructuring pattern, but the {} in an import statement can’t be nested.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The syntax for renaming is different:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import {foo as f} from './bar.mjs'; // importing
    const {foo: f} = require('./bar.mjs'); // destructuring
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h4&gt;
  
  
  Namespace imports awesomeness
&lt;/h4&gt;

&lt;p&gt;Namespace imports can be treated as alternative to  named imports. If we namespace-import a module, it becomes an object whose properties are the named exports. For eg&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Module my-math.js has two named exports: square and LIGHTSPEED.

function times(a, b) {
  return a * b;
}
export function square(x) {
  return times(x, x);
}
export const LIGHTSPEED = 299792458;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as myMath from './lib/my-math.mjs'; &amp;lt;--Namespace imports
assert.equal(myMath.square(3), 9);

assert.deepEqual(
  Object.keys(myMath), ['LIGHTSPEED', 'square']);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;I always had an issue with getting my head around these different types of imports, so I spent some time looking around it. I hope you found this use-full. Thanks for the time. ❤️&lt;/p&gt;

&lt;h5&gt;
  
  
  Here are the major refrences
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://exploringjs.com/impatient-js/ch_modules.html"&gt;https://exploringjs.com/impatient-js/ch_modules.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://requirejs.org/"&gt;https://requirejs.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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