<?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>Forem: Aman Sethi</title>
    <description>The latest articles on Forem by Aman Sethi (@amansethi00).</description>
    <link>https://forem.com/amansethi00</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%2F523778%2F0c8e54ae-6a8e-4849-8ebe-e2e477b82f57.jpeg</url>
      <title>Forem: Aman Sethi</title>
      <link>https://forem.com/amansethi00</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amansethi00"/>
    <language>en</language>
    <item>
      <title>Thoughts of a junior software engineer on useEvent hook in react 18</title>
      <dc:creator>Aman Sethi</dc:creator>
      <pubDate>Sat, 02 Jul 2022 20:08:59 +0000</pubDate>
      <link>https://forem.com/amansethi00/thoughts-of-a-junior-software-engineer-on-useevent-hook-in-react-18-56cn</link>
      <guid>https://forem.com/amansethi00/thoughts-of-a-junior-software-engineer-on-useevent-hook-in-react-18-56cn</guid>
      <description>&lt;p&gt;&lt;br&gt;
&lt;code&gt;A Hook to define an event handler with an always-stable function identity.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The above line is from the description of the useEvent hook in the rfc released by React team - &lt;a href="https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md"&gt;https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React team recently announced they are working on a new hook called useEvent, I am fascinated by the use case it solves in our application code -&lt;/p&gt;

&lt;p&gt;1) to prevent unnecessary re-render of child components even though the function uses the latest values from the parent component. example - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cUeGFj9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhr928jb6zksvkvvhvw5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cUeGFj9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhr928jb6zksvkvvhvw5.png" alt="simple code" width="880" height="684"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in this code, on every user input change, the ChildComponent will re-render,this can easily be prevented with a useCallback with dependencies on text, but that would not be useful as in dependencies, we will use text, and it will still cause the child to re-render on every input change, here comes our saviour useEffect, so if we wrap the sendMessage function with this hook, the child won't re-render, and when called on click it will consume the latest state of text in the parent too, this is really useful as it removes the pain of finding the right dependencies to be added in the useCallback to improve optimisation, let's see what have we changed with useEvent -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HW-2dq7c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p240x1941mbupekinc84.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HW-2dq7c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p240x1941mbupekinc84.png" alt="code with usehook" width="880" height="684"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;2) is not a replacement of useCallback hook &lt;br&gt;
The main difference I see between useEvent and useCallback is if the functions need to be called in child with the latest data at a later stage on change of event, we should use useEvent(whenever it is available in react offical release) otherwise when we want component's UI to be updated(or show calculated UI data) on change of a state in parent we should use useCallback.&lt;/p&gt;

&lt;p&gt;The useEvent can solve the issue of revalidating too much of the useCallback but it doesn't solve every usecase and is discussed more on the &lt;a href="https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md#when-useevent-should-not-be-used"&gt;rfc&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;But in the meanwhile, I wrote this article. In the current version of React, the useEvent hook is not available. So keep your eyes peeled for another update! Thank you for reading. I hope you like it. 👋🏼&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Typescript = Javascript with benefits</title>
      <dc:creator>Aman Sethi</dc:creator>
      <pubDate>Sun, 09 May 2021 18:23:36 +0000</pubDate>
      <link>https://forem.com/amansethi00/typescript-javascript-with-benefits-8eo</link>
      <guid>https://forem.com/amansethi00/typescript-javascript-with-benefits-8eo</guid>
      <description>&lt;p&gt;Hello everyone,hope you are taking good care of yourself in these times and coding everyday.Js is a must learn language when working with web related projects,be it frontend related or just backend.Modern libraries and frameworks use JS and many developers love using it but it doesn't mean we should totally outlook areas where JS is not efficient.Since JS is dynamic typed languages, variables can be abused in the codebase and it will not be known to the program until it is executed, that is during runtime.&lt;/p&gt;

&lt;p&gt;Here comes Typescript to save you by throwing errors everytime some types go wrong,don't worry we will look into types more below.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Typescript?
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeScript is an open-source language which builds on JavaScript, one of the world’s most used tools, by adding static type definitions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's first look at the difference between static typed vs dynamic typed language-&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Type
&lt;/h3&gt;

&lt;p&gt;The language where types of variables are checked during compile time.Here in the code if we defined variable of type string and assign a number later to it,the compiler will show error.&lt;br&gt;
Example - C++,C,Java&lt;/p&gt;
&lt;h3&gt;
  
  
  Dynamic Type
&lt;/h3&gt;

&lt;p&gt;The language where types are checked at runtime(during execution). &lt;br&gt;
Example:Python,Javascript&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typescript is essentially javascript with static type&lt;/strong&gt;,which means we won't get the freedom to change variable types without letting  typescript know in advance,to understand more what I mean look at the example below. &lt;/p&gt;
&lt;h1&gt;
  
  
  Why Typescript
&lt;/h1&gt;

&lt;p&gt;To explain why ts ,let's see why not js,we all know javascript is hated for its unexpected behaviour like in the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//calling the function with a number &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//calling the function with a string&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman));

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

&lt;/div&gt;



&lt;p&gt;Our plain old javascript will not complain(throw an error) and will show the behavior we all are familiar with,but when working with larger codebase in organisations this behaviour might get caught only during runtime.Now here comes typescript to save us from doing these mistakes by static typing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does TS solve this
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;All valid JavaScript code is also TypeScript code. You might get type-checking errors, but that won't stop you from running the resulting JavaScript. While you can go for stricter behavior, that means you're still in control.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The power of TS comes from defining types.When writing functions we define types of the parameters required,this way ts checks the type of arguements passed matches the type of parameters.&lt;/p&gt;

&lt;p&gt;Let's look at the code for the above example in typescript and observe the behaviour&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//will give error saying "Argument of type 'string' is not assignable to parameter of type 'number'"&lt;/span&gt;
&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//will give error saying "Argument of type 'boolean' is not assignable to parameter of type 'number'"&lt;/span&gt;
&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//ts happily compiles it,no error&lt;/span&gt;
&lt;span class="nx"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just by writing some types, TS saved our debugging time,this part is super important as when code grows and if we were using js these errors could only get caught in the runtime.&lt;/p&gt;

&lt;p&gt;So ok writing types saves us from errors but is there more which ts does?&lt;br&gt;
Added benefits which comes along using ts are:&lt;/p&gt;
&lt;h2&gt;
  
  
  1.Speeds up development
&lt;/h2&gt;

&lt;p&gt;When used with proper types ts doesn't let us use other types and in a component we don't have to look in the file it was defined to send the props requires as ts will show error id more or less than defined props are send in the component.&lt;br&gt;
Look at the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ComponentProp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ComponentProp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* using the code */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when passing props in this Component in out App.js,it checks and if it doesn't match the ComponentProp types shoes error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;56&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; {/&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;shows&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="nx"&gt;saying&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Type 'number' is not assignable to type 'string'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&amp;gt;{/&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;shows&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="nx"&gt;saying&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Property 'y' is missing in type '{ x: string; }' but required in type 'ComponentProp'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This way just looking at the error we know what to pass as props and also with typechecking of ts prevents passing extra or less number of arguements.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Refactoring becomes super easy
&lt;/h2&gt;

&lt;p&gt;Now one of the dev from your team decides to refactor a component or a function,she can't just add some parameters on the Component/function and forget to add it in the rest of the codebase as typescript will throw errors guiding her to solve the error with &lt;strong&gt;descriptive error messages&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Provides first layer of documentation
&lt;/h2&gt;

&lt;p&gt;So you wrote code with types with good type names and now whenever some new from another team needs to understand the codebase,she could get the rough idea by looking at the types for the component/function,just that simple.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In my opinion to use TS or to keep using JS in projects is choice which entirely depends on your project,as if you are working solo on a project it is ok to use JS and get your work done with it as chances are it will be you who is going to maintain/refactor the code.When working with teams is when TS can be used along with its superpowers,ofcourse.&lt;/p&gt;

&lt;p&gt;Thank you for reading till here,I hope it added value to your learning.Please feel free to comment your thoughts on this blog.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I actively tweet my learning.For more updates follow &lt;a href="https://twitter.com/amansethi00"&gt;Aman Sethi&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How is React different from vanilla JS</title>
      <dc:creator>Aman Sethi</dc:creator>
      <pubDate>Fri, 11 Dec 2020 09:14:06 +0000</pubDate>
      <link>https://forem.com/amansethi00/how-is-react-different-from-vanilla-js-4lfn</link>
      <guid>https://forem.com/amansethi00/how-is-react-different-from-vanilla-js-4lfn</guid>
      <description>&lt;h1&gt;
  
  
  Vanilla Js
&lt;/h1&gt;

&lt;p&gt;VanillaJS is a name to refer to using plain JavaScript without any additional libraries. All major web browsers have a built-in JavaScript engine that executes the code on the user's device. It is the language to communicate with the browsers directly. Let it be any framework Angular, React, etc they are all built on top of vanilla JS. When making small web apps using vanilla JS or any of the framework wouldn't make any big of a difference. But when there are more functionalities to be added code gets messier.&lt;/p&gt;

&lt;h1&gt;
  
  
  React
&lt;/h1&gt;

&lt;p&gt;It is a framework built on vanilla JS. Think of it as the middleware to communicate with the browser. Modern browsers provide many functionalities that improve user experience and provide good visual animations also. To communicate with the browser we use vanilla JS. But the code gets many times too long making things look complicated in vanilla JS. React hides these complexities and let a developer access the features of the browser using less code. Also, I would like to remind you here that &lt;em&gt;React is a &lt;code&gt;declarative&lt;/code&gt; language for this reason.&lt;/em&gt;  &lt;/p&gt;

&lt;p&gt;React is a &lt;code&gt;Component-Based&lt;/code&gt; framework. This means components are once written and used multiple times within the app. In vanilla JS it is not the same case as everything has to be hard-wired to make things work.&lt;/p&gt;

&lt;p&gt;React can also render on the server using Node and power mobile apps using &lt;code&gt;React Native&lt;/code&gt;.Many companies use React Native for their mobile app because the same code can be deployed to both &lt;code&gt;Play Store&lt;/code&gt; and &lt;code&gt;App Store&lt;/code&gt;.This helps them ship out new features and information to their users in less time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I hope that this article helped you learn something new today. If it did you can follow me on &lt;a href="https://twitter.com/amansethi00"&gt;Twitter&lt;/a&gt;, to stay connected.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Shallow Copy vs Deep Copy in JAVASCRIPT</title>
      <dc:creator>Aman Sethi</dc:creator>
      <pubDate>Thu, 03 Dec 2020 17:19:32 +0000</pubDate>
      <link>https://forem.com/amansethi00/shallow-copy-vs-deep-copy-in-javascript-11d6</link>
      <guid>https://forem.com/amansethi00/shallow-copy-vs-deep-copy-in-javascript-11d6</guid>
      <description>&lt;p&gt;Shallow copy and deep copy are used to copy objects in javascript. Before directly jumping into the differences first let's see why objects are not copied using &lt;code&gt;=&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NF4jF07---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qpc64dx9zf0m6gvqcksx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NF4jF07---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qpc64dx9zf0m6gvqcksx.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;=&lt;/code&gt; operator makes a reference to the same memory location and does not actually copy. Let's understand it with the example-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gywtCZoh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bl9q0cyjzg6irappu5ck.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gywtCZoh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bl9q0cyjzg6irappu5ck.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus it is clear from the example that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are both references to same memory location.So we cannot copy using &lt;code&gt;=&lt;/code&gt; operator.&lt;/p&gt;

&lt;h1&gt;
  
  
  Shallow Copy
&lt;/h1&gt;

&lt;p&gt;It is used to copy objects which are not nested(i.e. do not contain another object within them). There are two methods -&lt;/p&gt;

&lt;p&gt;1.using spread(...)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zUmObYzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0e6zoncbtgctwcw516ls.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zUmObYzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0e6zoncbtgctwcw516ls.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here when &lt;code&gt;c&lt;/code&gt; is modified,&lt;code&gt;a&lt;/code&gt; remains the same so &lt;code&gt;c&lt;/code&gt; is a copy of &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;2.using Object.assign() method&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3oW6gRso--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5n00arekxso8339qhelf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3oW6gRso--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5n00arekxso8339qhelf.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here when &lt;code&gt;d&lt;/code&gt; is modified, &lt;code&gt;a&lt;/code&gt; remains the same so &lt;code&gt;d&lt;/code&gt; is a copy of &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deep Copy
&lt;/h1&gt;

&lt;p&gt;Deep copy copies the objects irrespective of whether it is nested or not.It uses JSON.stringify() and JSON.parse() methods.Let's understand it with an example that has nested objects-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hQgLHFry--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fdm9bea1yynmqpmqah11.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hQgLHFry--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fdm9bea1yynmqpmqah11.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Applying one of the methods of shallow copy-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a2bKkipa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4eh5fvhit9v509lazegk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a2bKkipa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4eh5fvhit9v509lazegk.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Observe that when the nested object of &lt;code&gt;b&lt;/code&gt; is modified &lt;code&gt;a&lt;/code&gt; also gets modified. The reason behind this is &lt;code&gt;shallow copy&lt;/code&gt; in case of nested objects use the reference of nested objects and only make copy the parent object.&lt;/p&gt;

&lt;p&gt;Applying JSON.stringify() and JSON.parse() methods for deep copy-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tV7OGF8e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hvf2zey6ej4dt2oc52td.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tV7OGF8e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hvf2zey6ej4dt2oc52td.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here when the nested object of &lt;code&gt;c&lt;/code&gt; is modified &lt;code&gt;a&lt;/code&gt; remains the same.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I hope this blog helped you understand the differences between &lt;code&gt;Deep Copy&lt;/code&gt; and &lt;code&gt;Shallow Copy&lt;/code&gt; in javascript.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How I created a translator web app using HTML,CSS and JAVASCRIPT</title>
      <dc:creator>Aman Sethi</dc:creator>
      <pubDate>Wed, 02 Dec 2020 05:03:46 +0000</pubDate>
      <link>https://forem.com/amansethi00/how-i-created-a-translator-web-app-using-html-css-and-javascript-2724</link>
      <guid>https://forem.com/amansethi00/how-i-created-a-translator-web-app-using-html-css-and-javascript-2724</guid>
      <description>&lt;p&gt;There are three things that are needed for an application-input,processing and output.For processing in this app means translation.We will be using &lt;code&gt;funtranslation api&lt;/code&gt; which returns translated content.&lt;/p&gt;

&lt;h1&gt;
  
  
  Funtranslations API
&lt;/h1&gt;

&lt;p&gt;There are so many translation languages available which can be used for translations.Here we will be using minion language and to get response from the api we need to send the url in the given format-&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%2Fi%2F3ui04si33n441kiccxfl.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%2Fi%2F3ui04si33n441kiccxfl.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Taking user input
&lt;/h1&gt;

&lt;p&gt;For any translator app, the first thing needed is the user's input so we that using &lt;code&gt;textarea&lt;/code&gt; tag of HTML.&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%2Fi%2Fvqwdz9i7dkanzvhaets5.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%2Fi%2Fvqwdz9i7dkanzvhaets5.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Processing user input
&lt;/h1&gt;

&lt;p&gt;For processing which is done in our app.js, we reference &lt;code&gt;textarea&lt;/code&gt; in javascript using &lt;code&gt;document.querySelector()&lt;/code&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foupezjr4an7xl9hnrj9b.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%2Fi%2Foupezjr4an7xl9hnrj9b.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have reference to input and to extract the value from this we will use &lt;code&gt;textInp.value&lt;/code&gt;.But first, we will make a function to get our translation url which we will be using it to fetch translated text from the server using the &lt;code&gt;funtranslations api&lt;/code&gt; &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%2Fi%2Fhw92wvx3exaxoidsg9mt.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%2Fi%2Fhw92wvx3exaxoidsg9mt.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here serverUrl is the base url which is required for API. Now a fetch function which gets the translated text and displays output using &lt;code&gt;innerText&lt;/code&gt; on an output element also referenced using &lt;code&gt;document.querySelector()&lt;/code&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0xuskdwfbd53sclj8jh5.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%2Fi%2F0xuskdwfbd53sclj8jh5.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Translated output to the user
&lt;/h1&gt;

&lt;p&gt;We will create a div that has no text and will show output after processing when the button is clicked.&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%2Fi%2Fi9ng1fyq6gkd822jt0hx.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%2Fi%2Fi9ng1fyq6gkd822jt0hx.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Button referenced in the script and added an event listener &lt;code&gt;addEventListener()&lt;/code&gt; on it which waits for a click on the button and then calls our &lt;code&gt;clickHandler&lt;/code&gt; function.&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%2Fi%2Fvo31p9c5ii9u28zrblx9.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%2Fi%2Fvo31p9c5ii9u28zrblx9.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check the live demo of this app you can go to &lt;a href="https://gobanana.netlify.app/" rel="noopener noreferrer"&gt;Banana app&lt;/a&gt;&lt;br&gt;
Also I have created a repo on &lt;a href="https://github.com/amansethi00/banana-translator-app" rel="noopener noreferrer"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is my first blog. Suggestions are welcome from all of you. Thank You.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To know more about me, you can connect with me on &lt;a href="https://twitter.com/amansethi00" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>html</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
