<?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: Nikhil </title>
    <description>The latest articles on DEV Community by Nikhil  (@nikhiltatpati).</description>
    <link>https://dev.to/nikhiltatpati</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%2F370935%2Ff6e60fa6-7092-4e99-89c6-f292b9de2941.png</url>
      <title>DEV Community: Nikhil </title>
      <link>https://dev.to/nikhiltatpati</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikhiltatpati"/>
    <language>en</language>
    <item>
      <title>In Depth Guide On Hoisting In JavaScript</title>
      <dc:creator>Nikhil </dc:creator>
      <pubDate>Sun, 06 Nov 2022 18:33:25 +0000</pubDate>
      <link>https://dev.to/nikhiltatpati/in-depth-guide-on-hoisting-in-javascript-3m9h</link>
      <guid>https://dev.to/nikhiltatpati/in-depth-guide-on-hoisting-in-javascript-3m9h</guid>
      <description>&lt;p&gt;In this article we will deep dive into hoisting concept in JavaScript. Before we start let's brush up some programming fundamentals.&lt;/p&gt;

&lt;h3&gt;
  
  
  Programming Fundamentals
&lt;/h3&gt;

&lt;p&gt;Computer program is just a set of instructions given to the computer to execute. We write programs in high level language (Python, JavaScript, etc.) which is easily understood/read by humans but computers do not understand this high level language. For computers to understand our program, it has to be converted into low level code. So in order to perform this functionality, languages use &lt;code&gt;Compilers&lt;/code&gt; and &lt;code&gt;Interpreters&lt;/code&gt;. Compilers and Interpreters take human readable code and convert it to computer readable machine code.&lt;/p&gt;

&lt;h5&gt;
  
  
  Difference between Compilers and Interpreters :
&lt;/h5&gt;

&lt;p&gt;Compiler, scans the entire program in one go and translates it as a whole into machine code.&lt;/p&gt;

&lt;p&gt;Where as Interpreter, translates program one statement at a time. Before interpreting next statement, previous statement is executed.&lt;/p&gt;

&lt;p&gt;Therefore, compiled languages are faster than interpreted languages as compiled language overall execution time is less compared to interpreted language.&lt;/p&gt;

&lt;h5&gt;
  
  
  Some examples of compiled and interpreted languages are:
&lt;/h5&gt;

&lt;p&gt;Compiled languages : C, C++, Go, Rust, etc.&lt;/p&gt;

&lt;p&gt;Interpreted languages : Python, Ruby, PHP, etc.&lt;/p&gt;

&lt;p&gt;Now, coming back to JavaScript. Let's check whether JavaScript is interpreted or compiled.&lt;/p&gt;

&lt;p&gt;So according to Google and MDN docs, JavaScript is an interpreted language.&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%2Fp3d19et1musa8hmoqxge.jpeg" 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%2Fp3d19et1musa8hmoqxge.jpeg" alt="JavaScript is interpreted language"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means that JavaScript code is executed line by line, top to bottom.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting in JavaScript
&lt;/h3&gt;

&lt;p&gt;Now that we know JavaScript is interpreted language, let's try to solve below problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(platform)

var platform = "Dev"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s try to execute the code line by line in our mind, same as the interpreter would do.&lt;/p&gt;

&lt;p&gt;1st line is console.log(platform).&lt;/p&gt;

&lt;p&gt;This should give “Reference Error: platform is not defined” because as per interpreter there is no as such variable called platform.&lt;/p&gt;

&lt;p&gt;But you will be amazed to know that the correct output is "undefined". Wondering how? 🤔&lt;/p&gt;

&lt;p&gt;This is where concept called &lt;code&gt;JavaScript Hoisting&lt;/code&gt; comes into picture.&lt;/p&gt;

&lt;p&gt;According to MDN docs,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code".&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Do you really think that the interpreter takes our code and tosses it up before execution? 🤔 Let's dig in more deeper.&lt;/p&gt;

&lt;p&gt;Getting output &lt;code&gt;undefined&lt;/code&gt; is weird, this means that our variable is declared but not initialised. This also means that the interpreter somehow knows what's happened in next line 🤯. Interpreter here is showing some glimpses of behaviour like a Compiler. &lt;/p&gt;

&lt;p&gt;The real reason behind this is that JavaScript is just-in-time compiled before it’s interpreted. Because of which concepts like hoisting and lexical scoping exist. In this compilation phase, all variables and functions in program is assigned with their appropriate scope.&lt;/p&gt;

&lt;p&gt;So, the best way to think about this is that all declarations, both variables and functions, are processed first and scope is assigned to them, before any part of your code is executed. Because of this, interpreter knows about the declaration even before it starts execution. &lt;/p&gt;

&lt;p&gt;Now that we know JavaScript code is parsed before any execution begins then why does JavaScript spec say that variable and function declarations are moved to the top of their scope, before the code is executed? &lt;/p&gt;

&lt;p&gt;To maintain simplicity, so that we can just think about the program as if it's executed by the JS engine in a single pass, top-to-down approach. Moving forward we will also understand examples with this ideology.&lt;/p&gt;

&lt;p&gt;Now let's understand how hoisting differs in &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting in &lt;code&gt;var&lt;/code&gt; variables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a); // undefined
var a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see in above example that &lt;code&gt;console.log&lt;/code&gt; prints &lt;code&gt;undefined&lt;/code&gt; and not &lt;code&gt;ReferenceError&lt;/code&gt; this is because after hoisting, the above program behaves as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a;
console.log(a);
a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that &lt;code&gt;var&lt;/code&gt; declaration is hoisted at the top of the program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting in &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables
&lt;/h3&gt;

&lt;p&gt;Hoisting in &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables is little different compared to &lt;code&gt;var&lt;/code&gt; variable hoisting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a);
// ReferenceError

let a = "Hello";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when we run the above code, we get &lt;code&gt;Reference Error : Cannot access 'a' before initialization&lt;/code&gt;. From this error we can notice that the variable &lt;code&gt;a&lt;/code&gt; is defined in the scope but it cannot be accessed before initialization.&lt;/p&gt;

&lt;p&gt;So the main catch is that, with variables declared with &lt;code&gt;var&lt;/code&gt; keyword are automatically initialised to undefined at the top of the scope whereas variable declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords are not initialised. &lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting in function declarations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet(); // Hello

function greet() {
    console.log("Hello");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code works fine even though greet function is called before it's even declared. In JavaScript the entire function is hoisted to the top.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. I hope that now you have clear understanding of JavaScript hoisting. Feel free to correct me if i have made any mistakes.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Improve Web Performance With Web Workers</title>
      <dc:creator>Nikhil </dc:creator>
      <pubDate>Wed, 27 Apr 2022 05:55:03 +0000</pubDate>
      <link>https://dev.to/nikhiltatpati/improve-web-performance-with-web-workers-3jal</link>
      <guid>https://dev.to/nikhiltatpati/improve-web-performance-with-web-workers-3jal</guid>
      <description>&lt;p&gt;If your are looking to optimise or improve performance of your web application then you should definitely use web workers but still many people are unaware of it and don't know how to setup with web application. In this post i'll do my best to help you with web workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Web Worker?
&lt;/h2&gt;

&lt;p&gt;In simple terms web worker is nothing but a javascript file that is executed by the browser in background separate from your application javascript file.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Web Worker can increase your application performance?
&lt;/h2&gt;

&lt;p&gt;As Web worker runs separate from the main javascript file you can put your computational intensive functions present in main javascript file into web worker file so main thread can run smoothly with no hiccups in user interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to setup web worker
&lt;/h2&gt;

&lt;p&gt;Create a javascript file named &lt;code&gt;worker.js&lt;/code&gt; as shown below in your project directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// worker.js
console.log("Hello from web worker!")
this.onmessage = e =&amp;gt; {
  console.log('worker.js: Message received from main script', e.data)
  this.postMessage('Hello main')
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;onmessage&lt;/strong&gt; : It is used to listen to any messages sent by the main application thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;postMessage&lt;/strong&gt; : It is used send message to main application thread.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;main.js&lt;/code&gt; file you can instantiate worker object by passing &lt;code&gt;worker.js&lt;/code&gt; path address to Worker class and then similarly you can use &lt;code&gt;onmessage&lt;/code&gt; and &lt;code&gt;postMessage&lt;/code&gt; function to communicate with the worker thread.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// main.js
const worker = new Worker('worker.js')
worker.postMessage('Hello Worker')
worker.onmessage = e =&amp;gt; {
  console.log('main.js: Message received from worker:', e.data)
}
// if you want to "uninstall" the web worker then use:
// worker.terminate()

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

&lt;/div&gt;



&lt;p&gt;You can use &lt;code&gt;worker.terminate()&lt;/code&gt; function to terminate your web worker execution.&lt;/p&gt;

&lt;p&gt;With this simple steps now you can put your compute intensive functions in web worker and communicate with main application thread once it's executed.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. Follow me on &lt;a href="https://twitter.com/nikhiltatpati"&gt;Twitter&lt;/a&gt; if you are interested in web development and javascript content.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React: useEffect vs useLayoutEffect</title>
      <dc:creator>Nikhil </dc:creator>
      <pubDate>Tue, 16 Feb 2021 09:57:53 +0000</pubDate>
      <link>https://dev.to/nikhiltatpati/react-useeffect-vs-uselayouteffect-1bpd</link>
      <guid>https://dev.to/nikhiltatpati/react-useeffect-vs-uselayouteffect-1bpd</guid>
      <description>&lt;p&gt;If you have just started working on React or have ever written a functional component, I am pretty sure that you may have come across useEffect hook and you may know that it is used to perform some side effects like data fetching, sending HTTP requests, etc. But in this post we will talk about another react hook called useLayoutEffect and see how it is different from useEffect hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  useLayoutEffect
&lt;/h2&gt;

&lt;p&gt;To be honest there is not much difference between useEffect and useLayoutEffect the only noticable difference is that useLayoutEffect runs before browser paints DOM elements on the screen as you can see in the image below.  &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%2F46aktwgrx0gcti8lcev6.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%2F46aktwgrx0gcti8lcev6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Credits- Donavon &lt;a href="https://github.com/donavon/hook-flow" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the image useEffect runs after the UI is painted. The only case where useLayoutEffect is useful and should be preferred over useEffect is when you are directly making DOM changes. As DOM changes would be done before it is painted on the screen and thus making the experience smooth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;One should prefer useEffect most of the time until and unless you are mutating DOM or want to do some performance measurements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up!
&lt;/h2&gt;

&lt;p&gt;Thank you for your time!! Let's connect to learn and grow together. &lt;a href="https://www.linkedin.com/in/nikhil-tatpati-097184194/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.buymeacoffee.com/nikhiltatpati" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>Build an In-Browser Transpiler</title>
      <dc:creator>Nikhil </dc:creator>
      <pubDate>Tue, 02 Feb 2021 09:23:51 +0000</pubDate>
      <link>https://dev.to/nikhiltatpati/build-an-in-browser-transpiler-pfc</link>
      <guid>https://dev.to/nikhiltatpati/build-an-in-browser-transpiler-pfc</guid>
      <description>&lt;p&gt;Recently I wanted to build an In-Browser transpiler for my react project. So in this article I will try to explain the process of building one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Transpiling
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Let us first understand what is transpiling?
&lt;/h3&gt;

&lt;p&gt;Transpiling is nothing but process of converting your source code from one language to another. Transpiling is done by a software program called transpilers or source-to-source compilers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we need transpilers?
&lt;/h3&gt;

&lt;p&gt;We know browser only understands javaScript. So transpilers helps us to write different languages, like CoffeeScript, TypeScript, or ClojureScript which during execution can be compiled to plain javaScript.&lt;/p&gt;

&lt;p&gt;The most famous transpiler in javaScript world is Babel, but in this article we will use esbuild which I personally found out to be quite fast and it also allows us to do In-Browser bundling( will talk about this in future post😉 ).&lt;/p&gt;

&lt;h2&gt;
  
  
  What are we building here?
&lt;/h2&gt;

&lt;p&gt;We will be building a simple react application that will  take the JSX code in textarea and will transpile the code into ES2015 javaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-transpiler
cd react-transpiler
npm install esbuild-wasm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Node and npm should be preinstalled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Building The App
&lt;/h3&gt;

&lt;p&gt;After initializing, open the code editor at current path.&lt;/p&gt;

&lt;h4&gt;
  
  
  Index.js
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as esbuild from "esbuild-wasm";
import { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";

const App = () =&amp;gt; {
  const ref = useRef&amp;lt;any&amp;gt;();
  const [input, setInput] = useState("");
  const [code, setCode] = useState("");

  const startService = async () =&amp;gt; {
    ref.current = await esbuild.startService({
      worker: true,
      wasmURL: "https://unpkg.com/esbuild-wasm@0.8.27/esbuild.wasm",
    });
  };
  useEffect(() =&amp;gt; {
    startService();
  }, []);

  const onClick = async () =&amp;gt; {
    if (!ref.current) {
      return;
    }

    const result = await ref.current.transform(input, {
      loader: "jsx",
      target: "es2015",
    });

    setCode(result.code);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;JSX Transpiler&amp;lt;/h2&amp;gt;
      &amp;lt;textarea
        value={input}
        style={{ height: "300px", width: "500px" }}
        onChange={(e) =&amp;gt; setInput(e.target.value)}
      &amp;gt;&amp;lt;/textarea&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onClick}&amp;gt;Transpile&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;pre&amp;gt;{code}&amp;lt;/pre&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

ReactDOM.render(&amp;lt;App /&amp;gt;, document.querySelector("#root"));

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&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%2Fi%2Fsyl9bqoj4nc68pdy1i96.jpg" 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%2Fi%2Fsyl9bqoj4nc68pdy1i96.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hurray🎉🎉&lt;/p&gt;

&lt;p&gt;We built an In-Browser JSX transpiler using esbuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up!
&lt;/h2&gt;

&lt;p&gt;Thank you for your time!! Let's connect to learn and grow together. &lt;a href="https://www.linkedin.com/in/nikhil-tatpati-097184194/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.buymeacoffee.com/nikhiltatpati" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>esbuild</category>
      <category>transpiler</category>
      <category>react</category>
    </item>
  </channel>
</rss>
