<?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: Sagnik Ghosh</title>
    <description>The latest articles on DEV Community by Sagnik Ghosh (@sagnik26).</description>
    <link>https://dev.to/sagnik26</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%2F918135%2Fddd0824f-84ae-4b28-a58c-03ecd8505585.jpeg</url>
      <title>DEV Community: Sagnik Ghosh</title>
      <link>https://dev.to/sagnik26</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagnik26"/>
    <language>en</language>
    <item>
      <title>React Development Essentials: Webpack and Babel Introduction</title>
      <dc:creator>Sagnik Ghosh</dc:creator>
      <pubDate>Mon, 25 Sep 2023 08:11:56 +0000</pubDate>
      <link>https://dev.to/sagnik26/react-development-essentials-webpack-and-babel-introduction-465b</link>
      <guid>https://dev.to/sagnik26/react-development-essentials-webpack-and-babel-introduction-465b</guid>
      <description>&lt;p&gt;Initiating a React project is typically an almost effortless process, largely thanks to the outstanding create-react-app tool. However, while employing it to kickstart yet another empty React project a few days ago, I had an epiphany. I realized that beyond having a vague understanding, I had never delved deep into the inner workings. So, I decided to do just that, and now I can initiate new React projects without relying on magical tools. This article serves as a manifestation of the insights I've gained in the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Webpack and Babel why do we need to know about these?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When I embarked on my coding journey and began learning JavaScript, I didn't pay much attention to file structure. For most of my tutorial projects, I used to write all the code in a single file, resulting in a massive monolithic JavaScript file that proved to be quite challenging to maintain.&lt;/p&gt;

&lt;p&gt;It wasn't until I delved into learning React that I truly grasped the importance of writing organized code. I learned to break down code into smaller, manageable components and then import and use them effectively. Usually, when we start our React projects, we rely on the magical "create-react-app" tool, which automates many tasks, including bundling. This convenience, however, often shields us from the underlying processes. As a developer, I believe it's valuable to understand how these mechanisms work and how to set up a React project from scratch without relying on the "create-react-app" template.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Webpack:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Webpack is a popular open-source JavaScript module bundler. It is primarily used to bundle JavaScript files and assets, such as CSS stylesheets, images, and more into a 'bundle' which we can deploy. As I discussed before that writing monolithic JavaScript is a wrong and inconvenient practice and instead we use code splitting where we write smaller chunks as components into different files and then use require() or import statements to connect them together. So, when we require or import anything from a file, it forms a dependency graph with that file as well as assets(if any).Webpack uses this dependency graph based on import or require and creates a bundle of our JavaScript code and assets. Alongside, it also performs some other optimizations on top of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Babel:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Babel is a JavaScript transpiler which transplies the latest features of JavaScript such as ES6+ and jsx formats into ES5 syntax which is understandable by all kinds browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Let's start configuring React with Webpack and Babel&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;First you need to have npm and node installed in your system. Now, create a folder and run &lt;strong&gt;npm init -y&lt;/strong&gt; to create package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir react-app-webpack-setup
cd react-app-webpack-setup
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get a package.json like this - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{&lt;br&gt;
  "name": "sample",&lt;br&gt;
  "version": "1.0.0",&lt;br&gt;
  "description": "",&lt;br&gt;
  "main": "index.js",&lt;br&gt;
  "scripts": {&lt;br&gt;
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"&lt;br&gt;
  },&lt;br&gt;
  "keywords": [],&lt;br&gt;
  "author": "",&lt;br&gt;
  "license": "ISC"&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After that, create a &lt;strong&gt;.gitignore&lt;/strong&gt; file in the root directory and put the following into that file -&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now let's start with Babel first. Now-a-days, we write most of our JavaScript code in modern ES6 syntax and in React, we usually use jsx format which are not supported by many browsers. This is where Babel comes into play and it helps transpiling modern JavaScript into a format which is understandable by browsers. Let's install Babel like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/core:&lt;/strong&gt; It contains the main functionality of Babel.&lt;br&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/cli:&lt;/strong&gt; It allows us to use Babel from terminal.&lt;br&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-env:&lt;/strong&gt; This preset transforms modern ES6 syntax into common JavaScript.&lt;br&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-react:&lt;/strong&gt; This preset transforms jsx format into vanilla JavaScript.&lt;/p&gt;

&lt;p&gt;Now, create a file &lt;strong&gt;.babelrc&lt;/strong&gt; in the root directory and set it up as following to tell the babel transpiler what presets to use to transpile the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "presets": ["@babel/preset-env", "@babel/preset-react"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are done with our babel setup for now. Let's start with installing and setting up the Webpack now -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i webpack webpack-cli webpack-dev-server --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;webpack:&lt;/strong&gt; It is the main module bundler.&lt;br&gt;
&lt;strong&gt;webpack-cli:&lt;/strong&gt; It is the command line interface for webpack.&lt;br&gt;
&lt;strong&gt;webpack-dev-server:&lt;/strong&gt; It provides a development server for live reloading.&lt;/p&gt;

&lt;p&gt;Now, install React and ReactDOM by running the follwing command on terminal -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing, create a folder &lt;strong&gt;src&lt;/strong&gt; in the root directory and put these three files into that folder as following - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;React setup with Webpack and Babel&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Introduction to setup React with Webpack and Babel&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to create a file &lt;strong&gt;webpack.config.js&lt;/strong&gt; and set it up properly to tell webpack what to do with our source code. But before that we will install some loaders and plugins which will be needed to bundle our JavaScript files as well as static assets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save-dev style-loader css-loader sass-loader babel-loader
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;css-loader:&lt;/strong&gt; It collects CSS from all the CSS files in the app and bundle it into one file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;style-loader:&lt;/strong&gt; It puts all stylings inside &lt;strong&gt;&lt;em&gt;style&lt;/em&gt;&lt;/strong&gt; tag in index.html file which will be created after bundling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sass-loader:&lt;/strong&gt; It is a package which converts sass into css. Though we don't need it in this sample project but still I will set it up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;babel-loader:&lt;/strong&gt; It is a package that transpiles JavaScript using babel and webpack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i html-webpack-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;html-webpack-plugin:&lt;/strong&gt; This plugin automatically creates an HTML file that serves the JavaScript bundles created by Webpack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;webpack.config.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: "./src/index.js",
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: "babel-loader"
            },
            {
                test: /\css$/,
                use: [ 
                    "style-loader",  // 2. Injects styles into DOM
                    "css-loader"    // 1. Turns css into commonjs
                ]
            },
            {
                test: /\.scss$/i,
                use: [
                    "style-loader", // 3. Injects styles into DOM
                    "css-loader",  // 2. Turns css into commonjs
                    "sass-loader" // 1. Turns sass into css
                ],
            },
            {
                test: /\.svg$/,
                loader: 'svg-inline-loader'
            }
        ]
    },
    resolve: {
        extensions: [".js", ".jsx", ".mjs", 
        ".json"],
        symlinks: false,
        cacheWithContext: false,
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: "bundle.[contenthash].js"
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "./src/index.html"
        })
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;entry:&lt;/strong&gt; Here we need to provide the entry path for the webpack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;module:&lt;/strong&gt; Here we need to add the loaders as needed under rules.&lt;br&gt;
For css &amp;amp; sass loader, always follow the right sequence as shown  otherwise there will be issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;resolve:&lt;/strong&gt; Here we need to define the extensions which are there in the project. If you are using typescript, then also add &lt;strong&gt;.ts&lt;/strong&gt; and &lt;strong&gt;.tsx&lt;/strong&gt; in the extensions array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;output:&lt;/strong&gt;  Here we need to provide the output path for the bundle created by webpack. If you follow carefully, I have used the output file name as &lt;strong&gt;bundle.[contenthash].js&lt;/strong&gt;. The &lt;strong&gt;contenthash&lt;/strong&gt; keyword will create a fresh new bundle file after each build followed by a unique name and the latest bundle file will be used in the created index.html.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;plugins:&lt;/strong&gt; Here we need to serve the plugins needed.&lt;/p&gt;

&lt;p&gt;Now, we are done with the setup of the webpack.config.js file and finally let's setup the package.json by adding the follwing scripts to run and build the project -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;package.json&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;open&lt;/strong&gt; flag is used to open the browser just after the server has started and the &lt;strong&gt;hot&lt;/strong&gt; flag is used to enable the hot module replacement feature of webpack which basically updates only the changed code instead of updating the entire code again and again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, to run the application, use the following command -&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;open the server and you should be seeing this - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r5btcnE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luo86nfnncecwl89rotp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r5btcnE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luo86nfnncecwl89rotp.png" alt="React Webpack setup demo" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yay! you have successfully setup React with Webpack and Babel and you can start building new React project on top of it.&lt;/p&gt;

&lt;p&gt;If you want to create a new build, then run the following command -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This blog is all about basic setup for React with webpack and Babel. But, there are a lot of things which can be done on top it like adding a redux-boilerplate which I will discuss in future.&lt;/p&gt;

&lt;p&gt;You can get the code for this sample project in the following github link - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/sagnik26/React-with-Webpack-and-Babel-setup"&gt;https://github.com/sagnik26/React-with-Webpack-and-Babel-setup&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>webpack</category>
      <category>babel</category>
      <category>react</category>
    </item>
    <item>
      <title>What is Execution Context in JavaScript?</title>
      <dc:creator>Sagnik Ghosh</dc:creator>
      <pubDate>Tue, 03 Jan 2023 12:39:52 +0000</pubDate>
      <link>https://dev.to/sagnik26/what-is-an-execution-context-in-javascript-fmk</link>
      <guid>https://dev.to/sagnik26/what-is-an-execution-context-in-javascript-fmk</guid>
      <description>&lt;h2&gt;
  
  
  How JavaScript Code Gets Executed
&lt;/h2&gt;

&lt;p&gt;The browser doesn't understand the high-level JavaScript code that we write in our applications. It needs to be converted into a format that the browser and our computers can understand – machine code.&lt;/p&gt;

&lt;p&gt;The browser's JavaScript engine then creates a special environment to handle the transformation and execution of this JavaScript code. This environment is known as the Execution Context. The Execution Context contains the code that's currently running, and everything that aids in its execution. During the Execution context runtime, the variables and functions are stored in memory, executable byte-code gets generated, and the code gets executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Context types
&lt;/h2&gt;

&lt;p&gt;There are two kinds of Execution Context in JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Global Execution Context (&lt;strong&gt;GEC&lt;/strong&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functional Execution Context (&lt;strong&gt;FEC&lt;/strong&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt;&lt;br&gt;
Whenever the JavaScript engine receives a script file, it first creates a default Execution Context known as the Global Execution Context. The GEC is the base/default Execution Context where all JavaScript code that is not inside of a function gets executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; For every JavaScript file, there can only be one GEC.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional Execution Context (GEC)&lt;/strong&gt;&lt;br&gt;
Whenever a function is called, the JavaScript engine creates a different type of Execution Context known as a Function Execution Context (FEC) within the GEC to evaluate and execute the code within that function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Since every function call gets its own FEC, there can be more than one FEC in the run-time of a script.&lt;/p&gt;

&lt;h2&gt;
  
  
  How are Execution Contexts Created?
&lt;/h2&gt;

&lt;p&gt;The creation of an Execution Context (GEC or FEC) happens in two phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creation Phase&lt;/li&gt;
&lt;li&gt;Execution Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;CREATION PHASE&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the creation phase, the Execution Context is first associated with an Execution Context Object (ECO). The Execution Context Object stores a lot of important data which the code in the Execution Context uses during its run-time.&lt;/p&gt;

&lt;p&gt;The creation phase occurs in 3 stages. These stages are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creation of the Variable Object (VO)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creation of the Scope Chain&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setting the value of the this keyword&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CREATION PHASE : Creation Of The Variable Object (VO)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Variable Object (VO) is an object-like container created within an Execution Context. It stores the variables and function declarations defined within that Execution Context.&lt;/p&gt;

&lt;p&gt;In the GEC, for each variable declared with the var keyword, a property is added to VO that points to that variable and is set to 'undefined'.&lt;/p&gt;

&lt;p&gt;Also, for every function declaration, a property is added to the VO, pointing to that function, and that property is stored in memory. This means that all the function declarations will be stored and made accessible inside the VO, even before the code starts running.&lt;/p&gt;

&lt;p&gt;The FEC, on the other hand, does not construct a VO. Rather, it generates an array-like object called the 'argument' object, which includes all of the arguments supplied to the function. Learn more about the argument object here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CREATION PHASE : Creation of The Scope Chain&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After the creation of the Variable Object (VO) comes the creation of the Scope Chain as the next stage in the creation phase of an Execution Context.&lt;/p&gt;

&lt;p&gt;Scope in JavaScript is a mechanism that determines how accessible a piece of code is to other parts of the codebase. Scope answers the questions: from where can a piece of code be accessed? From where can't it be accessed? What can access it, and what can't?&lt;/p&gt;

&lt;p&gt;Each Function Execution Context creates its scope: the space/environment where the variables and functions it defined can be accessed via a process called Scoping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CREATION PHASE : Setting The Value of The "this" Keyword&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next and final stage after scoping in the creation phase of an Execution Context is setting the value of the this keyword.The JavaScript this keyword refers to the scope where an Execution Context belongs. Once the scope chain is created, the value of 'this' is initialized by the JS engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;EXECUTION PHASE&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Finally, right after the creation phase of an Execution Context comes the execution phase. This is the stage where the actual code execution begins.&lt;/p&gt;

&lt;p&gt;Up until this point, the VO contained variables with the values of undefined. If the code is run at this point it is bound to return errors, as we can't work with undefined values. At this stage, the JavaScript engine reads the code in the current Execution Context once more, then updates the VO with the actual values of these &lt;br&gt;
variables. Then the code is parsed by a parser, gets transpired to executable byte code, and finally gets executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Call Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Call Stack keeps track of all the Execution Contexts created during the life cycle of a script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript is a single-threaded language, which means that it is capable of only executing a single task at a time. Thus, when other actions, functions, and events occur, an Execution Context is created for each of these events. Due to the single-threaded nature of JavaScript, a stack of piled-up execution contexts to be executed is created, known as the Execution Stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When scripts load in the browser, the Global context is created as the default context where the JS engine starts executing code and is placed at the bottom of the execution stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The JS engine then searches for function calls in the code. For each function call, a new FEC is created for that function and is placed on top of the currently executing Execution Context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Execution Context at the top of the Execution stack becomes the active Execution Context, and will always get executed first by the JS engine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As soon as the execution of all the code within the active Execution Context is done, the JS engine pops out that particular function's Execution Context of the execution stack, moves towards the next below it, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>discuss</category>
    </item>
    <item>
      <title>What is JWT(JSON WEB TOKEN) &amp; how it works</title>
      <dc:creator>Sagnik Ghosh</dc:creator>
      <pubDate>Tue, 27 Dec 2022 10:45:03 +0000</pubDate>
      <link>https://dev.to/sagnik26/what-is-jwtjson-web-token-how-it-works-2fik</link>
      <guid>https://dev.to/sagnik26/what-is-jwtjson-web-token-how-it-works-2fik</guid>
      <description>&lt;h2&gt;
  
  
  What is JWT ?
&lt;/h2&gt;

&lt;p&gt;JSON Web Token(JWT) is an open standard(RFC 7519) that defines secure transmission of information between between two parties as a JSON object. JWTs can be signed using a public/private key pair using RSA algorithm and that's why the information shared between two parties is verified and secured.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you use JWTs?
&lt;/h2&gt;

&lt;p&gt;There are basically two scenarios where JSON web tokens are useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authorization:&lt;/strong&gt;&lt;br&gt;
This is the most common use-case of JWT. When user is logged in, each subsequent request will include a JWT token which allows the user to access routes, services, &amp;amp; resources that are permitted with the token.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Information Exchange:&lt;/strong&gt;&lt;br&gt;
JWTs provide a secure way to share sensitive information between two parties. JWTs can be signed using public/private key pairs &amp;amp; that's why you can be sure about the senders who they say they are.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Structure of JWT
&lt;/h2&gt;

&lt;p&gt;JWT consists of three parts separated by dots( . ),&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Header&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Payload&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Signature&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header.Payload.Signature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;p&gt;The header consists of two parts such as the type of the token and the signing algorithm. The type of the token is JWT. The algorithms used are HMAC, SHA256 or RSA.&lt;/p&gt;

&lt;h4&gt;
  
  
  example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "alg": "HS256",
  "typ": "JWT"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;The payload contains the claims such as the statements about an entity and additional data. There are three types of claims - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Registered claims (&lt;strong&gt;iss&lt;/strong&gt;, &lt;strong&gt;exp&lt;/strong&gt;, &lt;strong&gt;sub&lt;/strong&gt;, &lt;strong&gt;aud&lt;/strong&gt;, etc).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Public claims.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Private claims.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "sub": "1001",
  "iat": 1894561234,
  "name": "Sagnik ghosh",
  "admin": true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Signature
&lt;/h3&gt;

&lt;p&gt;The signature is the most important part of a JWT which is calculated by encoding the header, the payload and a secret using encoding techniques like Base64url or HMACSHA256.&lt;/p&gt;

&lt;h4&gt;
  
  
  example:
&lt;/h4&gt;

&lt;p&gt;For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to play with JWT, you can go to &lt;a href="https://jwt.io/" rel="noopener noreferrer"&gt;jwt.io&lt;/a&gt; and get your hands dirty.&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%2F47bpkj9hdm7jo69ge5uu.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%2F47bpkj9hdm7jo69ge5uu.png" alt="JWT"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How JWT works?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever the user wants to access a protected route or resource, the user agent should send the JWT, typically in the Authorization header using the Bearer schema.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorization: Bearer &amp;lt;token&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The server's protected routes will check for a valid JWT in the
Authorization header, and if it's present, the user will be 
allowed to access protected resources.&lt;/li&gt;
&lt;/ul&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%2F4pip66ftoiy0dwfahio1.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%2F4pip66ftoiy0dwfahio1.png" alt="JWT working"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of using JWT
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As JSON is more verbose than XML, so, when it is encoded it's size becomes smaller and more compact, which makes JWT better than SAML(Security Assertion Markup Language Tokens) which is another standard for secure transmission of data between two parties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Signing JSON with digital signature is much easier which makes the process of validating the signature more simpler.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSON parsers are common in most programming languages because they map directly to objects. This makes it easier to work with JWT.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security issues
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Signed tokens though protected against tampering, is readable by anyone. So, any secret information should not be put inside the payload or header elements of a JWT unless it is encrypted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HTTPS must be used to secure the Authorization headers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a JWT token is assigned, it is set to automatically expire after some time. But if an attacker gets the token before it expires then, that leads to various exploits. So, one must build a token revocation list on the server to invalidate tokens in order to protect the system from such attacks. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
