<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[aamchora]]></title><description><![CDATA[I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.]]></description><link>https://blog.aamchora.space</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 13:48:07 GMT</lastBuildDate><atom:link href="https://blog.aamchora.space/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[What exactly Just in time (JIT) compilation is in JavaScript?]]></title><description><![CDATA[Is JavaScript compiled language or interpreted language or a mix of both?
Don't worry! Take a chill pill 😎
I'll break it down for you, bit by bit.
But first thing first...
Take a deep breath, and follow along.
JavaScript engines use the JIT(just in ...]]></description><link>https://blog.aamchora.space/what-exactly-just-in-time-jit-compilation-is-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/what-exactly-just-in-time-jit-compilation-is-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[compiler]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[interpreter]]></category><category><![CDATA[JIT compiler]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Mon, 25 Jul 2022 10:28:45 GMT</pubDate><content:encoded><![CDATA[<p>Is JavaScript compiled language or interpreted language or a mix of both?</p>
<p>Don't worry! Take a chill pill 😎</p>
<p>I'll break it down for you, bit by bit.</p>
<p>But first thing first...</p>
<p>Take a deep breath, and follow along.</p>
<p>JavaScript engines use the JIT(just in time) compiler to execute the code.</p>
<p>I know you must be wondering what the hell is JIT and how it works?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqzgpiikwnmvnfatw06c.gif" alt="Tired" /></p>
<p><strong>Let me demystify it for you</strong></p>
<p>But before understanding JIT, let's change the topic for a while, and try to understand what compiler and interpreter are and how they work?</p>
<p><strong>Compiler</strong></p>
<p>The source code is converted into machine code once and then gets executed. </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycmj8qvffi2g9mlm7qea.png" alt="Compiler" /></p>
<p> <strong>Pros</strong></p>
<ul>
<li>Suppose we have a loop that runs 40 times, we do not need to translate the same code again and again. The time it saves is huge.</li>
<li>The converted code will be more efficient cause we have more time for optimization.</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
<li>It takes a little bit more time to start up because it has to go through that compilation step at the beginning.</li>
</ul>
<p><strong>Interpreter</strong></p>
<p>Interpreters are quick. We don’t have to go through that whole compilation step before execution. It just starts translating the first line and then executes it.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3uijzblxnjgett9ugbc.png" alt="Interpreters" /></p>
<p> <strong>Pros</strong></p>
<ul>
<li>Fast start-up times are characteristic of interpreters. That's why browsers used JavaScript interpreters in the beginning.</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
<li>When you’re running the same code more than once. For example, if you’re in a loop. Then you have to do the same translation over and over and over again.</li>
<li>The code will be less efficient than the compiler cause we have less time for optimization.</li>
</ul>
<p><strong>Just In Time(JIT) compiler</strong></p>
<p>To get rid of the interpreter's inefficiency, "<em>the interpreter keeps retranslating the same code every time it goes through the loop</em>".</p>
<p>In the JIT compiler, we have a new component called a monitor (aka a profiler). That monitor watches the code as it runs and</p>
<ul>
<li>Identify the hot or warm components of the code eg: repetitive code.</li>
<li>Transform those components into machine code during run time.</li>
<li>Optimize the generated machine code.</li>
<li>Hot swap the previous implementation of the code.</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgraj0a2gpd55xgc9whm.png" alt="Just In Time(JIT) compiler" /></p>
<p>In short <strong>Just in time compiler</strong> is nothing but a combination of an interpreter and a compiler.</p>
<p>I hope you found it helpful, and if you have any questions, please let me know.</p>
<hr />
<p><em>Feel proud to correct me if I'm wrong:)</em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[Polyfills and Transpilers in JavaScript.]]></title><description><![CDATA[Have you ever been in a situation where your code runs in one browser and fails in another?
And you would be wondering what the hell is happening??

Even after checking your code thoroughly, over and over again, you find it 100% correct.
Then where t...]]></description><link>https://blog.aamchora.space/polyfills-and-transpilers-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/polyfills-and-transpilers-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[polyfills]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Babel]]></category><category><![CDATA[transpiler]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Wed, 13 Jul 2022 11:43:09 GMT</pubDate><content:encoded><![CDATA[<p>Have you ever been in a situation where your code runs in one browser and fails in another?</p>
<p>And you would be wondering what the hell is happening??</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qudbhkzdfuluv2dn85z6.gif" alt="What the hell is happening" /></p>
<p>Even after checking your code thoroughly, over and over again, you find it 100% correct.</p>
<p>Then where the hell actual problem lies in?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8a1gfv9xz8lqb6ckqbg.gif" alt="where is the problem" /></p>
<p><strong>Let's understand this weird behaviour of JavaScript.</strong></p>
<p>As we all know, JavaScript keeps on evolving over time. The browsers need to be updated with the time too. </p>
<p>If we don't update our browser,  <strong>Modern JavaScript</strong> code will fail on the older versions of the browsers.</p>
<p><em>So basically It's not your fault It's the browser's fault.</em></p>
<p><strong>Pro Tip:</strong> Don't forget to update your browser.</p>
<p>But wait...</p>
<p>We don't want to give users a bad user experience.</p>
<p>Is there any way we can fix this issue..?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p06mthgntww6fqed9yol.gif" alt="is there any way?" /></p>
<h3 id="heading-and-the-answer-is">And the answer is ...</h3>
<p>Yes! we're magicians.</p>
<p>We gotta learn new magic mantras for this.</p>
<p>And those are:</p>
<ul>
<li>Polyfill's Mantra</li>
<li>Transpiler's Mantra</li>
</ul>
<p><strong>Polyfill's Mantra</strong></p>
<p>A polyfill is a piece of code used to provide modern functionality on older browsers that do not natively support it.</p>
<p>EG: </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> str = <span class="hljs-string">'hello world'</span>;

<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">'hello'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">'bye'</span>)); <span class="hljs-comment">//false</span>
</code></pre>
<p>If you run the above code in updated versions of Chrome, firebox, safari, etc. It will work fine.</p>
<p>But if you run it in Internet Explorer 11 you will face an error as below.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjxqpp4odauim3m5scfx.png" alt="IE 11" /></p>
<p>To make our code run in older engines we need to write a polyfill for includes function.</p>
<pre><code class="lang-js">
<span class="hljs-comment">// Checking if includes method exist or not</span>
<span class="hljs-keyword">if</span> (!<span class="hljs-built_in">String</span>.prototype.includes) {
    <span class="hljs-comment">// polyfill method</span>
    <span class="hljs-built_in">String</span>.prototype.includes = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">searchString</span>) </span>{
    <span class="hljs-keyword">let</span> isFound = <span class="hljs-literal">false</span>;

    <span class="hljs-comment">// here &lt;this&gt; refers to the string it self</span>
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.indexOf(searchString) !== <span class="hljs-number">-1</span>) {
      isFound = <span class="hljs-literal">true</span>;
    }
    <span class="hljs-keyword">return</span> isFound;
  }
} 

<span class="hljs-comment">// Now it will work in IE 11 too.</span>

<span class="hljs-keyword">let</span> str = <span class="hljs-string">'hello world'</span>;

<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">'hello'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">'bye'</span>)); <span class="hljs-comment">//false</span>
</code></pre>
<p><strong>Transpiler's Mantra</strong></p>
<p>A transpiler is a piece of software that translates one source code to another source code. It can parse modern code and rewrite it using the older syntax of that language so that it will also work in outdated engines.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yrekiaqxh3m3j7k3kts.png" alt="Transpiler" /></p>
<p><a target="_blank" href="https://babeljs.io/">Babel</a> is one of the most used transpilers out there.</p>
<p>Usually, developer runs the transpiler on their own computer or in the cloud and then deploys the transpiled code to the server.</p>
<p><strong>Tip:</strong> Remember these two mantras, and your code will never fail.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jd3uok45wyc47weggigy.gif" alt="Powers" /></p>
<hr />
<p><em><strong>Do let me know if this blog was helpful</strong></em></p>
<p><em>follow me for more such blog posts.</em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[How to upload file using ExpressJS(NodeJS) - Multer]]></title><description><![CDATA[It took me 4-days and 100+ attempts to solve this simple issue of uploading a file using node. 
And the moment I got it right, I was like...phewww

Here is how I solved this issue.
By creating a - 

Simple node server.
Middleware to process files.
PO...]]></description><link>https://blog.aamchora.space/how-to-upload-file-using-expressjsnodejs-multer</link><guid isPermaLink="true">https://blog.aamchora.space/how-to-upload-file-using-expressjsnodejs-multer</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[Express]]></category><category><![CDATA[Express.js]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Mon, 11 Jul 2022 05:50:23 GMT</pubDate><content:encoded><![CDATA[<p>It took me 4-days and 100+ attempts to solve this simple issue of uploading a file using node. </p>
<p>And the moment I got it right, I was like...phewww</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szl49by3a0nfymfp6wct.gif" alt="phew gif" /></p>
<p>Here is how I solved this issue.</p>
<p>By creating a - </p>
<ul>
<li>Simple node server.</li>
<li>Middleware to process files.</li>
<li>POST route to accept files.</li>
<li>Route to serve files.</li>
<li>Middleware to Handle CORS issue(you won't get this issue in postman).</li>
</ul>
<p><strong>Node server using express</strong></p>
<ul>
<li>Initialize the project using below command</li>
</ul>
<blockquote>
<p>node init</p>
</blockquote>
<ul>
<li>Install the required dependencies.</li>
</ul>
<blockquote>
<p>using npm -&gt; <code>npm i express dotenv</code></p>
<p>using yarn -&gt; <code>yarn add express dotenv</code> </p>
</blockquote>
<ul>
<li>Create server.js and paste below code </li>
</ul>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> dotenv = <span class="hljs-built_in">require</span>(<span class="hljs-string">"dotenv"</span>);

<span class="hljs-keyword">const</span> app = express();

dotenv.config(); <span class="hljs-comment">// &lt;-- To use.env file</span>

app.use(express.json());
<span class="hljs-comment">/*
express.json() middleware helps us to parse all HTTP requests

like --&gt; req.body

And express.json() middleware always should be at the top

*/</span>

<span class="hljs-keyword">const</span> PORT = process.env.PORT || <span class="hljs-number">5000</span>;
app.listen(
  PORT,
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Server is up and running on port <span class="hljs-subst">${PORT}</span>`</span>)
);
</code></pre>
<p>Now run the node server using below command</p>
<blockquote>
<p>node server.js</p>
</blockquote>
<p><strong>Create a middleware to process files.</strong></p>
<p>Install the dependencies</p>
<blockquote>
<p>using npm -&gt; <code>npm i multer</code></p>
<p>using yarn -&gt; <code>yarn add multer</code> </p>
</blockquote>
<p>Create <code>processFile.js</code> file and paste below code in it and create <code>upload</code> folder to store uploaded files in it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> multer = <span class="hljs-built_in">require</span>(<span class="hljs-string">"multer"</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);

<span class="hljs-keyword">const</span> MIME_TYPES = {
  <span class="hljs-string">"image/jpg"</span>: <span class="hljs-string">"jpg"</span>,
  <span class="hljs-string">"image/jpeg"</span>: <span class="hljs-string">"jpg"</span>,
  <span class="hljs-string">"image/png"</span>: <span class="hljs-string">"png"</span>,
  <span class="hljs-comment">// you can add more here</span>
};

<span class="hljs-keyword">const</span> storage = multer.diskStorage({
  <span class="hljs-attr">destination</span>: <span class="hljs-function">(<span class="hljs-params">req, file, callback</span>) =&gt;</span> {
    callback(<span class="hljs-literal">null</span>,path.join(__dirname, <span class="hljs-string">'./upload'</span>));
   <span class="hljs-comment">// Note ./upload path should be relative. Change this path according to your folder structure</span>
  },
  <span class="hljs-attr">filename</span>: <span class="hljs-function">(<span class="hljs-params">req, file, callback</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> name = file.originalname.split(<span class="hljs-string">" "</span>).join(<span class="hljs-string">"_"</span>);
    <span class="hljs-keyword">const</span> extension = MIME_TYPES[file.mimetype];
    callback(<span class="hljs-literal">null</span>, name + <span class="hljs-built_in">Date</span>.now() + <span class="hljs-string">"."</span> + extension);
  },
});

<span class="hljs-keyword">const</span> processFile = multer({ <span class="hljs-attr">storage</span>: storage }).single(<span class="hljs-string">"file"</span>); <span class="hljs-comment">//&lt;--here `file` key</span>
<span class="hljs-comment">/*
Look at the last example of how we will use `file` key to 
upload files using form data and postman.

For accepting multiple files change above line to 

const processFile = multer({ storage: storage }).array('file', 12);
Where 12 is the number of files you want to accept.

*/</span>

<span class="hljs-built_in">module</span>.exports = processFile;
</code></pre>
<p><strong>Create a POST route to accept file.</strong></p>
<p>Paste below code in server.js file </p>
<p><em>For uploading single file</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> processFile = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./processFile"</span>);

app.post(<span class="hljs-string">"/api/upload"</span>, processFile, <span class="hljs-function">(<span class="hljs-params">req, res, next</span>) =&gt;</span> {

  <span class="hljs-keyword">const</span> url = req.protocol + <span class="hljs-string">"://"</span> + req.get(<span class="hljs-string">"host"</span>);

  <span class="hljs-keyword">const</span> file = url + <span class="hljs-string">"/files/"</span> + req.file.filename;

  res.status(<span class="hljs-number">201</span>).json({
    <span class="hljs-attr">message</span>: <span class="hljs-string">"Success"</span>,
    <span class="hljs-attr">file</span>: file,
  });
});
</code></pre>
<p><em>For uploading multiple files</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> processFile = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./processFile"</span>);

app.post(<span class="hljs-string">"/api/upload"</span>, processFile, <span class="hljs-function">(<span class="hljs-params">req, res, next</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> files = []
  <span class="hljs-keyword">const</span> url = req.protocol + <span class="hljs-string">"://"</span> + req.get(<span class="hljs-string">"host"</span>);
  req.files.forEach(<span class="hljs-function"><span class="hljs-params">file</span>=&gt;</span>{
    <span class="hljs-keyword">const</span> file_url = url + <span class="hljs-string">"/files/"</span> + file.filename;
    files.push({
      <span class="hljs-attr">url</span>: file_url,
    });
  })

  res.status(<span class="hljs-number">201</span>).json({
    <span class="hljs-attr">message</span>: <span class="hljs-string">"files saved successfully!"</span>,
    files,
  });
});
</code></pre>
<p>So far you would be able to store files in your <code>upload</code> folder but you can't access files using links you are receiving in the response and for that, we need to serve those files.</p>
<p><strong>Serve uploaded files</strong></p>
<p>Add below code in <code>server.js</code> file and restart the server.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);

app.use(<span class="hljs-string">"/files"</span>, express.static(path.join(__dirname, <span class="hljs-string">"upload"</span>)));
</code></pre>
<p>Now you should be able to see the files if you visit the links. </p>
<p><strong>CORS (Cross-Origin Resource Sharing)</strong></p>
<p>Note: You won't face this issue in the postman but in your client-side app you will face this issue.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l39k1ppbiv49xuey10di.png" alt="cors error" /></p>
<p>To resolve the CORS issue</p>
<ul>
<li>Create a cors middleware.</li>
<li>Apply cors middleware at the top of your file</li>
</ul>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> cors = <span class="hljs-function">(<span class="hljs-params">req, res, next</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> origin = <span class="hljs-string">"*"</span>; <span class="hljs-comment">// &lt;-- change this in production</span>
  res.setHeader(<span class="hljs-string">"Access-Control-Allow-Origin"</span>, origin);
  res.setHeader(
    <span class="hljs-string">"Access-Control-Allow-Headers"</span>,
    <span class="hljs-string">"Origin, X-Requested-With, Content, Accept, Content-Type, Authorization"</span>
  );
  res.setHeader(
    <span class="hljs-string">"Access-Control-Allow-Methods"</span>,
    <span class="hljs-string">"GET, POST, PUT, DELETE, PATCH, OPTIONS"</span>
  );
  next();
};

app.use(cors); <span class="hljs-comment">// &lt;-- Should be at the top</span>
</code></pre>
<p><strong>Upload file from client</strong></p>
<pre><code class="lang-js">
  <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> FormData();
  data.append(<span class="hljs-string">"file"</span>, FILE_URI);

  fetch(<span class="hljs-string">"http://localhost:5000/api/upload"</span>, {
    <span class="hljs-attr">method</span>: <span class="hljs-string">"post"</span>,
    <span class="hljs-attr">body</span>: data,
  })
  .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json())
  .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
     <span class="hljs-built_in">console</span>.log(err);
  });
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm52lp4d26otr16k06dy.png" alt="upload file using postman" /></p>
<p>And That's how I solved this issue. Let me know if you have any doubt.</p>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[Functions are Objects in JavaScript]]></title><description><![CDATA[Whattttt???

Yes, You read that correctly.
Let's deep dive into this topic.
Before we get into a serious drill, Let's quickly revise objects and functions in JavaScript.
Object: 
Objects in JavaScript are nothing but a non-primitive type data structu...]]></description><link>https://blog.aamchora.space/functions-are-objects-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/functions-are-objects-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[javascript framework]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Tue, 05 Jul 2022 14:06:27 GMT</pubDate><content:encoded><![CDATA[<p><strong>Whattttt???</strong></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q610q7szfdlkbgvrqqen.gif" alt="Confused man" /></p>
<p><strong>Yes, You read that correctly.</strong></p>
<p>Let's deep dive into this topic.</p>
<p>Before we get into a serious drill, Let's quickly revise objects and functions in JavaScript.</p>
<p><strong>Object:</strong> </p>
<p>Objects in JavaScript are nothing but a non-primitive type data structure. We can define an object using curly brackets and put key-value pairs separated by a comma in it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
   <span class="hljs-attr">name</span>:<span class="hljs-string">"Hemendra"</span>,
   <span class="hljs-attr">nickname</span>:<span class="hljs-string">"Hemu"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
}

user.city = <span class="hljs-string">"Bhilwara"</span> <span class="hljs-comment">// add new value in existing object</span>

user.city = <span class="hljs-string">"Jaipur"</span> <span class="hljs-comment">// update new value in existing object</span>

<span class="hljs-built_in">console</span>.log(user.name);
<span class="hljs-comment">/*
output

Hemendra

*/</span>
<span class="hljs-built_in">console</span>.log(user);

<span class="hljs-comment">/*
output

{
   name:"Hemendra",
   nickname:"Hemu",
   email:"hi@aamchora.space",
   city:"Jaipur"
}
*/</span>
</code></pre>
<p>Read more about <a target="_blank" href="https://aamchora.hashnode.dev/javascript-objects-in-depth">objects in depth.</a></p>
<p><strong>Function</strong></p>
<p>Unlike other programming languages, JavaScript has implemented the functions very differently.</p>
<p>Let's have a look</p>
<p><strong>Definition:</strong> </p>
<p>A function is a block of organized code that performs a specific task. </p>
<p><strong>Advantages of functions</strong></p>
<ul>
<li>Functions make your program easier to read, understand, and debug.</li>
<li>Functions can make a program smaller by eliminating repetitive code.</li>
</ul>
<p>Now you would be wondering, What's the <strong>difference</strong> then? </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnlhgt64dggwj4ab580p.gif" alt="What is different" /></p>
<p>In JavaScript, functions are <strong>first-class objects</strong>, because they can have properties and methods just like any other object. Every JavaScript function is actually a <strong>Function object</strong>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb7zqh9hx4xv376yeqn0.gif" alt="Now way gif" /></p>
<p>Let's see how we define functions in JavaScript first</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">a,b</span>)</span>{
 <span class="hljs-keyword">return</span> a+b;
}

<span class="hljs-keyword">const</span> res = sum(<span class="hljs-number">2</span>,<span class="hljs-number">4</span>);

<span class="hljs-built_in">console</span>.log(res); <span class="hljs-comment">// outputs -&gt; 6</span>
</code></pre>
<p>Above we have created a function named sum which adds two numbers.</p>
<p>But as I said earlier, is the sum function created above also an object? can we add properties and values to the sum function as we did in the user object above?</p>
<p>let's see this in action</p>
<pre><code class="lang-js">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">a,b</span>)</span>{
 <span class="hljs-keyword">return</span> a+b;
}

sum.help = <span class="hljs-string">"use this function to add two numbers"</span>;
<span class="hljs-comment">// like an object we have added help key and it's value to the above function.</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// will work as before</span>
<span class="hljs-keyword">const</span> res = sum(<span class="hljs-number">3</span>,<span class="hljs-number">6</span>);
<span class="hljs-built_in">console</span>.log(res) <span class="hljs-comment">// outputs --&gt; 9</span>

<span class="hljs-comment">// but now </span>

<span class="hljs-built_in">console</span>.log(sum.help) 
<span class="hljs-comment">// outputs --&gt; use this function to add two numbers</span>
</code></pre>
<p>Let's verify if the sum function is an instance of an object or not</p>
<pre><code class="lang-js">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">a,b</span>)</span>{
 <span class="hljs-keyword">return</span> a+b;
}

<span class="hljs-built_in">console</span>.log(sum <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Function</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(sum <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Object</span>); <span class="hljs-comment">// true</span>

<span class="hljs-keyword">let</span> name = <span class="hljs-string">"Hemendra"</span>
<span class="hljs-built_in">console</span>.log(name <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Object</span>); <span class="hljs-comment">// false</span>
</code></pre>
<p>And Voila, It proves that the functions are objects in JavaScript.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vasb78g9cnezilh95nxg.gif" alt="I was right gif" /></p>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
]]></content:encoded></item><item><title><![CDATA[What is throttling in JavaScript?]]></title><description><![CDATA[Again a jargonish word, but let me demystify it for you.

It's nothing but a simple technique we use to prevent unnecessary function calls to improve app's performance.
Unlike Debouncing, here we block a function call for a particular time if it's be...]]></description><link>https://blog.aamchora.space/what-is-throttling-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/what-is-throttling-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[performance]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 01 Jul 2022 11:15:24 GMT</pubDate><content:encoded><![CDATA[<p>Again a jargonish word, but let me demystify it for you.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rz7pwdqvr8e25wa9sig9.gif" alt="let me help you" /></p>
<p>It's nothing but a simple technique we use to prevent unnecessary function calls to improve app's performance.</p>
<p>Unlike <a target="_blank" href="https://aamchora.hashnode.dev/what-is-debouncing-in-javascript">Debouncing</a>, here we block a function call for a particular time if it's been executed recently. Or we could also say that throttling ensures a function call regularly at a fixed rate.</p>
<p>Let's look at the below example,</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Throttling<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn"</span>&gt;</span>Click here<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">submitData</span>(<span class="hljs-params">query</span>)</span>{
   <span class="hljs-comment">// some api call</span>
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"result"</span>,count++)
}

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">".btn"</span>).addEventListener(<span class="hljs-string">"click"</span>,<span class="hljs-function">()=&gt;</span>{
submitData();
})
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/noojew5jtc2l6uxtze68.png" alt="first example" /></p>
<p>In the above example, if you click 10 times in 2000ms, Function will be called 10 times, as you can clearly see in the above example. </p>
<p>It's a very expensive operation, and as dev, our job is to make operations as cheap as possible. </p>
<p>Let's see how throttling helps us to make these operations cheaper.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">throttle</span>(<span class="hljs-params">fn,delay</span>)</span>{
  <span class="hljs-keyword">let</span> last = <span class="hljs-number">0</span>;
 <span class="hljs-comment">/*  
here ...args is optional I've used this in case, if you 
want to pass some parameters you can use ...args
*/</span> 
  <span class="hljs-keyword">return</span> <span class="hljs-function">(<span class="hljs-params">...args</span>)=&gt;</span>{
    <span class="hljs-keyword">const</span> now = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime();
    <span class="hljs-keyword">if</span>(now - last &lt; delay){
      <span class="hljs-keyword">return</span>;
    }
   last = now;
   <span class="hljs-keyword">return</span> fn(...args)
  }

}

<span class="hljs-keyword">const</span> magicFunction = throttle(submitData,<span class="hljs-number">1000</span>);

<span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">submitData</span>(<span class="hljs-params"></span>)</span>{
   <span class="hljs-comment">// some api call</span>
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"result"</span>,count++)
}

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">".btn"</span>).addEventListener(<span class="hljs-string">"click"</span>,magicFunction);
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owa67zg0804zmsp8ko7e.png" alt="second" /></p>
<p>Now, if you click 10 times in 2000ms, Function will be called 2 times only, as you can see in the code. We have blocked the function call for 1000ms.</p>
<p><em>That's how we can implement the <strong>throttling technique</strong> to <strong>improve our app's performance</strong></em></p>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[What is debouncing in JavaScript?]]></title><description><![CDATA[While building an app using JavaScript. We may need to call the function many times to achieve the desired result.
Some examples are 

While scrolling or resizing the screen you would want to compute something.
Sometimes we may need to display quick ...]]></description><link>https://blog.aamchora.space/what-is-debouncing-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/what-is-debouncing-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Tue, 28 Jun 2022 07:44:12 GMT</pubDate><content:encoded><![CDATA[<p>While building an app using JavaScript. We may need to call the function many times to achieve the desired result.</p>
<p>Some examples are </p>
<ul>
<li>While scrolling or resizing the screen you would want to compute something.</li>
<li>Sometimes we may need to display quick results as user types in the input field.</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1mtgpuz1ztkqemb75lp.png" alt="google search" /></p>
<p>Let's see, how we usually write code to achieve the desired result.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Debouncing<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"search"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"search"</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<pre><code class="lang-js">
<span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">search</span>(<span class="hljs-params">query</span>)</span>{
   <span class="hljs-comment">// some api call</span>
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"result"</span>,count++)
}

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">".search"</span>).addEventListener(<span class="hljs-string">"input"</span>,<span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>{
search(e.target.value);
})
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orzvwjsqotqxzb01t664.png" alt="example one" /></p>
<p>In the above example, If I search for a laptop, The function search will be called 6 times. Which is <strong>an inefficient way</strong> of calling a function. And it is <strong>very expensive</strong> for JavaScript. </p>
<p>You must be wondering what do you mean by <strong>expensive for JavaScript</strong>?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdu6hyncmawfej6l6gb4.gif" alt="wondering man" /></p>
<p><a target="_blank" href="https://aamchora.hashnode.dev/what-exactly-javascript-is">JavaScript is a synchronous single-threaded language.</a> Which means we have a single <strong>callstack</strong> to execute everything. And because it is single-threaded, we should not block the thread by calling functions unnecessary.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pifqybnp073w5z95w3e.gif" alt="understanding boy" /></p>
<p><strong>But wait ...</strong></p>
<p>How do we prevent unnecessary function calls to improve performance?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7arukhkqppyn1z255tvo.gif" alt="questioning man" /></p>
<p>Here, the concept of <strong>debouncing</strong> kicks into the picture.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zjp0k3rfstwb1q0iqry.gif" alt="Kicks in" /></p>
<p><strong>Debouncing</strong> is a technique used to improve the browser's performance. It ensures that time-consuming tasks do not fire so often, In other words, it limits the rate at which a function gets invoked.</p>
<p>Let's re-write the above code using <strong>Debouncing technique</strong>.</p>
<pre><code class="lang-js">
<span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">search</span>(<span class="hljs-params">query</span>)</span>{
   <span class="hljs-comment">// some api call</span>
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"result"</span>,count++)

}

<span class="hljs-keyword">const</span> magicFunction = debounce(search,<span class="hljs-number">300</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">debounce</span>(<span class="hljs-params">func, delay</span>)</span>{
  <span class="hljs-keyword">let</span> timer;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>) </span>{
    <span class="hljs-built_in">clearTimeout</span>(timer);
    timer = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      func(...args);
    }, delay);
  };
}

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">".search"</span>).addEventListener(<span class="hljs-string">"input"</span>,<span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>{
  magicFunction(e.target.value);
});
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aigz2ova79mvebklrbnp.png" alt="example two" /></p>
<p>The idea behind debouncing technique is that we <strong>do not call function during the event</strong> but after the event.</p>
<p>In the above example, We can clearly see.</p>
<p><strong>debounce</strong> is a <strong>higher order function(HOF)</strong>. Which takes two parameters the first parameter will be a function and the second will be a delay.</p>
<p>If the delay between two keystrokes is less than 300ms, function will not execute and <code>setTimeout</code> will be destroyed. And if the delay is more than 300ms, then only function will execute.</p>
<p><em>That's how we can implement the <strong>debouncing technique</strong> to <strong>improve our app's performance</strong></em></p>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[What exactly JavaScript is?]]></title><description><![CDATA[Definition 
JavaScript is a synchronous single-threaded scripting language that was initially meant to run on the client-side(web browsers). But nowadays, we can run JavaScript on the server-side as well.
But wait, What does it mean by synchronous an...]]></description><link>https://blog.aamchora.space/what-exactly-javascript-is</link><guid isPermaLink="true">https://blog.aamchora.space/what-exactly-javascript-is</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript framework]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Sun, 26 Jun 2022 19:28:09 GMT</pubDate><content:encoded><![CDATA[<p><strong>Definition</strong> </p>
<p>JavaScript is a synchronous <strong>single-threaded</strong> scripting language that was initially meant to run on the client-side(web browsers). But nowadays, we can run JavaScript on the server-side as well.</p>
<p>But wait, What does it mean by synchronous and single-threaded?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpu3mqpzlg1m00wlwzag.gif" alt="Thinking face" /></p>
<h3 id="heading-lets-deep-dive-into-it">Let's deep dive into it</h3>
<p><strong>Synchronous</strong></p>
<p>In English, it simply means that something is happening at the same time. And in terms of programming language, it simply means the code will execute in order line by line. </p>
<p>EG:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"one"</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"two"</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"three"</span>);

<span class="hljs-comment">/*
The above code will execute in below order

one
two
three

*/</span>
</code></pre>
<p>But, sometimes you might encounter some code that will run asynchronously in JavaScript.</p>
<p>EG:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"one"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">magicFunction</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"It's magic"</span>);
}

<span class="hljs-built_in">setTimeout</span>(magicFunction,<span class="hljs-number">5000</span>);

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"two"</span>);

<span class="hljs-comment">/*
The above code will output the below result

one
two
undefined
It's magic --&gt; will display this after 5000ms.

*/</span>
</code></pre>
<p>Now you would be wondering what the hell is happening, you just have read that JavaScript is a synchronous language??</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcpwzcfjufedozt18f7u.gif" alt="Super confusing" /></p>
<p>Let's understand this more deeply why and how.</p>
<p>In the above example, <code>setTimeout</code> will execute asynchronously. </p>
<p>But what if I tell you that -  <strong>setTimeout</strong> is not a part of the JavaScript language itself instead it's a part of the browser's <strong>web API</strong>.</p>
<p>Some examples of browser APIs are fetch, setInterval, DOM methods etc.</p>
<p>To make JavaScript more powerful on the client-side, browser JS-Engine attaches <strong>browser APIs</strong> with JavaScript and it will work like normal JavaScript.</p>
<p>So it means JavaScript is a <strong>synchronous language only</strong>. There are some other wrappers around it that make JavaScript run asynchronously.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ywx6ba59lsnudgnddp2.gif" alt="Interesting" /></p>
<p>Okay, but what does it mean by single-threaded? </p>
<p><strong>Single-thread</strong></p>
<p>It simply means we have a single-thread or a callstack to execute a JavaScript code. And we should not block this thread or else your app will crash and you won't be able to perform another task while one task is executing.</p>
<p>In the above example, we saw that <code>setTimeout</code> was running asynchronously.</p>
<p>But here it won't block the main thread. It will <strong>execute outside the callstack</strong>.</p>
<p>After timeout, <strong>callback function inside the seTimeout</strong> will wait in <strong>callback queue</strong> till the <strong>callstack gets empty</strong>. </p>
<p>An Event loop constantly keeps checking whether the <strong>callstack</strong> is empty or not. As soon as it gets empty event loop put <strong>callback function</strong> from <strong>callback queue</strong> to <strong>callstack</strong> and it gets executed.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4iiep7voftytoai1qle9.png" alt="JavaScript single thread" /></p>
<p>And there you go ... 🤗🤗🤗</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w77t4tmitpd279ee9jwb.gif" alt="Victory" /></p>
<p>Now you know what exactly JavaScript is.</p>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
<p><a target="_blank" href="https://twitter.com/AamChora">Twitter</a> || <a target="_blank" href="https://www.linkedin.com/in/hemendrakhatik/">Linkedin</a></p>
]]></content:encoded></item><item><title><![CDATA[Take Screenshot in react-native.]]></title><description><![CDATA[Taking screenshots programmatically in react-native is very easy. Without further a do, let's start writing a code.
Dependencies

react-native-view-shot for taking screenshots.
@react-native-community/cameraroll for saving the screenshot.

Install th...]]></description><link>https://blog.aamchora.space/take-screenshot-in-react-native</link><guid isPermaLink="true">https://blog.aamchora.space/take-screenshot-in-react-native</guid><category><![CDATA[React Native]]></category><category><![CDATA[React]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 24 Jun 2022 16:14:36 GMT</pubDate><content:encoded><![CDATA[<p>Taking screenshots programmatically in react-native is very easy. Without further a do, let's start writing a code.</p>
<p><strong>Dependencies</strong></p>
<ol>
<li><code>react-native-view-shot</code> for taking screenshots.</li>
<li><code>@react-native-community/cameraroll</code> for saving the screenshot.</li>
</ol>
<p><strong>Install the dependency</strong></p>
<p>For npm -&gt; <code>npm install react-native-view-shot @react-native-community/cameraroll</code></p>
<p>For yarn -&gt; <code>yarn add react-native-view-shot @react-native-community/cameraroll</code></p>
<p>After installing the dependencies</p>
<ul>
<li>Import the ViewShot and CameraRoll</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> ViewShot <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native-view-shot'</span>;
<span class="hljs-keyword">import</span> CameraRoll <span class="hljs-keyword">from</span> <span class="hljs-string">'@react-native-community/cameraroll'</span>;
</code></pre>
<ul>
<li><p>After importing <strong>ViewShot</strong> wrap the area we want to take screenshot using <strong>ViewShot</strong>.</p>
</li>
<li><p>Now we will use <strong>useRef hook</strong> to create ref.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, {useRef} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>; <span class="hljs-comment">// should be outside the component</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ref = useRef(); <span class="hljs-comment">// should be inside the component</span>
</code></pre>
</li>
<li><p>Pass the required props in ViewShot as mentioned below.</p>
</li>
</ul>
<pre><code class="lang-js"> &lt;ViewShot
    ref={ref}
    options={{
    <span class="hljs-attr">fileName</span>: <span class="hljs-string">'file-name'</span>, <span class="hljs-comment">// screenshot image name</span>
    <span class="hljs-attr">format</span>: <span class="hljs-string">'jpg'</span>, <span class="hljs-comment">// image extension</span>
    <span class="hljs-attr">quality</span>: <span class="hljs-number">0.9</span> <span class="hljs-comment">// image quality</span>
   }} &gt;

....some amazing content ....
&lt;ViewShot/&gt;
</code></pre>
<ul>
<li>Create a function to take screenshots and paste the below code.</li>
</ul>
<pre><code class="lang-js"> <span class="hljs-keyword">const</span> takeScreenShot = <span class="hljs-function">() =&gt;</span> {
   <span class="hljs-comment">// to take a screenshot</span>
    ref.current.capture().then(<span class="hljs-function"><span class="hljs-params">uri</span> =&gt;</span> {
      <span class="hljs-comment">// to save screenshot in local memory</span>
      CameraRoll.save(uri,{<span class="hljs-attr">type</span>:<span class="hljs-string">"photo"</span>,<span class="hljs-attr">album</span>:<span class="hljs-string">"Album codes"</span>});
      alert(<span class="hljs-string">"Took screenshot"</span>);
    });
  };
</code></pre>
<ul>
<li>Call the above function to take the screenshot.</li>
</ul>
<p><strong>And Voila, We're done easy peasy :)</strong>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emdmqg0dw0aj1a7e6gs4.gif" alt="easy peasy" /></p>
<p><strong>Complete code</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, {useRef} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> {StyleSheet, Text, View, Button} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native'</span>;
<span class="hljs-keyword">import</span> ViewShot <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native-view-shot'</span>;
<span class="hljs-keyword">import</span> CameraRoll <span class="hljs-keyword">from</span> <span class="hljs-string">'@react-native-community/cameraroll'</span>;

<span class="hljs-keyword">const</span> SomeComponent =<span class="hljs-function">() =&gt;</span> {

  <span class="hljs-keyword">const</span> ref = useRef();
  <span class="hljs-keyword">const</span> takeScreenShot = <span class="hljs-function">() =&gt;</span> {
    ref.current.capture().then(<span class="hljs-function"><span class="hljs-params">uri</span> =&gt;</span> {
      CameraRoll.save(uri,{<span class="hljs-attr">type</span>:<span class="hljs-string">"photo"</span>,<span class="hljs-attr">album</span>:<span class="hljs-string">"QR codes"</span>});
      alert(<span class="hljs-string">"Took screenshot"</span>);
    });
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.container}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ViewShot</span>
        <span class="hljs-attr">ref</span>=<span class="hljs-string">{ref}</span>
        <span class="hljs-attr">options</span>=<span class="hljs-string">{{</span>
        <span class="hljs-attr">fileName:</span> '<span class="hljs-attr">file-name</span>', // <span class="hljs-attr">screenshot</span> <span class="hljs-attr">image</span> <span class="hljs-attr">name</span>
        <span class="hljs-attr">format:</span> '<span class="hljs-attr">jpg</span>', // <span class="hljs-attr">image</span> <span class="hljs-attr">extention</span>
        <span class="hljs-attr">quality:</span> <span class="hljs-attr">0.9</span> // <span class="hljs-attr">image</span> <span class="hljs-attr">quality</span>
        }} &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span> Some awesome content<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ViewShot</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Share QR Code"</span> <span class="hljs-attr">onPress</span>=<span class="hljs-string">{takeScreenShot}/</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  );
};


<span class="hljs-keyword">const</span> styles = StyleSheet.create({
  <span class="hljs-attr">container</span>: {
    <span class="hljs-attr">flex</span>:<span class="hljs-number">1</span>,
    <span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
    <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'center'</span>,
    <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'#171821'</span>,
  }

});
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Custom fonts in react-native.]]></title><description><![CDATA[Adding fonts in react-native app can be frustrating. If you are doing it for the first time. But trust me, it is as simple as it sounds. Follow the below steps carefully and it will work as expected.

Download the fonts
Create fonts folder inside the...]]></description><link>https://blog.aamchora.space/custom-fonts-in-react-native</link><guid isPermaLink="true">https://blog.aamchora.space/custom-fonts-in-react-native</guid><category><![CDATA[React Native]]></category><category><![CDATA[Android]]></category><category><![CDATA[iOS]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 24 Jun 2022 16:11:35 GMT</pubDate><content:encoded><![CDATA[<p><strong>Adding fonts in react-native app can be frustrating. If you are doing it for the first time. But trust me, it is as simple as it sounds. Follow the below steps carefully and it will work as expected.</strong></p>
<ol>
<li>Download the fonts</li>
<li>Create fonts folder inside the assets folder <code>src/assets/fonts</code>.</li>
<li>Put all the font files inside the fonts folder.</li>
</ol>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qfem38qwavbsarzp78la.png" alt="react-native fonts" /></p>
<p>4.Now create <code>react-native.config.js</code> file in your app's root folder and paste below code in it.</p>
<pre><code>module.exports <span class="hljs-operator">=</span> {
  project: {
    ios: {},
    android: {},
  },
  assets: [<span class="hljs-string">'./src/assets/fonts'</span>], <span class="hljs-comment">// &lt;---- fonts folder path</span>
};
</code></pre><p>Run the below command </p>
<p><code>npx react-native link</code></p>
<p>And voila, it's done now you can <strong>use the file name as your fontFamily name(don't put extension name after file name)</strong> and it will work like a charm.</p>
]]></content:encoded></item><item><title><![CDATA[How arrays and objects are copied in JavaScript?]]></title><description><![CDATA[First, we need to thoroughly understand two concepts.

Pass by value.
Pass by reference.

Pass by value
It simply means, While calling a function. You are passing values directly inside a function as an argument.
EG:
// function declaration
function ...]]></description><link>https://blog.aamchora.space/how-arrays-and-objects-are-copied-in-javascript</link><guid isPermaLink="true">https://blog.aamchora.space/how-arrays-and-objects-are-copied-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><category><![CDATA[React Native]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[data structures]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 24 Jun 2022 16:09:01 GMT</pubDate><content:encoded><![CDATA[<p>First, we need to thoroughly understand two concepts.</p>
<ol>
<li>Pass by value.</li>
<li>Pass by reference.</li>
</ol>
<p><strong>Pass by value</strong></p>
<p>It simply means, While calling a function. You are passing values directly inside a function as an argument.</p>
<p>EG:</p>
<pre><code><span class="hljs-comment">// function declaration</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`hello <span class="hljs-subst">${name}</span>!`</span>);
}

<span class="hljs-comment">// function calling</span>
greet(<span class="hljs-string">"Hemendra"</span>);<span class="hljs-comment">// &lt;- we're passing value as an argument</span>

<span class="hljs-comment">// prints --&gt; hello Hemendra!</span>
</code></pre><p><strong>Pass by reference</strong></p>
<p>It simply means, While calling a function, You will pass a reference of a constant or a variable instead of a value directly inside a function as an argument.</p>
<p>EG:</p>
<pre><code><span class="hljs-comment">// function declaration</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`hello <span class="hljs-subst">${name}</span>!`</span>);
}

<span class="hljs-keyword">const</span> name = <span class="hljs-string">"Hemendra"</span>;

<span class="hljs-comment">// function calling</span>
greet(name); <span class="hljs-comment">// &lt;-- we're passing a reference of a constant called name  </span>

<span class="hljs-comment">// prints --&gt; hello Hemendra!</span>
</code></pre><p>Pretty simple right? </p>
<p>Yes, it's that simple!</p>
<p>But, there is more to understand here. Let's understand this by an example.</p>
<p>EG:1</p>
<pre><code>// You might have written something <span class="hljs-keyword">like</span> this <span class="hljs-keyword">before</span>

let score = <span class="hljs-number">10</span>;
let <span class="hljs-keyword">temp</span> = score;
<span class="hljs-keyword">temp</span> = <span class="hljs-keyword">temp</span> + <span class="hljs-number">2</span>;

console.log(`score <span class="hljs-keyword">is</span> ${score}`); // prints-&gt; score <span class="hljs-keyword">is</span> <span class="hljs-number">10</span>
console.log(`<span class="hljs-keyword">temp</span> <span class="hljs-keyword">is</span> ${<span class="hljs-keyword">temp</span>}`); // prints-&gt; <span class="hljs-keyword">temp</span> <span class="hljs-keyword">is</span> <span class="hljs-number">12</span>
</code></pre><p>EG: 2</p>
<pre><code>// You might have <span class="hljs-keyword">done</span> something like this as well

<span class="hljs-built_in">let</span> todos = [<span class="hljs-string">"T1"</span>, <span class="hljs-string">"T2];
let tempTodos = todos;
tempTodos.push("</span>T3<span class="hljs-string">");

console.log(`old todos are <span class="hljs-variable">${todos}</span>`); 
// prints-&gt; old todos are T1, T2, T3
console.log(`temp todos are <span class="hljs-variable">${tempTodos}</span>`); 
// prints-&gt; temp todos are T1, T2, T3</span>
</code></pre><p>Wait whatttttt?? </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlkq4xxwcwjf9c3k370y.gif" alt="confusion" /></p>
<p>Yes, I was also like this when I was a beginner. Let's understand what just happened in the above example.</p>
<p><strong>Remember this</strong></p>
<ul>
<li>While working with primitive data type in JavaScript we copy its value.</li>
</ul>
<p><code>EG: string, number, boolean, symbol, undefined, null</code>.</p>
<ul>
<li>While working with non-primitive data type in JavaScript we copy its reference.</li>
</ul>
<p><code>EG: arrays, objects, functions</code></p>
<p><strong>Explanation</strong></p>
<p>In the first example, we used the primitive data type which is why the score and temp variable's value was different.</p>
<p>But, In the second example, we used an array that was a non-primitive data type, and while copying the variable, <strong>we have reference of it instead of its value</strong>. Hence both the variables <code>todos</code> and <code>tempTodos</code> <strong>were pointing to the same value in the memory.</strong></p>
<p>Thus, while changing the tempTodos variable todos variable was also changing.</p>
<p><strong>Tip:</strong> </p>
<p>We often encounter an array of objects while building any application.
We create multiple functions to modify the array of objects.</p>
<p>But do remember this concept,</p>
<p>How array or objects are copied in JavaScript. Else you will end up facing a lot of bugs in your app.</p>
<hr />
<p><strong><em>follow me for more such blog posts.</em></strong></p>
<p><em>Let me know if this blog was helpful.</em> </p>
]]></content:encoded></item><item><title><![CDATA[JavaScript objects in depth.]]></title><description><![CDATA[Being a JavaScript developer, you might be aware of the fact that we can't build any large-scale app without dealing with Objects.
In JavaScript, Everything is an Object.
Lets, Deep dive into JavaScript Objects.
Object: Objects in JavaScript are noth...]]></description><link>https://blog.aamchora.space/javascript-objects-in-depth</link><guid isPermaLink="true">https://blog.aamchora.space/javascript-objects-in-depth</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 24 Jun 2022 16:03:52 GMT</pubDate><content:encoded><![CDATA[<p>Being a JavaScript developer, you might be aware of the fact that we can't build any large-scale app without dealing with Objects.</p>
<p><strong>In JavaScript, Everything is an Object.</strong></p>
<p>Lets, Deep dive into JavaScript Objects.</p>
<p><strong>Object:</strong> Objects in JavaScript are nothing but a non-primitive type data structure. We can define an object using curly brackets and put key-value pairs separated by a comma in it.</p>
<p>EG:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
   <span class="hljs-attr">name</span>:<span class="hljs-string">"Hemendra"</span>,
   <span class="hljs-attr">nickname</span>:<span class="hljs-string">"Hemu"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
   <span class="hljs-attr">city</span>:<span class="hljs-string">"bhilwara"</span>
}
</code></pre>
<h2 id="heading-crud-operations-on-an-object">CRUD operations on an object</h2>
<p><strong>Create</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> student = {}; <span class="hljs-comment">// an empty array</span>

student.name = <span class="hljs-string">"Hemendra Khatik"</span>; 
<span class="hljs-comment">//important -&gt; student["name"]="Hemendra Khatik" is also valid</span>

student.branch = <span class="hljs-string">"CSE"</span>;
student.age = <span class="hljs-number">25</span>;

<span class="hljs-built_in">console</span>.log(student); <span class="hljs-comment">// will print the below structure</span>
<span class="hljs-comment">/*
{
   name:"Hemendra Khatik",
   branch:"CSE",
   age:25
}
*/</span>
</code></pre>
<p>or all key values at once</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> user = {
   <span class="hljs-attr">name</span>:<span class="hljs-string">"Hemendra"</span>,
   <span class="hljs-attr">nickname</span>:<span class="hljs-string">"Hemu"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
   <span class="hljs-attr">city</span>:<span class="hljs-string">"bhilwara"</span>
}
</code></pre>
<p><strong>Read</strong></p>
<p>We use <code>.</code> operator to access the values of an object.</p>
<pre><code class="lang-js">user.name; <span class="hljs-comment">// to access the "Hemendra".</span>
user.city; <span class="hljs-comment">// to access the "bhilwara".</span>
user.username; <span class="hljs-comment">// will be undefined because it is not present in the user object.</span>
</code></pre>
<p>We can also access properties of an object using square brackets.</p>
<pre><code class="lang-js">user[<span class="hljs-string">"name"</span>];    <span class="hljs-comment">// to access the "Hemendra".</span>
user[<span class="hljs-string">"city"</span>];    <span class="hljs-comment">// to access the "bhilwara".</span>
</code></pre>
<p><strong>Update</strong></p>
<p>Update an object .</p>
<pre><code class="lang-js">student.age = <span class="hljs-number">21</span>; <span class="hljs-comment">// now age is changed from 25 to 21</span>
</code></pre>
<p><strong>Delete</strong></p>
<p>Delete key from an object.</p>
<pre><code class="lang-js"><span class="hljs-keyword">delete</span> student.name; <span class="hljs-comment">// to delete the name key from student object</span>
</code></pre>
<h2 id="heading-other-helpful-methods">Other Helpful methods</h2>
<p>print the keys only from an object.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
   <span class="hljs-attr">username</span>:<span class="hljs-string">"aamchora"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
};

<span class="hljs-built_in">Object</span>.keys(user);
<span class="hljs-comment">/* 
Above expression returns an array containing 
keys only from an object ["username", "email"]
*/</span>
</code></pre>
<p>print the values only from an object.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
   <span class="hljs-attr">username</span>:<span class="hljs-string">"aamchora"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
};

<span class="hljs-built_in">Object</span>.values(user);
<span class="hljs-comment">/* 
Above expression returns an array containing 
values only from an object ["aamchora", "hi@aamchora.space"]
*/</span>
</code></pre>
<p>Freeze the JavaScript Object</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
   <span class="hljs-attr">username</span>:<span class="hljs-string">"aamchora"</span>,
   <span class="hljs-attr">email</span>:<span class="hljs-string">"hi@aamchora.space"</span>,
};

<span class="hljs-built_in">Object</span>.freeze(user);
<span class="hljs-comment">/* 
Above expression freezes the object. 
It means we cannot add, remove or update its properties.
It will not throw any error unless you are in strict mode but
there will be no effect of value change on your object.
*/</span>

user.username = <span class="hljs-string">"NewUserName"</span>; <span class="hljs-comment">// --&gt; will not work</span>
user.newKey = <span class="hljs-string">"xyz"</span>; <span class="hljs-comment">// --&gt; will not work</span>
<span class="hljs-keyword">delete</span> user.email; <span class="hljs-comment">// --&gt; will not work</span>

<span class="hljs-built_in">console</span>.log(user);
<span class="hljs-comment">/*
{
  username:"aamchora",
  email:"hi@aamchora.space",
}
*/</span>
</code></pre>
<h2 id="heading-cloning-an-object">Cloning an object</h2>
<blockquote>
<p>You can not copy non-primitive type data structure as you copy primitive type data structure.</p>
</blockquote>
<p>EG: Primitive type data structure. </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> b = a;
b= b + <span class="hljs-number">1</span>;
<span class="hljs-built_in">console</span>.log(a) <span class="hljs-comment">// 10</span>
<span class="hljs-built_in">console</span>.log(b) <span class="hljs-comment">// 11</span>
</code></pre>
<p>EG: Non-Primitive type data structure. </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> obj = {
 <span class="hljs-attr">a</span>:<span class="hljs-number">10</span>;
};
<span class="hljs-keyword">let</span> obj2 = obj;
obj2.a = <span class="hljs-number">12</span>;

<span class="hljs-keyword">if</span>(obj === obj2){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are same"</span>);
};
<span class="hljs-comment">// above condition will be true </span>

<span class="hljs-built_in">console</span>.log(obj) <span class="hljs-comment">// {a:12}</span>
<span class="hljs-built_in">console</span>.log(obj2) <span class="hljs-comment">// {a:12}</span>
</code></pre>
<p>To understand this weird behavior of JavaScript, you can read below blog. </p>
<p><a target="_blank" href="https://dev.to/aamchora/never-change-arrays-and-objects-directly-in-javascript-5317">Click here to read</a></p>
<blockquote>
<p>We can use <code>Object.assign(targetObject, sourceObject)</code> to clone an object.</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> };
<span class="hljs-keyword">const</span> obj2 = <span class="hljs-built_in">Object</span>.assign({}, obj);
obj2.a = <span class="hljs-number">2</span>;

<span class="hljs-keyword">if</span>(obj === obj2){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are same"</span>);
}<span class="hljs-keyword">else</span>{
 <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are different"</span>);
};

<span class="hljs-comment">// above condition will be false here </span>

<span class="hljs-built_in">console</span>.log(obj); <span class="hljs-comment">// { a: 1 }</span>
<span class="hljs-built_in">console</span>.log(obj2); <span class="hljs-comment">// { a: 2 }</span>
</code></pre>
<p><strong>Shallow copy</strong>
A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made.</p>
<p>Note: For Shallow copy we use <code>Object.assign(targetObject, sourceObject)</code>.</p>
<p>Look at below example to understand Shallow copy.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> obj = {
 <span class="hljs-attr">a</span>:<span class="hljs-number">1</span>
}

<span class="hljs-keyword">const</span> obj2 = {
 <span class="hljs-attr">b</span>:<span class="hljs-number">2</span>,
 <span class="hljs-attr">c</span>:obj <span class="hljs-comment">// here c's value is a reference to the other object called obj</span>
}

<span class="hljs-keyword">const</span> copy = <span class="hljs-built_in">Object</span>.assign({},obj2); <span class="hljs-comment">// here obj2 is a source object </span>

<span class="hljs-comment">// changing the copied object's value </span>
copy.c.a = <span class="hljs-number">1111</span>;


<span class="hljs-keyword">if</span>(copy.c.a === obj.a){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are same"</span>);
}
<span class="hljs-comment">// above condition will be true</span>
</code></pre>
<p><strong>Deep copy</strong></p>
<p>A deep copy of an object is a copy whose properties do not share the same references(point to the same underlying values)  as those of the source object from which the copy was made.</p>
<p>Note: For Deep copy we use <code>JSON.parse(JSON.stringify(sourceObject))</code>.</p>
<p>Look at below example to understand Deep copy.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> obj = {
 <span class="hljs-attr">a</span>:<span class="hljs-number">1</span>
}

<span class="hljs-keyword">const</span> obj2 = {
 <span class="hljs-attr">b</span>:<span class="hljs-number">2</span>,
 <span class="hljs-attr">c</span>:obj <span class="hljs-comment">// here c's value is a reference to the other object called obj</span>
}

<span class="hljs-keyword">const</span> copy = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(obj2)) <span class="hljs-comment">// here obj2 is a source object </span>

<span class="hljs-comment">// changing the copied object's value </span>
copy.c.a = <span class="hljs-number">1111</span>;


<span class="hljs-keyword">if</span>(copy.c.a === obj.a){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are same"</span>);
}<span class="hljs-keyword">else</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"obj and obj2 are not same"</span>);
}
<span class="hljs-comment">// above condition will be false</span>
</code></pre>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
]]></content:encoded></item><item><title><![CDATA[Why scalability is important?]]></title><description><![CDATA[Scalable tech sounds SCALABLE but what makes scalability important???
Remember - we cannot build scalable tech in a jiffy.
We all know it is important to ship fast features when a product needs to thrive and live up to the expectations of users and s...]]></description><link>https://blog.aamchora.space/why-scalability-is-important</link><guid isPermaLink="true">https://blog.aamchora.space/why-scalability-is-important</guid><category><![CDATA[scalability]]></category><category><![CDATA[clean code]]></category><category><![CDATA[technology]]></category><category><![CDATA[Startups]]></category><dc:creator><![CDATA[Hemendra Khatik]]></dc:creator><pubDate>Fri, 24 Jun 2022 15:58:15 GMT</pubDate><content:encoded><![CDATA[<p>Scalable tech sounds <strong>SCALABLE</strong> but what makes scalability important???</p>
<p><strong>Remember</strong> - we cannot build scalable tech in a jiffy.</p>
<p>We all know it is important to ship fast features when a product needs to thrive and live up to the expectations of users and stakeholders. </p>
<p>But in most startups, it doesn’t work that way.</p>
<p>Founders often assume that</p>
<ul>
<li>Hiring a bigger team of developers will ship features faster.</li>
<li>Code is perfect as long as the feature functions well.</li>
<li>It’s not important to create a scalable tech or clean code.</li>
</ul>
<p>These assumptions backfire as slow progress when they wish to scale. On top of it, they end up paying high costs for building tiny features which takes more time and effort.</p>
<p>But, what if? I tell you there is a way </p>
<blockquote>
<p>You can add any feature and functionality at very minimal cost &amp; effort in no time.</p>
</blockquote>
<p>Yes, it's possible!</p>
<h3 id="heading-things-to-keep-in-mind-while-building-a-scalable-tech">Things to keep in mind while building a scalable tech.</h3>
<ol>
<li>Code scalability.</li>
<li>Server scalability.</li>
</ol>
<h3 id="heading-code-scalability"><strong>Code scalability</strong></h3>
<p>Writing scalable code is super important for large-scale apps. </p>
<p>Because -</p>
<ol>
<li>It helps multiple teams work together in a single codebase conveniently. </li>
<li>It gets easier to add new features.</li>
<li>It takes less time to understand the code and focus more on the building.</li>
</ol>
<p><strong>Your code should be clean and maintainable.</strong></p>
<ul>
<li>Components, functions, and variable names should be meaningful.</li>
<li>Use comments to explain the code logic.</li>
<li>Use DRY and KISS rules while writing code but don’t abuse these rules.</li>
</ul>
<p><strong>Code navigation.</strong> </p>
<ul>
<li>Navigating through your code should be easy.</li>
<li>Create a good folder structure and group similar files altogether.</li>
</ul>
<p><strong>Well-documented code.</strong></p>
<ul>
<li>Reading documentation is fun but writing one is painful. Have some empathy, and don’t be lazy.</li>
<li>Don’t keep flowcharts and algorithms in your mind. Spit it out in the documentation.</li>
</ul>
<h3 id="heading-server-scalability"><strong>Server scalability.</strong></h3>
<p>At any stage, your app users can drastically increase which means increased API requests. In most cases, single server can’t handle it well.</p>
<p>In such conditions, you don’t want your app to crash. To prevent your app from crashing you can do the following -</p>
<ol>
<li><strong>Horizontal scaling.</strong> <ol>
<li>Buy more servers.</li>
<li>Setup load-balancer.</li>
</ol>
</li>
<li><strong>Vertical scaling.</strong><ol>
<li>Buy bigger server.</li>
</ol>
</li>
<li><strong>Hybrid scaling(Horizontal + Vertical).</strong><ol>
<li>Buy more bigger servers.</li>
</ol>
</li>
</ol>
<hr />
<p><em><strong>follow me for more such blog posts.</strong></em></p>
<p><em>Let me know if this blog was helpful.</em></p>
]]></content:encoded></item></channel></rss>