<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ekansh Kothiyal</title>
    <description>The latest articles on DEV Community by Ekansh Kothiyal (@chunkybyte).</description>
    <link>https://dev.to/chunkybyte</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%2F412629%2F7132fb53-7784-4783-b613-0df2d2c2635d.jpg</url>
      <title>DEV Community: Ekansh Kothiyal</title>
      <link>https://dev.to/chunkybyte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chunkybyte"/>
    <language>en</language>
    <item>
      <title>:/javascript/1 - Compiled Or Interpreted? The mystery of JavaScript translation.</title>
      <dc:creator>Ekansh Kothiyal</dc:creator>
      <pubDate>Sat, 24 Oct 2020 09:37:27 +0000</pubDate>
      <link>https://dev.to/chunkybyte/javascript-1-compiled-or-interpreted-the-mystery-of-javascript-translation-38oh</link>
      <guid>https://dev.to/chunkybyte/javascript-1-compiled-or-interpreted-the-mystery-of-javascript-translation-38oh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Is JavaScript a compiled language or is it interpreted? And why are people so torn between this simple choice?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We know what are the compiled and interpreted languages, as discussed in  &lt;a href="https://blog.chunkybyte.xyz/javascript-0-bare-minimum-computer-fundamentals" rel="noopener noreferrer"&gt;the previous article in the series&lt;/a&gt;. Let's look into it in more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;If you're looking for an answer 10 mins before your front-end interview starts - JavaScript is both and say the phrase "Just-In-Time (JIT) Compiler" a couple of times.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You're welcome!&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Now let's dive deeper for you, my beautiful explorers of the JS wilderness!
&lt;/h3&gt;

&lt;p&gt;Let's quickly get an overview of what these types of languages mean based on the traditional translations -&lt;/p&gt;

&lt;h4&gt;
  
  
  Compiled Language
&lt;/h4&gt;

&lt;p&gt;A compiled language is translated directly into native machine code &lt;em&gt;(imagine a file that only contains 0s and 1s)&lt;/em&gt; that the processor can execute. Examples: C, C++, Rust, and Go.&lt;/p&gt;

&lt;h4&gt;
  
  
  Interpreted Language
&lt;/h4&gt;

&lt;p&gt;An interpreted language, on the other hand, is read line by line and executed by an interpreter. Examples: PHP, Python, and Ruby.&lt;/p&gt;




&lt;p&gt;Now that the basics are dusted and settled, we continue our exploration of whether JavaScript is compiled or interpreted…&lt;/p&gt;

&lt;h3&gt;
  
  
  But before we dive any deeper…
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Programming languages can have both - &lt;strong&gt;compiled and interpreted implementations&lt;/strong&gt;. It is not part of the language's specification but an implementation decision.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Yes, you read that right!&lt;/em&gt; If a language is compiled or interpreted, it's not really the language's call but the way that it has been implemented.&lt;/p&gt;




&lt;h3&gt;
  
  
  Let's Start The Game, Watson!
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Once you eliminate the impossible, whatever remains, however improbable, must be the truth. - &lt;strong&gt;Sherlock Holmes&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Suspect 1: Is JavaScript Compiled?
&lt;/h3&gt;

&lt;p&gt;Let's take a peek into the popular compiled language &lt;strong&gt;C&lt;/strong&gt;. So we write some C code in an editor (a &lt;code&gt;"sourcecode".c&lt;/code&gt; file) and after four steps of the compilation process- &lt;em&gt;preprocessing, compiling, assembling, and linking&lt;/em&gt; (&lt;em&gt;ignore the fancy terms for today&lt;/em&gt;), the compiler will create an output native code (in a &lt;code&gt;"compiledcode".out&lt;/code&gt; file). This compiled file is what we run to see the result of our program.&lt;/p&gt;

&lt;p&gt;So for every little change, we compile our program again and then run our executable. &lt;em&gt;If I had a nickel for every little change I had to make on the fly while coding, I would... I don't know, have bought a ton of bitcoin or something!&lt;/em&gt; Now if I had to compile my code after every little change on the web, &lt;em&gt;eish!&lt;/em&gt; Clearly, that's not how we do things in JavaScript. We are directly running our source code, the javascript we wrote, on the browser. There is no object code that needs to be run. So…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict: Not Compiled!&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Suspect 2: Is Javascript Interpreted?
&lt;/h3&gt;

&lt;p&gt;As we know, web developers need to code stuff and run things quickly. Interpreters fit the job description perfectly here. We write a line and hit refresh - &lt;em&gt;voila!&lt;/em&gt; Having compiler in this equation, &lt;em&gt;no thanks.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Many definitions on the Internet seem to suggest its Interpreted. &lt;strong&gt;So let's try this example code:&lt;/strong&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603481240151%2FFcZCkjaC7.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603481240151%2FFcZCkjaC7.png" title="Hoisting in JavaScript" alt="2-hoisting-js.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, it works if you try. So from what we know about the interpreter, it will read line 3 and execute the function &lt;code&gt;foo&lt;/code&gt; and pri… &lt;em&gt;wait how does the Interpreter know of the function foo which is down at line number 5? Huh.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can't be an Interpreter job! How could it know what's coming in the next line? We can rule that out now. &lt;em&gt;There is some aspect of compilation involved here, Watson!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict: Not Interpreted!&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Blistering Barnacles, Watson! We are out of suspects! 
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Wait… what if we are not in a Sherlock Holmes novel but an Agatha Christie story. Like the plot of the book *censored to avoid giving out spoilers&lt;/em&gt;, when we are out of suspects, that means everyone is a suspect!*&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's both!  * &lt;em&gt;gasps&lt;/em&gt; *&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The interpreter is amazing if you need to run the code once but when it happens multiple times, it becomes significantly slower in execution as compared to the compiled native code. So there was a need to shrink this gap - &lt;strong&gt;enter Just-In-Time compilation&lt;/strong&gt; over traditional static compilation!&lt;/p&gt;

&lt;h3&gt;
  
  
  Criminal: Just-In-Time Compiler - the best of both worlds!
&lt;/h3&gt;

&lt;p&gt;It used to be just &lt;em&gt;interpreters&lt;/em&gt; back in the day. But in order to overcome the significant inefficiency gap, the browsers started bringing in the &lt;em&gt;compilers&lt;/em&gt; for optimizing the process better.&lt;/p&gt;

&lt;p&gt;So in a nutshell, the compilation, in this case, happens during the execution.&lt;/p&gt;

&lt;p&gt;Every browser has its own way of handling this but generally, a new component called a &lt;strong&gt;&lt;em&gt;Monitor (or Profiler)&lt;/em&gt;&lt;/strong&gt; is added to the translator, &lt;em&gt;JS Engine&lt;/em&gt;. This &lt;em&gt;Profiler&lt;/em&gt; monitors the code for redundant code and the object types.&lt;/p&gt;

&lt;h4&gt;
  
  
  (A Very) Basic Workflow
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;First&lt;/em&gt;, the &lt;em&gt;Profiler&lt;/em&gt; goes over everything. It monitors the process. If a code block runs a lot of times, it is marked &lt;strong&gt;"hot"&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the initial run, a non-optimized &lt;em&gt;Abstract Syntax Tree&lt;/em&gt; is generated and handed over to the interpreter to execute &lt;em&gt;(will cover in future posts, for now, it's a tree representation of your code)&lt;/em&gt;. This is to avoid any more delay in execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meanwhile there is an optimization step. The "&lt;em&gt;hot&lt;/em&gt;" code block the &lt;em&gt;profiler&lt;/em&gt; marked earlier is sent to another optimizing compiler that converts it to a faster and more optimized version of itself after proper checks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If all things look fine, this optimized version is sent and we have a better code. However, if there are some problems with this optimized code, JIT will &lt;em&gt;deoptimize *and trash this optimization, the non-optimized machine code is executed instead. *FYI this has a small performance hit.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So the profiler and the compiler are working together to give you the best possible performance. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Therefore,
&lt;/h4&gt;

&lt;p&gt;The JIT compilation makes your code faster through monitoring the code and optimizing the recurring similar code blocks. And yes, this has its own overhead added to the process but the performance gains outweigh those.&lt;/p&gt;

&lt;p&gt;Next in the series, we &lt;em&gt;might *take a look into the JavaScript runtime ecosystem. *Whatever that means&lt;/em&gt; :/&lt;/p&gt;

&lt;p&gt;💜&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;[1]  &lt;a href="https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/" rel="noopener noreferrer"&gt;A crash course in just-in-time (JIT) compilers by Lin Clark.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design/138541#138541" rel="noopener noreferrer"&gt;Mike Samuel on Is JavaScript interpreted by design?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3]  &lt;a href="https://blog.bitsrc.io/how-does-javascript-really-work-part-1-7681dd54a36d" rel="noopener noreferrer"&gt;How Does JavaScript Really Work? (Part 1) by Priyesh Patel&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;I am writing a series of JavaScript articles aimed at understanding the architecture of this language in the simplest language possible (for me). There are plenty of resources aimed at syntax learning. Not many people understand the jargons that are around a lot or what is really happening behind the scenes. Hope you like it! &lt;/p&gt;

&lt;p&gt;In the words of Ed Sheeran, "&lt;em&gt;My, my, my, my, oh give me love&lt;/em&gt;" 💜&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>firstyearincode</category>
    </item>
    <item>
      <title>:/javascript/0 - The Bare Minimum Computer Fundamentals</title>
      <dc:creator>Ekansh Kothiyal</dc:creator>
      <pubDate>Wed, 21 Oct 2020 07:09:37 +0000</pubDate>
      <link>https://dev.to/chunkybyte/javascript-0-the-bare-minimum-computer-fundamentals-15j6</link>
      <guid>https://dev.to/chunkybyte/javascript-0-the-bare-minimum-computer-fundamentals-15j6</guid>
      <description>&lt;p&gt;&lt;em&gt;It's 2020!&lt;/em&gt; Everyone is saying developing websites is easy. &lt;em&gt;It is.&lt;/em&gt; You learn the basics of HTML and  CSS. &lt;em&gt;Done!&lt;/em&gt; Now you start learning JavaScript. &lt;em&gt;You can officially say at parties that you create websites.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So what is JavaScript?&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript is a synchronous, blocking, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles. Javascript is an &lt;strong&gt;exasperating farrago of distortions!&lt;/strong&gt; [1]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Yeah, of course, it's synchronous and single-threaded and that "farragos" thing… I know that! I think I can start with React now.&lt;/em&gt;&lt;br&gt;
[Dramatization based on real-life events]&lt;/p&gt;

&lt;p&gt;I don't know about others but I, sure as hell, took my sweet amount of time to &lt;em&gt;really&lt;/em&gt; know what all that "&lt;em&gt;farragos&lt;/em&gt;" and alike, all meant. Being a good samaritan, I thought I should create a guide. This is for beginners and people like me who were in a hurry to skip the JS basics.&lt;/p&gt;

&lt;p&gt;For learning the coding syntax and implementation, I always suggest starting with  &lt;a href="http://freecodecamp.org/"&gt;freecodecamp.org&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  The Introduction
&lt;/h1&gt;

&lt;p&gt;I'll start with the assumption that you know a little about computers but really, &lt;strong&gt;You know nothing, Jon Doe!&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Javascript is a high-level language.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;What does that mean?&lt;/em&gt; High-level language syntax looks closely like the English language more than it looks like the machine code. In contrast, a &lt;em&gt;low-level language&lt;/em&gt; would look more closer to machine code though will not be machine code. This is how you code in a high-level language - a simple example of adding two numbers 1234 and 4321:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_1_gpsy_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603021751509/-ySMZj73y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_1_gpsy_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603021751509/-ySMZj73y.png" alt="High Level Language Code Snippet to add two numbers" width="880" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Computers don't understand that, &lt;em&gt;not really&lt;/em&gt;. They talk in machine code. Machine code is just combinations of 0s and 1s &lt;em&gt;- the language of computers&lt;/em&gt;. It looks something like this (the same example of adding 1234 and 4321):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--79HxGB7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603022377011/oKrGPlRjw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--79HxGB7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603022377011/oKrGPlRjw.gif" alt="Machine Code Snippet to add two numbers" width="468" height="205"&gt;&lt;/a&gt;&lt;br&gt;
&lt;small&gt;&lt;em&gt;Source: &lt;a href="http://www.dspguide.com/ch4/5.htm"&gt;http://www.dspguide.com/ch4/5.htm&lt;/a&gt;&lt;/em&gt;&lt;small&gt;&lt;/small&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Translation
&lt;/h3&gt;

&lt;p&gt;So now we have two entities - &lt;em&gt;a computer and you, the user&lt;/em&gt;. Since we have two entities speaking different languages, we need a translator in order to enable a "&lt;em&gt;conversation&lt;/em&gt;" between the two.&lt;/p&gt;

&lt;p&gt;Fundamentally, we have three ways of these translations:&lt;br&gt; &lt;br&gt;
a. Assembler&lt;br&gt;&lt;br&gt;
b. Compiler&lt;br&gt;&lt;br&gt;
c. Interpreter&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Assembler&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The assembler converts low-level assembly code into machine code - &lt;em&gt;what's assembly code?&lt;/em&gt; For now, it's basically just a "placeholder instruction" of a combination of bits that mean something to your computer. You are more in control of instructing what the computer does, one instruction at a time. Example: I want to instruct my computer to allocate two bytes of storage to a variable "&lt;em&gt;x&lt;/em&gt;" and initialize it with a value 23. The assembly language code would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x: .word   23
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it's different from other approaches is that it just does a 1-on-1 translation of mnemonics and literally just replace the labels with their machine code counterpart. I would suggest Google about it to know more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Compiler and Interpreter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Compiler will "&lt;em&gt;process&lt;/em&gt;" the source file to machine code (or bytecode) before executing while an Interpreter will start "&lt;em&gt;reading&lt;/em&gt;" the source code one line at a time and start executing.&lt;/p&gt;

&lt;p&gt;I'll borrow &lt;a href="https://medium.com/@almog4130/javascript-is-it-compiled-or-interpreted-9779278468fc"&gt;Almog Adziashvili's fun alcohol analogy&lt;/a&gt; to explain the difference.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's make a Mojito! &lt;br&gt;&lt;br&gt;
You need - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;White Rum&lt;/li&gt;
&lt;li&gt;Fresh Lime Juice&lt;/li&gt;
&lt;li&gt;2 teaspoons sugar&lt;/li&gt;
&lt;li&gt;6 mint leaves&lt;/li&gt;
&lt;li&gt;Soda Water

We have two ways - the Compiler Way and the Interpreter Way.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;**&lt;em&gt;Compiler Way:&lt;/em&gt; ** We gather all the ingredients and the amount of those ingredients required in one place. Now we add all these in a blender together to mix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Interpreter Way:&lt;/em&gt;&lt;/strong&gt; We take the blender glass and start getting the ingredients one at a time. First the White Rum from the bar cabinet, then we go to the refrigerator to get the lime juice and run the blender, then add the sugar and run the blender, and so on.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Voila!&lt;/em&gt; We have the mojito ready and it's going to taste the same both ways.&lt;/p&gt;

&lt;p&gt;In a compilation, the &lt;strong&gt;time to build a compiled code&lt;/strong&gt; (i.e. the ingredient preparation time) is longer than the interpreter way BUT the &lt;strong&gt;run time&lt;/strong&gt; (i.e. the time to mix the ingredients) is less than the interpreter way.&lt;/p&gt;

&lt;p&gt;Continuing on the example, you will have to gather the ingredients again before the blending part, whenever your mom has cleaned the kitchen again or you are making a mojito in a different house. So this process of building the compiled code is going to happen every time you want to run your source code after a change (however small) or in a different environment.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Closing argument:&lt;/em&gt; The compiler is usually more secure since the compiled code is just machine code. It needs no source code to run. So no source is exposed. This is faster and provides a ready-to-run executable on the machine. &lt;br&gt;
The interpreter is faster during the development phase - quick debugging and can be run on any machine that can support it. A little slower on runtime but faster while creating prototypes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question of the Hour: So is JavaScript compiled or interpreted?
&lt;/h2&gt;

&lt;p&gt;Take your time to think. The elders call it interpreted. So it has a line by line execution way. &lt;em&gt;Seems about right.&lt;/em&gt; Is it though? &lt;br&gt;
&lt;br&gt;*music intensifies*&lt;br&gt;
Get your mind gears moving and wait for tomorrow!&lt;br&gt;
&lt;br&gt;&lt;br&gt; I am posting about it in detail in the next article in the series this week.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;[1] Referencing a  &lt;a href="https://qz.com/india/980055/shashi-tharoor-on-farrago-an-indian-politicians-tweet-made-the-entire-country-look-up-an-obscure-word/"&gt;viral tweet in 2017&lt;/a&gt;  from the Indian Politician, Dr. Shashi Tharoor. &lt;br&gt;&lt;br&gt;
[2]  &lt;a href="https://medium.com/@almog4130/javascript-is-it-compiled-or-interpreted-9779278468fc"&gt;JavaScript - is it Compiled or Interpreted?&lt;/a&gt; by &lt;a href="https://medium.com/@almog4130"&gt;Almog Adziashvili&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;I am writing a series of JavaScript articles aimed at understanding the architecture of this language in the simplest language possible. There are plenty of resources aimed at syntax learning. Not many people understand the jargons that are around a lot or what is really happening behind the scenes. Hope you like it! &lt;br&gt;&lt;em&gt;Show some love (in any form really) &amp;lt;3&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>firstyearincode</category>
    </item>
    <item>
      <title>Duck Punching in JavaScript (with Example)</title>
      <dc:creator>Ekansh Kothiyal</dc:creator>
      <pubDate>Fri, 19 Jun 2020 20:32:24 +0000</pubDate>
      <link>https://dev.to/chunkybyte/duck-punching-in-javascript-with-example-4nkf</link>
      <guid>https://dev.to/chunkybyte/duck-punching-in-javascript-with-example-4nkf</guid>
      <description>&lt;p&gt;We have the famous &lt;em&gt;Duck Test&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Coasting off that, we have &lt;strong&gt;Duck Punching&lt;/strong&gt; (or &lt;em&gt;Monkey Patching&lt;/em&gt;) in programming — a technique wherein alterations are made to a dynamic code during runtime without altering the source code.&lt;/p&gt;

&lt;p&gt;Here is a great &lt;a href="https://consolia-comic.com/"&gt;Consolia Comic&lt;/a&gt; strip to give you a better understanding in a funnier graphical way (if you’re into that):&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nvmv5P-x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.consolia-comic.com/comics/ducks-1-duck-punching.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nvmv5P-x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.consolia-comic.com/comics/ducks-1-duck-punching.png" alt="Duck Punching - Consolia Comics" width="625" height="2822"&gt;&lt;/a&gt;&lt;/p&gt;
Source: &lt;a href="https://consolia-comic.com/comics/ducks-1-duck-punching"&gt;Consolia-Comic&lt;/a&gt;





&lt;p&gt;So let’s dive straight into an example and get our hands a little dirty:&lt;/p&gt;
&lt;h3&gt;
  
  
  Problem Statement
&lt;/h3&gt;

&lt;p&gt;We have the popular &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"&gt;&lt;code&gt;indexOf&lt;/code&gt; function&lt;/a&gt; in JavaScript’s String prototype. The indexOf function returns the first occurrence of the search value inside the string that calls it.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;King, are you glad you are the king?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;you&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// returns 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Suppose you wanted to do something extra on top of this function by extending the legacy &lt;code&gt;indexOf&lt;/code&gt; function. You were asked to add an optional parameter that would give you the last occurrence index instead — a simple boolean flag.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let’s do some duck punching -
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There you go! But let’s break that down a little bit.&lt;/p&gt;

&lt;p&gt;We start with the simple Duck Punching pattern:&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;oldMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// Do Your Magik Thang!&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;oldMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arguments&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;&lt;strong&gt;Explanation:&lt;/strong&gt; In the first line — we save the legacy prototype method inside a variable. In the subsequent lines, we redefine this method and do our &lt;em&gt;‘thang’&lt;/em&gt; and we have extended the capabilities of a legacy function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, we initialize the old method to a new variable and then assign the &lt;code&gt;indexOf&lt;/code&gt; method to a new method.&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;legacyIndexOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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;&lt;strong&gt;Second&lt;/strong&gt;, we add a new parameter &lt;code&gt;getLastIndexFlag&lt;/code&gt; that defaults to false and we have two conditions based on its value. If the &lt;code&gt;getLastIndexFlag&lt;/code&gt; value passed is &lt;code&gt;false&lt;/code&gt;/&lt;code&gt;null&lt;/code&gt;, we use the existing &lt;code&gt;indexOf()&lt;/code&gt; functionality in the &lt;code&gt;if&lt;/code&gt; block. But if the &lt;code&gt;getLastIndexFlag&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;, we get into the &lt;code&gt;else&lt;/code&gt; block where we reverse the string using the Array method and call our legacy &lt;code&gt;indexOf()&lt;/code&gt;.&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getLastIndexFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;getLastIndexFlag&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="nx"&gt;legacyIndexOf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;legacyIndexOf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt;&lt;span class="p"&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;Let’s try it out:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;King, are you glad you are the king?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;you&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&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;// returns 14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila! There is your better version of &lt;code&gt;indexOf&lt;/code&gt; as per your workplace needs!&lt;/p&gt;

&lt;p&gt;Note: The 2nd parameter - &lt;code&gt;null&lt;/code&gt; is passed for the &lt;code&gt;fromIndex&lt;/code&gt; parameter that tells what index to start the search for in the original &lt;code&gt;indexOf&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So that was one example of &lt;em&gt;Duck Punching&lt;/em&gt;. You can basically extend from the idea and work with your use case. A great way to articulate the advantage of this is how &lt;a href="https://www.paulirish.com/2010/duck-punching-with-jquery/"&gt;Paul Irish&lt;/a&gt; puts it -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;a way to fulfill your own feature request on a library.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Future
&lt;/h3&gt;

&lt;p&gt;Duck Punching has somewhat become less of a thing now by 2020 (or so I have been told by my more experienced colleagues). There are better methods using do the same thing &lt;em&gt;(yet to explore)&lt;/em&gt;. It still was a fun thing to read about and from my experience still a common coding tool in your back pocket.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cheers!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>duckpunching</category>
    </item>
  </channel>
</rss>
