<?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: Amandeep Singh</title>
    <description>The latest articles on DEV Community by Amandeep Singh (@aman_singh).</description>
    <link>https://dev.to/aman_singh</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%2F188553%2F00a5e300-0a48-43af-abc7-3ff24d18ab7d.jpeg</url>
      <title>DEV Community: Amandeep Singh</title>
      <link>https://dev.to/aman_singh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aman_singh"/>
    <language>en</language>
    <item>
      <title>Thinking about accessibility—a few tips to  make a difference</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 20 Jun 2021 06:47:29 +0000</pubDate>
      <link>https://dev.to/aman_singh/thinking-about-accessibility-a-few-tips-to-make-a-difference-461i</link>
      <guid>https://dev.to/aman_singh/thinking-about-accessibility-a-few-tips-to-make-a-difference-461i</guid>
      <description>&lt;p&gt;While conducting an interview session, I asked a candidate about what web accessibility is, the answer was, "Isn't it about aria-label?". It made me smile. It wasn't an unexpected answer. I have experienced the same replies many times while bringing this topic up casually over a cup of coffee chat. &lt;/p&gt;

&lt;p&gt;I know where the root problem lies. I believe it's the lack of awareness and knowledge about accessibility in general. It's a mindset that we consider accessibility as an &lt;strong&gt;optional thing&lt;/strong&gt;. Web accessibility is rarely seen as a part of acceptance criteria when writing Jira tickets.&lt;/p&gt;

&lt;p&gt;Well, being a web developer, we can change this mindset. We can educate ourselves, spread awareness, share the guidelines. Together we can make the web more inclusive and accessible.&lt;/p&gt;

&lt;p&gt;With the motivation aside, It's time to look into a few tips that can quickly get you up and running with accessibility in mind. After all, it's not that scary as you might have thought. Time to make a difference. &lt;/p&gt;




&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use Semantic elements&lt;/li&gt;
&lt;li&gt;Don't use divs for creating a button&lt;/li&gt;
&lt;li&gt;Use 'skip to main content' link&lt;/li&gt;
&lt;li&gt;Don't forget the focus state&lt;/li&gt;
&lt;li&gt;Never miss the 'alt' tag on your image&lt;/li&gt;
&lt;li&gt;Link text should describe the purpose of the link&lt;/li&gt;
&lt;li&gt;No ARIA is better than Bad ARIA&lt;/li&gt;
&lt;li&gt;Implementing your custom UI widgets&lt;/li&gt;
&lt;li&gt;Use ‘lang’ attribute to define the language of the page, and element&lt;/li&gt;
&lt;li&gt;Use jest-axe to automate your a11y tests&lt;/li&gt;
&lt;li&gt;Use CSS to highlight the accessibility issues&lt;/li&gt;
&lt;li&gt;Using Icon only as button&lt;/li&gt;
&lt;li&gt;VisuallyHidden React component&lt;/li&gt;
&lt;li&gt;Color contrast&lt;/li&gt;
&lt;li&gt;Accessibility checklist&lt;/li&gt;
&lt;li&gt;Learn to operate VoiceOver on your Mac&lt;/li&gt;
&lt;li&gt;Use a11y eslint plugin as your dev dependency&lt;/li&gt;
&lt;li&gt;Use ARIA live regions to announce dynamic changes in the page&lt;/li&gt;
&lt;li&gt;Zoom up the page to 200% and see if you can still use the website&lt;/li&gt;
&lt;li&gt;Chrome/FireFox extensions&lt;/li&gt;
&lt;li&gt;Read articles on accessibility, follow blogs/newsletters&lt;/li&gt;
&lt;li&gt;It’s not only about the UI&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Use semantic elements where possible
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/semantics"&gt;Semantic elements&lt;/a&gt; are the ones that convey meaning to both the browser and the developer.
&lt;/li&gt;
&lt;li&gt;They have some &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles"&gt;intrinsic role&lt;/a&gt; associated with them. &lt;/li&gt;
&lt;li&gt;Screen readers would announce their role to help visually impaired users navigate a page.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://support.apple.com/en-au/guide/voiceover/mchlp2719/mac"&gt;VoiceOver rotor&lt;/a&gt; in mac can list available &lt;code&gt;landmarks&lt;/code&gt;, &lt;code&gt;links&lt;/code&gt;, &lt;code&gt;headings&lt;/code&gt; if the semantics elements are used.&lt;/li&gt;
&lt;li&gt;Improves the page search ranking &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/SEO"&gt;SEO&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/semantics"&gt;Semantic elements&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://24ways.org/2017/accessibility-through-semantic-html/"&gt;Accessibility Through Semantic HTML&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Don't use divs for creating a button
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;div&lt;/code&gt; is not a semantic element, it doesn't convey anything to the browser. It's also non-interactive.&lt;/li&gt;
&lt;li&gt;Adding a &lt;code&gt;role="button"&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; enough; it won't make it keyboard accessible.&lt;/li&gt;
&lt;li&gt;You need to add &lt;code&gt;tabindex="0"&lt;/code&gt; to make it focusable.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;button&lt;/strong&gt; element gets you all the aforementioned features for free. &lt;/li&gt;
&lt;li&gt;References: &lt;a href="https://web.dev/use-semantic-html/#use-button-instead-of-div"&gt;Use button instead of divs&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use 'skip to main content' link
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For users with some motor disabilities, it can be hard to navigate every link item before reaching the &lt;code&gt;main&lt;/code&gt; content block.&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;skip to main content&lt;/code&gt; link allows the users to skip the long navigation links, and land straight to the main content.&lt;/li&gt;
&lt;li&gt;Put the &lt;code&gt;skip to main content&lt;/code&gt; link at the top of the page, so that it can receive focus when the &lt;code&gt;Tab&lt;/code&gt; key is hit.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webaim.org/techniques/skipnav/"&gt;Skip Navigation links&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.a11ymatters.com/pattern/skip-link/"&gt;Implementing skip to content link&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/WCAG20-TECHS/G1.html"&gt;WCAG Guidelines&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Don't forget the focus state
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Focus indicator helps users figure out where they are on the page.&lt;/li&gt;
&lt;li&gt;It also benefits the users with short-term memory to discover where the focus is located.&lt;/li&gt;
&lt;li&gt;If you're setting &lt;code&gt;outline: none&lt;/code&gt; for the focus state, make sure to style this state to make focus clear and visible.&lt;/li&gt;
&lt;li&gt;Aid in keyboard navigation as you can see a focus ring around interactive elements when tabbing.&lt;/li&gt;
&lt;li&gt;You can style the focus ring to improve aesthetics; it doesn't have to be ugly.&lt;/li&gt;
&lt;li&gt;Can use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible"&gt;:focus-visible&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hacks.mozilla.org/2019/06/indicating-focus-to-improve-accessibility/"&gt;Indicating-focus-to-improve-accessibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.deque.com/blog/give-site-focus-tips-designing-usable-focus-indicators/"&gt;Designing usable focus indicators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.nicchan.me/blog/tips-for-focus-styles/"&gt;Tips for focus styles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://zellwk.com/blog/style-hover-focus-active-states/"&gt;Style hover, focus, active state&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Never miss the 'alt' tag on your image
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Every image that has a semantic meaning should have a non-empty alt attribute value.&lt;/li&gt;
&lt;li&gt;If the image is used for presentational purposes only (Background gradient), you need to pass &lt;code&gt;alt=""&lt;/code&gt; value. &lt;/li&gt;
&lt;li&gt;Skipping the &lt;code&gt;alt&lt;/code&gt; tag is &lt;strong&gt;not&lt;/strong&gt; an option at all.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abilitynet.org.uk/news-blogs/five-golden-rules-compliant-alt-text"&gt;Five Golden Rules for Image alt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.a11yproject.com/checklist/#images"&gt;Image Accessibility checklist&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Link text should describe the purpose of the link
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It's like your promise to the users. Screen reader users rely on some shortcuts to bring up the links available on your page.&lt;/li&gt;
&lt;li&gt;Link text shouldn't be ambiguous. For example, &lt;code&gt;click here&lt;/code&gt; as link text conveys no meaning. Use meaningful description text.&lt;/li&gt;
&lt;li&gt;Shouldn't be too long. Long text can be hard to comprehend/remember and might miss conveying the actual purpose of a link.&lt;/li&gt;
&lt;li&gt;References: &lt;a href="https://www.w3.org/WAI/WCAG21/Techniques/general/G91.html"&gt;link text purpose (WCAG)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  No ARIA is better than Bad ARIA
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Assistive technologies rely on the aria roles of the elements to convey semantics. It's kind of a promise.&lt;/li&gt;
&lt;li&gt;Using a bad &lt;code&gt;aria&lt;/code&gt; role can cause more harm than good. So choose it wisely.&lt;/li&gt;
&lt;li&gt;Moreover, you don't need to use a &lt;code&gt;role&lt;/code&gt; while using semantic elements. Mostly all semantic elements have implicit roles.&lt;/li&gt;
&lt;li&gt;References:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/#no_aria_better_bad_aria"&gt;No ARIA is better than Bad ARIA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://w3c.github.io/aria-practices/examples/"&gt;ARIA Design Pattern Examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementing your custom UI widgets
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you're implementing your own custom UI widget (e.g &lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal"&gt;Modal&lt;/a&gt;/&lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/#tabpanel"&gt;Tabs&lt;/a&gt;), I'll highly recommend reading &lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/#intro"&gt;WAI-ARIA Authoring Practices&lt;/a&gt; and &lt;a href="https://www.w3.org/WAI/WCAG21/Techniques/#introduction"&gt;WCAG Techniques&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;This will help you to make informed decisions and avoid basic a11y issues. &lt;/li&gt;
&lt;li&gt;Take inspiration from open source libraries that take pride in keeping accessibility as their core feature. &lt;a href="https://reach.tech/"&gt;Reach UI&lt;/a&gt;, &lt;a href="https://chakra-ui.com/"&gt;Chakra UI&lt;/a&gt;, &lt;a href="https://mantine.dev/guides/ssr/"&gt;Mentine&lt;/a&gt;, &lt;a href="https://reakit.io/"&gt;Reakit&lt;/a&gt; are some of the examples.&lt;/li&gt;
&lt;li&gt;References

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/#intro"&gt;WAI-ARIA Authoring Practices Guide&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3.org/WAI/WCAG21/Techniques/#introduction"&gt;WCAG Techniques&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use 'lang' attribute to define the language of the page, and element
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure to use &lt;code&gt;&amp;lt;html lang='prefered-language-of-your-users'&amp;gt;&lt;/code&gt;. For example, &lt;code&gt;&amp;lt;html lang="en"&amp;gt;&lt;/code&gt; would set the language to &lt;code&gt;English&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Allow assistive technologies such as screen readers to invoke the correct pronunciation.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang#language_tag_syntax"&gt;language tag syntax mdn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/WAI/standards-guidelines/act/rules/html-page-lang-b5c3f8/"&gt;WCAG guidelines&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use jest-axe to automate your a11y tests
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you're using jest, it's easy to automate the findings of basic a11y issues via &lt;a href="https://github.com/nickcolley/jest-axe"&gt;jest-axe&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;For example, in React, you can create a test utility function like the following:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;axe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jest-axe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="cm"&gt;/*
 * @param {object} ui element
 * @param {object} axeOptions jest-axe options
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;testA11y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;axeOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isValidElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;axeOptions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveNoViolations&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// And now can use it to test your component in jest like:&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should have no basic a11y issues&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;testA11y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;YourReactComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;References: &lt;a href="https://github.com/nickcolley/jest-axe"&gt;jest-axe&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use CSS to highlight the accessibility issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For example, you can create a CSS selector to draw attention towards the issues. For example, draw a red outline if an image is missing an alt tag. &lt;/li&gt;
&lt;li&gt;And if a developer misses a rule, it would be clearly visible. Check the following examples:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;
  &lt;span class="c"&gt;/* All img tag must have alt attribute. For decorative images, provide empty value (alt="") */&lt;/span&gt;
  &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="nt"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;dotted&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;All&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="n"&gt;elements&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;alt&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c"&gt;/* For tabpanl UI, the &amp;lt;ul&amp;gt; element should have the role of "tablist" */&lt;/span&gt;
  &lt;span class="nc"&gt;.tab&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="nt"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"tablist"&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5em&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c"&gt;/* A elements that don't have a class get default styles */&lt;/span&gt;
  &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="py"&gt;text-decoration-skip-ink&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&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;h3&gt;
  
  
  Using Icon only as button
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;As Icon button has no visible text associated; it's important to make sure the assistive technologies has a way to announce its name.&lt;/li&gt;
&lt;li&gt;There are multiple ways to do so. let's one example as suggested by &lt;a href="https://www.sarasoueidan.com/blog/accessible-icon-buttons/"&gt;Sara Soudein&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
 &lt;span class="c"&gt;&amp;lt;!-- Using visually hidden text, accessible to screen reader --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt; 
   &lt;span class="nt"&gt;&amp;lt;svg&lt;/span&gt; &lt;span class="na"&gt;aria-hidden=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;focusable=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- svg content --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/svg&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"sr-only"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Menu&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt; 

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

&lt;/div&gt;



&lt;p&gt;and the 'sr-only' would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.sr-only&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:focus&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:active&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;clip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
   &lt;span class="nl"&gt;clip-path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
   &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="nl"&gt;white-space&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&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;You can explore other techniques in this great article referenced below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;References: &lt;a href="https://www.sarasoueidan.com/blog/accessible-icon-buttons/"&gt;Accessible Icon Buttons&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  VisuallyHidden React component
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can easily create a reusable react component using the aforementioned sr-only css. It can also be a part of your component library.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="cm"&gt;/**
 * This component will visually hide the content in the DOM, keeping it accessible
 * for the screen reader user.
 */&lt;/span&gt;
 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;srOnly&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;VisuallyHidden&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;as&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;span&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;srOnly&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Component&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;// And then you can use it like: &lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;VisuallyHidden&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="nx"&gt;visually&lt;/span&gt; &lt;span class="nx"&gt;hidden&lt;/span&gt; &lt;span class="nx"&gt;h1&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/VisuallyHidden&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Color contrast
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It's important to make sure that your UI has a minimum contrast ratio as described in &lt;a href="https://www.w3.org/TR/WCAG21/#contrast-minimum"&gt;WCAG guidelines&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Not meeting the color contrast ratio can cause users to perceive your text incorrectly. &lt;/li&gt;
&lt;li&gt;If you are a developer, you can collaborate in the design process, and enforce this constraint before working on implementing the actual UI.

&lt;ul&gt;
&lt;li&gt;References: &lt;/li&gt;
&lt;li&gt;&lt;a href="https://webaim.org/articles/contrast/"&gt;Color contrast WebAim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/WCAG21/#contrast-minimum"&gt;WCAG color contrast guidelines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://accessible-colors.com/"&gt;Accessible colors checker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Accessibility checklist
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The following checklists can come in handy when you visually want to keep track of accessibility standards in your app:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.a11yproject.com/checklist"&gt;accessibility checklist from a11yproject team&lt;/a&gt; can be handy when it comes to keeping things in check.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.a11yengineer.com/"&gt;accessibility checklist from a11yEngineer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Learn to operate VoiceOver on your Mac
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Learning Mac's &lt;a href="https://dequeuniversity.com/tips/learn-voiceover"&gt;VoiceOver&lt;/a&gt; tool is super easy. A few shortcuts and you shall be able to operate it in no time. &lt;/li&gt;
&lt;li&gt;Try using VoiceOver along with a keyboard to browse your website. You will be amazed to see how good or bad your accessibility ranking it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use a11y eslint plugin as your dev dependency
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Nothing is better than having a development tool that could warn you when your UI has some basic a11y issues. &lt;/li&gt;
&lt;li&gt;If your project uses React, &lt;a href="https://github.com/jsx-eslint/eslint-plugin-jsx-a11y"&gt;eslint-plugin-jsx-a11y&lt;/a&gt; is a must-have dev dependency to help you along.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use ARIA live regions to announce dynamic changes in the page
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions"&gt;aria-live&lt;/a&gt; to let the screen reader knows about the new content that would appear as of some user actions.&lt;/li&gt;
&lt;li&gt;Assistive technologies will announce dynamic changes in the content of a live region.&lt;/li&gt;
&lt;li&gt;Example would be an alert/notification that would appear when an action is completed.&lt;/li&gt;
&lt;li&gt;This is important to remember when you're creating a SPA (single page application) where content changes without a full page reload.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions"&gt;ARIA live regions (mdn docs)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitsofco.de/using-aria-live/"&gt;Using aria-live&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Zoom up the page to 200% and see if you can still use the website
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Many users would zoom up your page to 200% or more, and your app should still be functional.&lt;/li&gt;
&lt;li&gt;Content shouldn't overlap and your page should still be usable. &lt;/li&gt;
&lt;li&gt;Make sure interactive elements are still functional.&lt;/li&gt;
&lt;li&gt;References: 

&lt;ul&gt;
&lt;li&gt;WCAG &lt;a href="https://www.w3.org/WAI/WCAG21/quickref/?versions=2.0#resize-text"&gt;Resize Text -- Level AA&lt;/a&gt; success criteria requirement.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Chrome/FireFox extensions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The following web extensions can help you identify a few of your web accessibility issues right from your browser:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://chrome.google.com/webstore/detail/wave-evaluation-tool/jbbplnp"&gt;WebAim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://chrome.google.com/webstore/detail/axe-devtools-web-accessib/lhdoppojpmngadmnindnejefpokejbdd"&gt;AxeDevtool&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://chrome.google.com/webstore/detail/headingsmap/flbjommegcjonpdmenkdiocclhjacmbi?hl=en"&gt;Heading map&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Read articles on accessibility, follow blogs/newsletters
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The main issue behind so many inaccessible web apps is the lack of knowledge and awareness about the accessibility guidelines.&lt;/li&gt;
&lt;li&gt;You can invest some time learning about these standards and then sharing these with your teammates. You need to take the first step.&lt;/li&gt;
&lt;li&gt;I would recommend the following resources if you want to level your accessibility knowledge: 

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.sarasoueidan.com/blog/"&gt;Sara Soudein blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scottohara.me/"&gt;Scott O'Hara blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tink.uk/"&gt;Léonie Watson blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://marcysutton.com/"&gt;Marcy sutton blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.matuzo.at/blog/"&gt;Manuel Matuzovic blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.htmhell.dev/"&gt;HTML Hell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitsofco.de/tag/accessibility/"&gt;Ire Aderinokun articles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.a11yproject.com/"&gt;A11yProject&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://webaim.org/"&gt;WebAim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dequeuniversity.com/"&gt;Deque University&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://inclusive-components.design/"&gt;Inclusive Components by Heydon Pickering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://a11yweekly.com/"&gt;A11yweekly newsletter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  It's not only about the UI
&lt;/h3&gt;

&lt;p&gt;It's not just the UI elements or usage of assistive technologies that correspond to accessibility, it's also about your other web core vitals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;a href="https://developers.google.com/web/tools/lighthouse/"&gt;lighthouse&lt;/a&gt; check and see how well your app is performing overall.&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://dev.to/aman_singh/lazy-loading-react-components-no-rocket-science-ejn"&gt;code-splitting and lazy loading&lt;/a&gt; where possible to avoid sending unnecessary JavaScript.&lt;/li&gt;
&lt;li&gt;Don't make too many requests on your first page load.&lt;/li&gt;
&lt;li&gt;Think about &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement"&gt;progressive enhancement&lt;/a&gt; and server-side rendering.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Starting a web developer career has become more of knowing about React, VueJS, and AngularJS. The advent of these frameworks has made creating a complex UI so easy that we've forgotten about accessibility guidelines. No doubt our developer experience has improved, but we have paid the cost of forgetting about vanilla JavaScript fundamentals, HTML native elements, and accessibility.&lt;/p&gt;

&lt;p&gt;It's the time we resurrect these existing standards and lay down our app using the principles that would make it inclusive and accessible. Accessibility is not optional, and as a developer, it's our sole responsibility to make sure it won't go unattended.&lt;/p&gt;

</description>
      <category>ux</category>
      <category>react</category>
      <category>productivity</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Lazy-loading React components—no rocket science</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 30 May 2021 13:24:20 +0000</pubDate>
      <link>https://dev.to/aman_singh/lazy-loading-react-components-no-rocket-science-ejn</link>
      <guid>https://dev.to/aman_singh/lazy-loading-react-components-no-rocket-science-ejn</guid>
      <description>&lt;p&gt;Imagine you've been to a café and order a cup of coffee ☕. Once the bill 🧾 arrives, it isn't only for the coffee you ordered, but all the menu items available in the café. How would you feel? Shocking right!! It would be unfair to pay for all the items you haven't even ordered. Without arguing, you paid the bill, and never returned to this café.&lt;/p&gt;

&lt;p&gt;Well, that was just an analogy. Let's relate it with our web platform, build with an enormous JavaScript bundle. &lt;/p&gt;

&lt;p&gt;Here, our user is the customer, and we(developers) are the café owners. If our user has only requested the &lt;code&gt;registration/signup&lt;/code&gt; form, would you also send down the rest of the JavaScript bundle(the bill) responsible for the rest of the pages, carrying huge map or date libraries? How would your user feel? Most likely upset or might not come to your website again, right? &lt;/p&gt;

&lt;p&gt;The obvious reason is that their first-page load experience would be slow, and the page might take more time to be interactive (&lt;a href="https://web.dev/tti/" rel="noopener noreferrer"&gt;TTI&lt;/a&gt; &amp;amp; &lt;a href="https://web.dev/fid/" rel="noopener noreferrer"&gt;FID&lt;/a&gt;). The browser will keep itself busy parsing the JavaScript, while our users stare at a blank screen with a sad face ☹️.&lt;/p&gt;

&lt;p&gt;And the sad thing is that our poor user has no clue that it was us, the developers, who could have been more responsible while sending the full JavaScript bundle down to them in one go. &lt;/p&gt;

&lt;p&gt;Welcome to the world of code-splitting where you can lazy-load (dynamically) your JavaScript bundle dynamically, only when the user requested it. The bill you hand over to your user is exactly what they have eaten 😄.&lt;/p&gt;

&lt;h2&gt;
  
  
  Route-based splitting
&lt;/h2&gt;

&lt;p&gt;All modern JavaScript bundlers, like &lt;a href="https://webpack.js.org/guides/code-splitting/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt;. &lt;a href="https://rollupjs.org/guide/en/#code-splitting" rel="noopener noreferrer"&gt;Rollup&lt;/a&gt;, and &lt;a href=""&gt;parcel&lt;/a&gt;, supports code-splitting feature out of the box. These bundlers can create multiple bundles that can be dynamically loaded at run time, thus improving the web performance for your users. &lt;/p&gt;

&lt;p&gt;Splitting your JavaScript bundle based on the &lt;code&gt;routes/pages&lt;/code&gt; in your app is called route-based code splitting. For example, if you have &lt;code&gt;login&lt;/code&gt; and a home page, you would more likely be splitting the bundle based on these routes. And only send &lt;code&gt;login&lt;/code&gt; page JavaScript when the page loads.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;NextJS&lt;/a&gt; provides this route-based splitting feature out of the box. And if you're using &lt;a href="https://reactrouter.com/" rel="noopener noreferrer"&gt;React Router&lt;/a&gt;, &lt;a href="https://reactjs.org/docs/code-splitting.html#route-based-code-splitting" rel="noopener noreferrer"&gt;React-lazy&lt;/a&gt; is your best bet. &lt;/p&gt;

&lt;h2&gt;
  
  
  Component-based splitting
&lt;/h2&gt;

&lt;p&gt;With route-based splitting, we've made our users happy. It's time we take an extra step and implement &lt;strong&gt;component-based splitting&lt;/strong&gt;. Let's understand this with an example, followed by a coding exercise to strengthen our concepts. Before you realize it,  it will become a piece of cake for you 🍰. &lt;/p&gt;

&lt;p&gt;Imagine you're building a page to show a rental property. There's a button on this page that opens up a full-page map to show its address. This map component carries a complex functionality and has contributed to a large amount of JavaScript bundle. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./js-heavy-map-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="c1"&gt;// Property page component&lt;/span&gt;
 &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;showMap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShowMap&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Rental&lt;/span&gt; &lt;span class="nx"&gt;Property&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Property&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;showMap&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setShowMap&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="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Show&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/article&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Would you make this map &lt;em&gt;component&lt;/em&gt; part of your initial property page (route-based) bundle? What if the user never clicks the button, and only views the property metadata? Wouldn't that be a waste of resources to parse all that extra JavaScript causing the slow page load time? &lt;/p&gt;

&lt;p&gt;Yes, it would be unnecessary to send along all this heavy JavaScript bundle in this case. It might cause a heavy toll on mobile users where resources are limited compared to Desktop users. &lt;/p&gt;

&lt;p&gt;This is where the component-based loading comes into the picture and mitigates these issues. With this approach, you can &lt;strong&gt;lazy-load&lt;/strong&gt; the map component, and dynamically serve it when the user actually asks for it (click the button). This will make your &lt;em&gt;property page&lt;/em&gt; lean, improving the overall page-load performance. You can put more effort, and download the component when the user is about to hover the button, saving you an extra microsecond there.&lt;/p&gt;

&lt;p&gt;With the theory aside, we'll see how could you easily implement it in code using the dynamic import feature. We'll see two examples starting with &lt;a href="https://reactjs.org/docs/code-splitting.html#reactlazy" rel="noopener noreferrer"&gt;React.lazy&lt;/a&gt; approach, and then approach to do the same in NextJS projects using &lt;a href="https://nextjs.org/docs/advanced-features/dynamic-import#basic-usage" rel="noopener noreferrer"&gt;dynamic import&lt;/a&gt; feature. &lt;/p&gt;

&lt;p&gt;So, let's get started. &lt;/p&gt;

&lt;h3&gt;
  
  
  Lazy-loading via React.lazy
&lt;/h3&gt;

&lt;p&gt;We need to use React.lazy along with &lt;a href="https://reactjs.org/docs/react-api.html#reactsuspense" rel="noopener noreferrer"&gt;Suspense&lt;/a&gt; to lazy-load our Map component dynamically. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Change the old import to use React.lazy&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./js-heavy-map-component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Property page component&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;showMap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShowMap&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Rental&lt;/span&gt; &lt;span class="nx"&gt;Property&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Property&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Wrap you dynamic component with Suspense */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;showMap&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;}&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setShowMap&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Show&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/article&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;So, with this change, when your property page loads, the browser will not load extra JavaScript for the map component. The loading will only happen when the user hits the &lt;code&gt;Show map&lt;/code&gt; button—great saving with just a couple of lines. Didn't I say that it would your piece of cake 😉? Here's the &lt;a href="https://codesandbox.io/s/reactlazy-wv8m6" rel="noopener noreferrer"&gt;codesandbox demo&lt;/a&gt;. Download and run the app locally on your computer. Keep an eye on your &lt;code&gt;network&lt;/code&gt; tab when you hit the &lt;code&gt;Show map&lt;/code&gt; button. Here's your &lt;code&gt;lazy-loading&lt;/code&gt; in action. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkd0sirtyxisvm1qxclzq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkd0sirtyxisvm1qxclzq.gif" alt="Component based code-splitting using React lazy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Lazy-loading in NextJS
&lt;/h3&gt;

&lt;p&gt;With &lt;a href="https://nextjs.org/docs/advanced-features/dynamic-import" rel="noopener noreferrer"&gt;NextJS&lt;/a&gt;, implementing the dynamic loading is as easy as ABC. Similar to React.lazy API, NextJS has an equivalent &lt;a href="https://nextjs.org/docs/advanced-features/dynamic-import#basic-usage" rel="noopener noreferrer"&gt;dynamic&lt;/a&gt; module, which also let you pass &lt;a href="https://nextjs.org/docs/advanced-features/dynamic-import#with-custom-loading-component" rel="noopener noreferrer"&gt;additional options for loading component&lt;/a&gt;. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;dynamic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/dynamic&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Change the old import to use NextJS dynamic import&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./js-heavy-map-component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// Property page component&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;showMap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShowMap&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Rental&lt;/span&gt; &lt;span class="nx"&gt;Property&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Property&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;showMap&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;JSHeavyMapComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setShowMap&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Show&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/article&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;And here's the &lt;a href="https://codesandbox.io/s/nextjs-lazy-load-tk5y0" rel="noopener noreferrer"&gt;codesandbox demo&lt;/a&gt; for it if you want to play along.&lt;/p&gt;




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

&lt;p&gt;My main goal through this article was not only to tell you how to implement the code-splitting (the official docs are great resources for this) but to help you think about why do we need it in the first place. Remember that your beautiful app is of no good use if it takes minutes to load. User is the focus, and we should be mindful of what we send across the wire to the clients. Don't make your users pay the cost for the additional JavaScript. And with such a great set of tooling at our disposal, there's no excuse to &lt;strong&gt;not&lt;/strong&gt; do code-splitting. Your uses will thank you for this. &lt;/p&gt;

&lt;p&gt;Think inclusive, think accessible.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Curated list of Newsletters to level up your coding skills
</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 16 May 2021 04:34:04 +0000</pubDate>
      <link>https://dev.to/aman_singh/curated-list-of-newsletters-to-level-up-your-coding-skills-4heb</link>
      <guid>https://dev.to/aman_singh/curated-list-of-newsletters-to-level-up-your-coding-skills-4heb</guid>
      <description>&lt;h2&gt;
  
  
  Taking one step forward
&lt;/h2&gt;

&lt;p&gt;Learning is an unending process. The era we live in carries a plethora of resources to grab those learning opportunities—a major one is the 'Internet'. &lt;/p&gt;

&lt;p&gt;If you're working for a company, you might be learning from your peers, seniors, &amp;amp; colleagues. This is important to hone your skills, especially in software engineering where things are evolving every day. And as the metaphor goes, &lt;a href="https://en.wikipedia.org/wiki/Standing_on_the_shoulders_of_giants"&gt;Standing on the shoulders of giants&lt;/a&gt;, is completely true, and so does this quote, "&lt;em&gt;If you don't step forward, you're always in the same place&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;So, let's take one step forward, and I'd like to share a list of newsletters, which I have had been reading for a while. These newsletters are mostly focused on front-end (JavaScript, CSS, HTML, and Accessibility) topics but also cover UX, Usability, and Design. &lt;/p&gt;

&lt;p&gt;Overall great freebie packages, written by industry professionals, which get delivered to you while you sleep in the night 🛌. Let's get started.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. CSS, HTML
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://csslayout.news/"&gt;CSS Layout&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;tutorials, news, information on all things CSS Layout&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://twitter.com/rachelandrew"&gt;Rachel Andrew&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://css-weekly.com"&gt;CSS Weekly&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;CSS articles, tutorials, experiments, and tools&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://twitter.com/ZoranJambor"&gt;Zoran jambor&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://css-tricks.com/newsletter/"&gt;CSS-Tricks&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;focussed on CSS, but covers other web dev topics too&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://chriscoyier.net/"&gt;Chris&lt;/a&gt; and &lt;a href="https://css-tricks.com/about"&gt;CSS-Tricks team&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://moderncss.dev/newsletter/"&gt;Modern CSS&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;CSS tips, accessibility, updates &amp;amp; front-end resources&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://thinkdobecreate.com/"&gt;Stephanie Eckles&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://www.smashingmagazine.com/the-smashing-newsletter/"&gt;Smashing Magazine&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;Front-End, UX&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://www.smashingmagazine.com/"&gt;Smashing Magazine&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. JavaScript
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://bytes.dev/"&gt;bytes by U;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;JavaScript ecosystem&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;every Monday&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://twitter.com/tylermcginnis"&gt;Tyler McGinnis&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="http://esnextnews.com/"&gt;ES.next&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;the latest in JavaScript and cross-platform tools&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://twitter.com/rauschma"&gt;Dr. Axel Rauschmayer&lt;/a&gt; and &lt;a href="https://twitter.com/jowe"&gt;Johannes Weber&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://javascriptweekly.com/"&gt;JavaScript Weekly&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;JavaScript articles, news and cool projects&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://Cooperpress.com"&gt;Cooper Press&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://gomakethings.com/"&gt;Go Make things&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;short email focussed on vanilla JavaScript, code snippets, tools, techniques&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;each weekday&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://gomakethings.com/about/"&gt;Chris Ferdinandi&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Accessibility, UX, Design
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://a11yweekly.com/"&gt;A11y Weekly&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;web accessibility&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://davidakennedy.com/"&gt;David A. Kennedy&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://adamsilver.io/signup/"&gt;Good Design&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;Design, UI, and UX&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;monthly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://adamsilver.io/"&gt;Adam Silver&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://newsletter.uxdesign.cc/"&gt;UX Collective&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;UX, Design&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="http://twitter.com/fabriciot"&gt;Fabricio Teixeira&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://www.nngroup.com/articles/subscribe/"&gt;Neilsen Norman Group&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;Design, Usability, and UX research&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://www.nngroup.com/"&gt;Nielsen Norman Group&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. ReactJS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://reactnewsletter.com/"&gt;React Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;React news, tutorials, resources&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;every Tuesday&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://twitter.com/tylermcginnis"&gt;Tyler McGinnis&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://kentcdodds.com/subscribe/"&gt;Kentcdodds newsletter&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;tutorials and articles on React, JavaScript and Node&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://kentcdodds.com/"&gt;Kentcdodds&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. NodeJS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nodeweekly.com/"&gt;Node Weekly&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;Node.js news and article&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://Cooperpress.com"&gt;Cooper Press&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. GraphQL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.graphqlweekly.com/"&gt;GraphQL Weekly&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;news, articles and projects about GraphQL&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://www.prisma.io/"&gt;Prisma&lt;/a&gt;, &lt;a href="https://www.novvum.io/"&gt;Novvum&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Web Dev &amp;amp; Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://www.stefanjudis.com/newsletter/"&gt;Stefan's web weekly&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;web dev learnings, productivity tips, useful GitHub projects, devsheets, &amp;amp; music&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://www.stefanjudis.com/"&gt;Stefan Judis&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;a href="https://frontendfoc.us/"&gt;FrontEnd Focus&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;front-end news, articles and tutorials. HTML, CSS, WebGL, Canvas, browser tech, and more&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;weekly&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://Cooperpress.com"&gt;Cooper Press&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Career Growth
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://humanwhocodes.com/newsletter/"&gt;Human Who Codes&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;about: &lt;em&gt;intended for web application engineers who want to grow their career; covers JS, web dev, leadership, architecture, teamwork&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;frequency: &lt;em&gt;every other Tuesday of the month&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;by: &lt;em&gt;&lt;a href="https://www.twitter.com/slicknet/"&gt;Nicholas C.Zakas&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Subscribing to these newsletters is half the battle won. You need to schedule a plan to read them on regular basis, otherwise, these emails may start to feel overwhelming. In your inbox, you can also filter and label them accordingly based on the category I showed you above. And then pick one or two specific topics to read on a regular interval. It's about putting in small efforts and being consistent.&lt;/p&gt;

&lt;p&gt;If you work for a company, you can share these new updates in a brown bag, or knowledge-sharing sessions. Soon you'll realise how far have you come.&lt;/p&gt;

&lt;p&gt;Thanks for reading, and do share this list with others so that they can join you on &lt;code&gt;riding the giants&lt;/code&gt;. Good luck 🙂.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>css</category>
    </item>
    <item>
      <title>Are you sure you know how an event propagates in JavaScript?</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Wed, 17 Feb 2021 13:33:38 +0000</pubDate>
      <link>https://dev.to/aman_singh/are-you-sure-you-know-how-event-propagates-in-javascript-2ojn</link>
      <guid>https://dev.to/aman_singh/are-you-sure-you-know-how-event-propagates-in-javascript-2ojn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Events&lt;/strong&gt; are everywhere in web programming — input change, mouse move, button click, and page scroll are all forms of events. These are the actions that get generated by the system so that you can respond to them however you like by registering event listeners.&lt;br&gt;
This results in an interactive experience for the user. Understanding how the event model works in modern web browsers can help you build robust UI interactions. Get it wrong, and you have bugs crawling around.&lt;/p&gt;

&lt;p&gt;My aim through this article is to elaborate some basics around the event propagation mechanism in the W3C event model. This model is implemented by all modern browsers. &lt;/p&gt;

&lt;p&gt;Let's get started ⏰.&lt;/p&gt;


&lt;h2&gt;
  
  
  Event propagation
&lt;/h2&gt;

&lt;p&gt;Imagine If we have two HTML elements, &lt;strong&gt;element1&lt;/strong&gt; and &lt;strong&gt;element2&lt;/strong&gt;, where &lt;strong&gt;element2&lt;/strong&gt; is the child of &lt;strong&gt;element1&lt;/strong&gt; as shown in the figure below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jf68va42ebsajhg5tx4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jf68va42ebsajhg5tx4.png" alt="Nested elements with click handlers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we add click handlers to both of them like this:&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="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element2 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think will be the output when you click &lt;strong&gt;element2&lt;/strong&gt;? 🤔&lt;/p&gt;

&lt;p&gt;The answer is &lt;code&gt;element2 is clicked&lt;/code&gt;, followed by &lt;code&gt;element1 is clicked&lt;/code&gt;. This phenomenon is known as &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture" rel="noopener noreferrer"&gt;Event bubbling&lt;/a&gt;, and it's a core part of the &lt;a href="https://dom.spec.whatwg.org/#introduction-to-dom-events" rel="noopener noreferrer"&gt;W3C event model&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In event bubbling the innermost target element handles the event first, and then it bubbles up in the DOM tree looking for other ancestor elements with registered event handlers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 In event bubbling the innermost target element handles the event first and then it bubbles up in the DOM tree&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the interesting bit is that &lt;strong&gt;event flow is not uni-directional&lt;/strong&gt;, as you might have assumed. The event flow mechanism in the W3C event model is &lt;strong&gt;Bi-directional.&lt;/strong&gt; Surprise Surprise! 😯.&lt;/p&gt;

&lt;p&gt;We mostly have been dealing with event bubbling when working with frameworks like React and never think much of another phase which is &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture" rel="noopener noreferrer"&gt;Event Capturing&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Event bubbling is just one side of the coin; Event capturing is the other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the event capturing phase, the event is first captured until it reaches the target element (&lt;code&gt;event.target&lt;/code&gt;). And you, as a web developer, can register your event handler in this phase by setting &lt;code&gt;true&lt;/code&gt; as the third argument inside the &lt;code&gt;addEventListener&lt;/code&gt; 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="c1"&gt;// With addEventListener() method, you can specify the event phase by using `useCapture` parameter.&lt;/span&gt;
&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCapture&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, it's &lt;strong&gt;false&lt;/strong&gt; indicating that we are registering this event in the bubbling phase.&lt;br&gt;
Let's modify our example above to understand this better.&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="c1"&gt;// Setting "true" as the last argument to `addEventListener` will register the event handler in the capturing phase.&lt;/span&gt;
&lt;span class="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&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;// Whereas, omitting or setting "false" would register the event handler in the bubbing phase. &lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element2 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have added &lt;code&gt;true&lt;/code&gt; for &lt;code&gt;useCapture&lt;/code&gt; parameter indicating that we are registering our event handler for &lt;em&gt;element1&lt;/em&gt; in the capturing phase. For &lt;em&gt;element2&lt;/em&gt;, omitting or passing &lt;code&gt;false&lt;/code&gt; will register the event handler in the bubbling phase.&lt;/p&gt;

&lt;p&gt;Now, if you click &lt;em&gt;element2&lt;/em&gt;, you will see &lt;code&gt;element1 is clicked&lt;/code&gt; is printed first followed by &lt;code&gt;element2 is clicked&lt;/code&gt;. This is the capturing phase in action.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 In the event capturing phase, the event is first captured until it reaches the target element &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's the diagram to help you visualise this easily: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcvjpohj3sxjn7pafq94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcvjpohj3sxjn7pafq94.png" alt="Demonstrating the event flow in W3C event model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The event flow sequence is: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The "click" event starts in &lt;strong&gt;capturing phase&lt;/strong&gt;. It looks if any ancestor element of &lt;em&gt;element2&lt;/em&gt; has &lt;code&gt;onClick&lt;/code&gt; event handler for the capturing phase. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The event finds element1, and invokes the handler&lt;/strong&gt;, printing out &lt;code&gt;element1 is clicked&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The event flows down to the target element itself(&lt;em&gt;element2&lt;/em&gt;) looking for any other elements on its way. But no more event handlers for the capturing phase are found.&lt;/li&gt;
&lt;li&gt;Upon reaching &lt;em&gt;element2&lt;/em&gt;, the bubbling phase starts and executes the event handler registered on &lt;em&gt;element2&lt;/em&gt;, printing &lt;code&gt;element2 is clicked&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The event travels upward again looking for any ancestor of the target element(element2) which has an event handler for the bubbling phase. This is not the case, so nothing happens.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, the key point to remember here is that the whole event flow is the combination of the &lt;strong&gt;event capturing phase&lt;/strong&gt; followed by the &lt;strong&gt;event bubbling phase&lt;/strong&gt;. And as an author of the event handler, you can specify which phase you are registering your event handler in. 🧐&lt;/p&gt;

&lt;p&gt;With this new knowledge in our bag, it's time to look back to our first example and try to analyse why the output was in reverse order. Here's the first example again so that you're not creating a &lt;code&gt;scroll&lt;/code&gt; event 😛&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="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element2 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Omitting the &lt;code&gt;useCapture&lt;/code&gt; value registered the event handlers in the bubbling phase for both the elements. When you clicked &lt;em&gt;element2,&lt;/em&gt; the event flow sequence was like: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The "click" event starts in &lt;strong&gt;capturing phase&lt;/strong&gt;. It looks if any ancestor element of element2 has &lt;code&gt;onClick&lt;/code&gt; event handler for capturing phase and doesn't find any.&lt;/li&gt;
&lt;li&gt;The event travels down to the target element itself(element2). Upon reaching element2, the bubbling phase starts and executes the event handler registered on element2, printing &lt;code&gt;element2 is clicked&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The event travels upwards again looking for any ancestor of the target element(element2) which has an event handler for the bubbling phase. &lt;/li&gt;
&lt;li&gt;This event finds one on &lt;em&gt;element1&lt;/em&gt;. The handler is executed and &lt;code&gt;element1 is clicked&lt;/code&gt; is printed out.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Another interesting thing you can do is logging out the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/eventPhase" rel="noopener noreferrer"&gt;eventPhase&lt;/a&gt; property of the event. This helps you visualise which phase of the event is currently being evaluated.&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="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;eventPhase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eventPhase&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;Here's the &lt;a href="https://codepen.io/singharmani/pen/rNWyrPE" rel="noopener noreferrer"&gt;codepen demo&lt;/a&gt; if you like to play with it. Or you can paste the code snippet below in your browser and see it yourself.&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;element1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// element1: Registering event handler for the capturing phase&lt;/span&gt;
&lt;span class="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;"&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;// element2: Registering event handler for the bubbling phase&lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;element2 is clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// clicking the element2&lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stopping the event propagation
&lt;/h3&gt;

&lt;p&gt;If you wish to prevent further propagation of current event in any phase, you could invoke &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation" rel="noopener noreferrer"&gt;stopPropagation&lt;/a&gt; method available on the &lt;code&gt;Event&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;So, it means invoking the &lt;code&gt;event.stopPropagation()&lt;/code&gt; inside the &lt;em&gt;element1&lt;/em&gt; event handler (in capturing phase), would stop the propagation. And if even if you click &lt;em&gt;element2&lt;/em&gt; now, it won't invoke its handler. &lt;/p&gt;

&lt;p&gt;The following example demonstrates that:&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="c1"&gt;// Preventing the propagation of the current event inside the handler&lt;/span&gt;
&lt;span class="nx"&gt;element1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stopPropagation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;element1 is clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;// The event handler for the element2 will not be invoked.&lt;/span&gt;
&lt;span class="nx"&gt;element2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;element2 is clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;event.stopPropagation&lt;/code&gt; stops the propagation only. It does not, however, prevent any default behaviour from occurring. For example, clicking on links are still processed. To stop those behaviours, you can use &lt;code&gt;event.preventDefault()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Finally, here's another cool &lt;a href="https://jsbin.com/zegabiqari/edit?js,console,output" rel="noopener noreferrer"&gt;JSbin demo&lt;/a&gt; if you like to play along and see how can you stop the event propagation via &lt;code&gt;event.stopPropagation&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article was helpful and has given you some insights. Thanks for reading 😍&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful resources:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dom.spec.whatwg.org/#introduction-to-dom-events" rel="noopener noreferrer"&gt;Introduction to "DOM Events"&lt;/a&gt; - (whatwg specs)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events" rel="noopener noreferrer"&gt;Introduction to events&lt;/a&gt; - (Mozilla documentation)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jsbin.com/zegabiqari/edit?js,console,output" rel="noopener noreferrer"&gt;Event phases and stop propagation demo&lt;/a&gt; - (JSbin demo)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>html</category>
    </item>
    <item>
      <title>An Easy Explanation to Prototypal Delegation in JavaScript</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 18 Oct 2020 11:14:27 +0000</pubDate>
      <link>https://dev.to/aman_singh/an-easy-explanation-to-prototypal-delegation-in-javascript-3ok2</link>
      <guid>https://dev.to/aman_singh/an-easy-explanation-to-prototypal-delegation-in-javascript-3ok2</guid>
      <description>&lt;p&gt;JavaScript language stands on two pillars: &lt;a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a0" rel="noopener noreferrer"&gt;functional programming&lt;/a&gt; and prototypal delegation. The addition of &lt;code&gt;classes&lt;/code&gt; in JavaScript is mere a syntactic sugar to give it Object-oriented programming feel: unwrap them and you will find functions inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypal Delegation
&lt;/h2&gt;

&lt;p&gt;Prototypal delegation is all about delegating the responsibility to the prototype higher up in the [[Prototype]] chain.&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;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&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="c1"&gt;// true&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&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="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&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="c1"&gt;// true&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&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="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prototype chain will look like this: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;foo --&amp;gt; Function.prototype --&amp;gt; Object.prototype --&amp;gt; null&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple words, if you try to look for a property that is &lt;strong&gt;not&lt;/strong&gt; owned by the object, the JavaScript engine will &lt;strong&gt;traverse up&lt;/strong&gt; its prototype chain until it finds it. Let's see an example to understand 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Garlic Naan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipeBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getRecipeName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;name&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;span class="c1"&gt;// Set 'recipeBook' as the prototype of 'recipe'&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recipeBook&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Prototypal delegation in action&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRecipeName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prototype chain will look like this: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;recipe --&amp;gt; recipeBook -&amp;gt; Object.prototype --&amp;gt; null&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The object &lt;code&gt;recipe&lt;/code&gt; doesn't own the &lt;code&gt;getRecipeName&lt;/code&gt; property. But by setting &lt;code&gt;recipeBook&lt;/code&gt; as its prototype, we have delegated the job of printing the &lt;code&gt;name&lt;/code&gt; to the &lt;code&gt;recipeBook&lt;/code&gt; instance. This is called the prototypal delegation.&lt;/p&gt;

&lt;p&gt;Now, let say you also want &lt;code&gt;recipe&lt;/code&gt; to delegate the task of &lt;code&gt;orderRecipe()&lt;/code&gt; to another instance &lt;code&gt;order&lt;/code&gt;. You can do this by extending the prototype chain like:&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;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ordered!`&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;span class="c1"&gt;// Extending the prototype chain&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipeBook&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan ordered!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prototype chain will extend to this: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;recipe --&amp;gt; recipeBook --&amp;gt; order --&amp;gt; Object.prototype --&amp;gt; null&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, I think it should be easy to relate why you are able to invoke &lt;code&gt;recipe.hasOwnProperty()&lt;/code&gt; even though none of the object literals we declared owned &lt;code&gt;hasOwnProperty&lt;/code&gt;. This is because all object literals implicitly inherit from &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes" rel="noopener noreferrer"&gt;&lt;strong&gt;Object.prototype&lt;/strong&gt;&lt;/a&gt;, which means the &lt;code&gt;hasOwnProptery()&lt;/code&gt; task has been delegated to &lt;code&gt;Object.protoype&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Here's the complete code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Garlic Naan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipeBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getRecipeName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;name&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;span class="c1"&gt;// Set 'recipeBook' as the prototype of 'recipe'&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recipeBook&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ordered!`&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;span class="c1"&gt;// Extending the prototype chain&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipeBook&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Prototypal delegation in action&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRecipeName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan'&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan ordered!'&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constructor function and the new keyword
&lt;/h2&gt;

&lt;p&gt;Before I leave you with this delegation concept, I also want to talk about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function" rel="noopener noreferrer"&gt;constructor functions&lt;/a&gt; and why do you need to use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new" rel="noopener noreferrer"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/a&gt; operator when creating instances. I hope with the prototype concept aside it should be easy to demystify their existence. &lt;/p&gt;

&lt;p&gt;Every function (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#use_of_prototype_property" rel="noopener noreferrer"&gt;except fat arrow&lt;/a&gt;) in JavaScript has a property called &lt;code&gt;prototype&lt;/code&gt; which is just a plain object with &lt;code&gt;constructor&lt;/code&gt; property. This is different from the internal [[Prototype]] relationship. &lt;/p&gt;

&lt;p&gt;Let's revisit the previous &lt;code&gt;recipe&lt;/code&gt; example and see how can you establish the same prototypal relationship using the constructor function.&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="c1"&gt;// Constructor function 'Recipe'&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prototype&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;Recipe&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="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visually it will look similar to the diagram below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk4jflqxwdgkcbw0b9aei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk4jflqxwdgkcbw0b9aei.png" alt="Constructor function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The property (prototype) is special because when you invoke &lt;code&gt;Recipe()&lt;/code&gt; using the &lt;strong&gt;new&lt;/strong&gt; operator, the new operator uses &lt;code&gt;Recipe.prototype&lt;/code&gt; as a prototype for instances it creates. Once the instance is created, the new operator passes that instance as &lt;code&gt;this&lt;/code&gt; internally as one of the parameters to &lt;code&gt;Recipe()&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Garlic Naan&lt;/span&gt;&lt;span class="dl"&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;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fopgg6phne0ke7d04rd4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fopgg6phne0ke7d04rd4s.png" alt="Recipe instance delegates the task to its prototype"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, it should be clear that why do we need to add properties to &lt;code&gt;Recipe.prototype&lt;/code&gt;: they become available on all the &lt;code&gt;Recipe&lt;/code&gt; instances via prototypal delegation. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;__&lt;strong&gt;&lt;em&gt;proto&lt;/em&gt;&lt;/strong&gt;__ is a pseudo property for accessing the prototype of an object&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Adding properties to 'Recipe.prototype' will make them &lt;/span&gt;
&lt;span class="c1"&gt;// available on all `Recipe` instances. &lt;/span&gt;
&lt;span class="nx"&gt;Recipe&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;getRecipeName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;name&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;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkj6cp4gdvwwjqo3tzdy1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkj6cp4gdvwwjqo3tzdy1.png" alt="Adding properties to prototype"&gt;&lt;/a&gt;&lt;br&gt;
Similarly, we can extend the chain and delegate the task of ordering the recipe to another object by setting it as a &lt;code&gt;Recipe.prototype&lt;/code&gt; prototype.&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="c1"&gt;// Order constructor&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;Order&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;recipeOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;recipeOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ordered!`&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;span class="c1"&gt;// Setting up the delegation aka Prototypal inheritance&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Recipe&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;Order&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;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan ordered!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete code example using Function constructor looks like:&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="c1"&gt;// Constructor function 'Recipe'&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prototype&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;Recipe&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="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Recipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Garlic Naan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;Recipe&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;getName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Order constructor&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;Order&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;recipeOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;recipeOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ordered!`&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;span class="c1"&gt;// Setting up the delegation aka Prototypal inheritance&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Recipe&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;Order&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="c1"&gt;// Prototypal delegation in action&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRecipeName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan'&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderRecipe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 'Garlic Naan ordered!'&lt;/span&gt;
&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here's how the final prototype chain looks like: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa5nxevviutg3lvz5jpjb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa5nxevviutg3lvz5jpjb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing thoughts
&lt;/h2&gt;

&lt;p&gt;Prototypes in JavaScript may seem daunting to begin with, but I hope this article has eased your learning path. Understanding the foundation of JavaScript is the key to become a good developer. If you want to explore more about the prototype chain, then I highly recommend reading this &lt;a href="https://exploringjs.com/impatient-js/ch_proto-chains-classes.html#prototype-chains" rel="noopener noreferrer"&gt;chapter&lt;/a&gt; by Dr. Axel. Thank you for reading 😍.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>prototypes</category>
      <category>functional</category>
    </item>
    <item>
      <title>How to smartly cleanup your commit history using git reset?</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 16 Aug 2020 16:37:54 +0000</pubDate>
      <link>https://dev.to/aman_singh/how-to-cleanup-commit-history-using-git-reset-46p3</link>
      <guid>https://dev.to/aman_singh/how-to-cleanup-commit-history-using-git-reset-46p3</guid>
      <description>&lt;h3&gt;
  
  
  TLDR
&lt;/h3&gt;

&lt;p&gt;Running &lt;code&gt;git reset --soft &amp;lt;commit-hash&amp;gt;&lt;/code&gt; will move the branch to this old commit. And now when you run &lt;code&gt;git status&lt;/code&gt;, you will see all the changes you have made since &lt;code&gt;commit-hash&lt;/code&gt; in your staging area. You can then create a single commit to update your commit history. &lt;/p&gt;

&lt;p&gt;With &lt;code&gt;--mixed&lt;/code&gt;(also a default) flag, Git will match your staging area with the content snapshot at that commit, which means all those changes since old commit have been unstaged too. Now, you can selectively stage those files and can create multiple new commits however you want.&lt;/p&gt;




&lt;p&gt;A commit message should justify your intent for the change, and not just add unnecessary noise to the Git history. Therefore, It's a good habit to keep your commit history clean and Git has a few tools to achieve this.&lt;/p&gt;

&lt;p&gt;In this article, I will show you how you can smartly clean up your commit history using &lt;code&gt;git reset&lt;/code&gt; But before I throw in that tip, I would also like to unveil the reasoning behind it so that it can be more fruitful in a long run.&lt;/p&gt;

&lt;p&gt;If you wish to skip the basics, you can jump straight to the cleanup section too. &lt;/p&gt;

&lt;p&gt;Let's get started. &lt;/p&gt;

&lt;h3&gt;
  
  
  Pointers 👈
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;HEAD&lt;/strong&gt; and &lt;strong&gt;branch&lt;/strong&gt; are two different concepts in Git. The branch is just a lightweight movable pointer to one of the commits you are on, whereas, HEAD by default points to the branch you are currently on.&lt;/p&gt;

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

&lt;p&gt;When Git moves the HEAD, it also modifies the index and working directory to reflect the changes of that commit.&lt;/p&gt;

&lt;p&gt;Now when you run &lt;code&gt;git reset&lt;/code&gt;, it moves the branch HEAD is pointing to. For example, if you are on &lt;code&gt;master&lt;/code&gt; branch, running &lt;code&gt;git reset 8fgh5&lt;/code&gt; will move the branch to this commit as shown in the picture below: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--upacckCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fgertwtcu6uufwub7kzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--upacckCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fgertwtcu6uufwub7kzg.png" alt="git reset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;code&gt;git reset&lt;/code&gt; moves the branch HEAD is pointing to; &lt;code&gt;git checkout &amp;lt;branch-name&amp;gt;&lt;/code&gt; moves the HEAD to that branch.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Git reset has 3 flavours at your disposal: --&lt;em&gt;soft&lt;/em&gt;, --&lt;em&gt;mixed&lt;/em&gt;, and --&lt;em&gt;hard&lt;/em&gt;. Irrespective of which one you chose, running &lt;code&gt;git reset&lt;/code&gt; will always start by moving the branch HEAD points to, and stops where you ask it to. It runs the following operations in order: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move the branch HEAD point to (&lt;strong&gt;stop here if --soft&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Modify the index(staging area) to look like HEAD (&lt;strong&gt;stop here if --mixed&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Make the working directory looks like the index (&lt;strong&gt;--hard&lt;/strong&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With the foundation set aside, let's see how can you use the &lt;code&gt;--soft&lt;/code&gt; and &lt;code&gt;--mixed&lt;/code&gt;(also a default) options with &lt;code&gt;git reset&lt;/code&gt; to clean up your commit history.&lt;/p&gt;

&lt;p&gt;Time for some awesomeness 🎉.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;&lt;/a&gt; It's cleanup time ⏰
&lt;/h3&gt;

&lt;p&gt;If you have been lazily writing multiple vague commits, you can use &lt;code&gt;git reset --soft &amp;lt;old-commit&amp;gt;&lt;/code&gt; to make your branch point to that old commit. And as we learned, Git will start by moving the branch pointer to it and stops right there. &lt;strong&gt;It won't modify the index or working directory&lt;/strong&gt;. And everything you have committed so far since this commit (old-commit) will be shown in your staging area. It essentially undid all the &lt;code&gt;git commit&lt;/code&gt; commands. Running &lt;code&gt;git status&lt;/code&gt; will now show 'Changes to be committed' message.     &lt;/p&gt;

&lt;p&gt;Now, you can go ahead and commit those changes with a new message 😍. &lt;/p&gt;

&lt;p&gt;I normally use &lt;code&gt;--soft&lt;/code&gt; reset variant when I feel like squashing old commits into a single commit. But if you wish to create multiple commits starting from &lt;code&gt;old-commit&lt;/code&gt;, you can also run &lt;code&gt;git reset &amp;lt;old-commit&amp;gt;&lt;/code&gt;. Git runs this command with &lt;code&gt;--mixed&lt;/code&gt; option by default. Doing so, Git will move your branch to that commit but also &lt;strong&gt;modifies your index area to match HEAD&lt;/strong&gt;. In other words, all the changes you have committed since then will be unstaged. Think of it as the opposite of &lt;code&gt;git add&lt;/code&gt;. Running &lt;code&gt;git status&lt;/code&gt; will now show &lt;code&gt;Changes not staged for commit&lt;/code&gt; message. &lt;/p&gt;

&lt;p&gt;Now you can selectively add files to the staging area and create meaningful commits to clean up your commit history.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;HEAD~{n}&lt;/strong&gt; refers to the nth parent of your HEAD. So, &lt;strong&gt;HEAD~1&lt;/strong&gt; gives you the first parent commit of HEAD. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Closing thoughts 👋
&lt;/h3&gt;

&lt;p&gt;I've always enjoyed this proverb: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope that through this article I have not just given you a tip, but also have elaborated the rationale behind it. Now you can feel more confident using &lt;code&gt;git reset&lt;/code&gt; in your daily workflow. Strive to keep your commit history clean, and your future colleagues will thank you for this 🙏. &lt;/p&gt;




&lt;p&gt;That's for today. And if you are interested in learning another tip, here's my article about &lt;a href="https://dev.to/aman_singh/git-interactive-enhance-your-work-productivity-10x-4h2l"&gt;git interactive&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
      <category>webdev</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Git Interactive—enhance your work productivity 10X</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Fri, 31 Jul 2020 17:28:41 +0000</pubDate>
      <link>https://dev.to/aman_singh/git-interactive-enhance-your-work-productivity-10x-4h2l</link>
      <guid>https://dev.to/aman_singh/git-interactive-enhance-your-work-productivity-10x-4h2l</guid>
      <description>&lt;p&gt;&lt;strong&gt;Git&lt;/strong&gt; has a plethora of commands at your disposal. But we often end up using only a handful of those in our daily git workflow. The &lt;code&gt;git add&lt;/code&gt; and &lt;code&gt;git checkout&lt;/code&gt; are one of those indispensable commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="c1"&gt;// stage the modified content of the working directory&lt;/span&gt;
&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// for checking out to other branches&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, I have often seen devs not leveraging these commands to their full power. The aforementioned usages are often enough to get the job done. Moreover, our mind is so comfortable in using them that we don’t want to explore what rest they have to offer.  &lt;/p&gt;

&lt;p&gt;This article is my attempt to change how you think about these two commands. Welcome to the world of &lt;strong&gt;git interactive&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Interactive Staging
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;git add -p&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;This command gives you the flexibility of selecting &lt;strong&gt;granular changes&lt;/strong&gt;, and mark them accordingly to go into your &lt;strong&gt;staging area&lt;/strong&gt;. Instead of adding a complete file, You can stage only desired hunks of diffs from it and leave the rest. For e.g, If you have made two changes in your tracked file &lt;code&gt;README.md&lt;/code&gt; , you can stage one of them and not the other. &lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Add smaller commits
&lt;/h4&gt;

&lt;p&gt;We should strive to keep our commits smaller. A commit subject should be enough to justify your reason for the specified change.&lt;/p&gt;

&lt;p&gt;In those scenarios, where you have gone too far and refactored a huge bunch of code, &lt;code&gt;git add -p&lt;/code&gt; is your best bet. It helps you to add small and isolated changes and make them ready for a commit. You can re-visit and select more hunks to be part of the subsequent commits. &lt;/p&gt;

&lt;h4&gt;
  
  
  Handy while debugging and refactoring
&lt;/h4&gt;

&lt;p&gt;While diagnosing an issue, you might have sprinkled a whole bunch of console.log statements. Doing so, you might have refactored a few lines of code that you might want to keep for future commits. &lt;/p&gt;

&lt;p&gt;You can smartly use &lt;code&gt;git add -p&lt;/code&gt; to cherry-pick those useful lines of code and create a commit out of that. This will leave the log statements in your working directory, which you can safely discard later on. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Interactive checkout
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;git checkout -p&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;This command allows you to &lt;strong&gt;discard hunks&lt;/strong&gt; interactively from your working directory. In other words, it does the opposite of interactive staging. Instead of discarding all the changes in a file, you can choose which part of the file you want to remove and keep the rest 🚀. &lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Keeping your working directory clean
&lt;/h4&gt;

&lt;p&gt;If you have been brainstorming about an idea, and have quickly scribbled a few lines to code. You can invoke this command and interactivity remove irrelevant code from your working directory, leaving important bits to become part of the staging area. &lt;/p&gt;

&lt;h4&gt;
  
  
  Handy while debugging
&lt;/h4&gt;

&lt;p&gt;The same use case as we have for interactive staging, but works in reverse order. Instead of cherry-picking useful bits, you can discard the unnecessary log statements and leave the important work for future staging.&lt;/p&gt;




&lt;p&gt;This is all for now. To learn more about git tools and command, I highly recommend reading this book &lt;a href="https://git-scm.com/book/en/v2"&gt;here&lt;/a&gt;.  Now go ahead and give these commands a try. Let me know your thoughts in the comments below. &lt;/p&gt;

&lt;p&gt;Also, if you've enjoyed reading this article, please don’t forget to show some ❤️. &lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
      <category>webdev</category>
      <category>tooling</category>
    </item>
    <item>
      <title>What’s the deal with Object.prototype.hasOwnProperty.call()?</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Fri, 24 Apr 2020 16:37:13 +0000</pubDate>
      <link>https://dev.to/aman_singh/what-s-the-deal-with-object-prototype-hasownproperty-call-4mbj</link>
      <guid>https://dev.to/aman_singh/what-s-the-deal-with-object-prototype-hasownproperty-call-4mbj</guid>
      <description>&lt;p&gt;I am sure you might have seen the following line of code either while reading someone’s code or in a library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&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;hasOwnProperty&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="nx"&gt;objRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;propName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now you are wondering what on earth this code is doing. You start doubting your JavaScript skills. Don’t worry. You are at the right place.&lt;/p&gt;

&lt;p&gt;I have chosen this piece of code for a few purposes, and by demystifying this, we will understand the following things: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is Object.prototype?&lt;/li&gt;
&lt;li&gt;How an object can borrow a function without implementing it or having it in its prototype chain?&lt;/li&gt;
&lt;li&gt;Why do we access &lt;code&gt;hasOwnProperty&lt;/code&gt; on the Object's prototype and &lt;strong&gt;not&lt;/strong&gt; on the instance itself?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If this sounds intriguing to you, let’s get started. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Object.prototype
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3"&gt;&lt;strong&gt;Prototypal inheritance&lt;/strong&gt;&lt;/a&gt; is one of the main pillars of JavaScript language which allows an object to inherit methods and properties on its prototype. You can think of the prototype as a template.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Prototypal inheritance allows an object to inherit methods and properties on its prototype.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s better to understand with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aman&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;name&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 true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you see, we haven’t defined any &lt;code&gt;hasOwnProperty&lt;/code&gt; on our &lt;code&gt;obj&lt;/code&gt; object but we managed to invoke it. How is it possible? 🤔&lt;/p&gt;

&lt;p&gt;This is possible due to the &lt;strong&gt;prototypal inheritance&lt;/strong&gt; and the way &lt;strong&gt;prototype chain&lt;/strong&gt; works.  Let’s dig a bit deeper. &lt;/p&gt;

&lt;p&gt;When we created our literal object &lt;code&gt;obj&lt;/code&gt;, its &lt;em&gt;prototype&lt;/em&gt; was set to &lt;strong&gt;Object.prototype&lt;/strong&gt;. To verify we can see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPrototypeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;[[Prototype]] is an inheritance relationship between objects. In our case, it’s the relation between the &lt;em&gt;obj&lt;/em&gt; and Object’s &lt;em&gt;prototype&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;The prototype chain looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Prototype chain&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="err"&gt;—&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="err"&gt;——&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When we try to access a property on an object, the interpreter first looks for it on the object itself. If it couldn’t find the property on the object, it will traverse up until it finds the property in the chain. &lt;/p&gt;

&lt;p&gt;Thus, when we invoked &lt;code&gt;hasOwnProperty()&lt;/code&gt;, the interpreter couldn't find it on the &lt;code&gt;obj&lt;/code&gt;, so it traverses up in the chain and finds it on &lt;strong&gt;Object.prototype&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, We can set up or override the prototype chain however we want by using &lt;strong&gt;Object.setPrototypeOf()&lt;/strong&gt; method or by using &lt;strong&gt;Object.create()&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;PersonPrototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="k"&gt;return&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;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt; 

&lt;span class="c1"&gt;// Setting person's prototype &lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PersonPrototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Trying to access getName() method will cause a prototype chain lookup (aka prototype delegation) &lt;/span&gt;
&lt;span class="c1"&gt;// and finds it on PersonPrototype. &lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'peter'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Borrowing a function
&lt;/h3&gt;

&lt;p&gt;Let's imagine If I have a following function and an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Greetings &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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How would you make the object &lt;code&gt;a&lt;/code&gt; borrow &lt;code&gt;sayHello&lt;/code&gt;, and invoke the correct &lt;em&gt;name&lt;/em&gt; in the greetings? We don't want &lt;code&gt;a&lt;/code&gt; to implement &lt;code&gt;sayHello&lt;/code&gt; or have it anywhere on its prototype chain. 🤔&lt;/p&gt;

&lt;p&gt;The answer is via &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; method available on &lt;strong&gt;Function.prototype&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In JavaScript, every function we create inherits from &lt;strong&gt;Function.prototype&lt;/strong&gt;. &lt;br&gt;
And as we have just learned that via prototype chain, we can use &lt;em&gt;call&lt;/em&gt; method on all function objects. 💡&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In JavaScript, every function we create inherits from Function.prototype. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The syntax of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;call method&lt;/a&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 'call' method is available on Function.prototype&lt;/span&gt;
&lt;span class="nx"&gt;func&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="nx"&gt;objRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first argument is an object who wants to borrow this function followed by the list of arguments for that function.&lt;/p&gt;

&lt;p&gt;So, for &lt;em&gt;a&lt;/em&gt; to borrow &lt;code&gt;sayHello&lt;/code&gt;, all we have to do is use &lt;code&gt;call&lt;/code&gt; method on &lt;code&gt;sayHello&lt;/code&gt; passing &lt;em&gt;a&lt;/em&gt; as an argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sayHello&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="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Greetings peter &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h3&gt;
  
  
  3. Object.prototype.hasOwnProperty vs instance.hasOwnProperty
&lt;/h3&gt;

&lt;p&gt;After a lightweight tutorial on Prototypal inheritance and borrowing functions, finally, it's time to demystify why one would use &lt;em&gt;hasOwnProperty&lt;/em&gt; on the &lt;em&gt;Object.prototype&lt;/em&gt; and not on the object instance.&lt;/p&gt;

&lt;p&gt;As we mentioned above that we can control the prototype chain ourselves. One way is to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"&gt;Object.create()&lt;/a&gt; method while creating Object instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Object.create() accepts an argument which becomes&lt;/span&gt;
&lt;span class="c1"&gt;// the prototype for newly created object.&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&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="c1"&gt;// Setting `null` as prototype for 'a'. &lt;/span&gt;

&lt;span class="c1"&gt;// Adding a 'name' property on the instance&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Using `hasOwnProperty` method would cause an error&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//🚫 throws a TypeError: a.hasOwnProperty is not a function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Trying to invoke &lt;code&gt;hasOwnProperty&lt;/code&gt; throws an error as there's no such method available on the object or its prototype chain. The prototype chain was like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Prototype chain&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;---&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You may be wondering why someone would create an object like this. But the irony is that in JavaScript you are allowed to be as crazy as you want 🔥. &lt;/p&gt;

&lt;p&gt;Imagine you are creating a library and exposing a function that accepts an object as an argument. If your function makes use of &lt;code&gt;hasOwnProperty&lt;/code&gt; directly on the object being passed from outside, it could break your code if someone passes an object with &lt;code&gt;null&lt;/code&gt; as its prototype. &lt;/p&gt;

&lt;p&gt;Consequently, to guard this problem we can use &lt;em&gt;function borrowing&lt;/em&gt; technique we previously learned. The passed-in object argument can borrow &lt;code&gt;hasOwnProperty&lt;/code&gt; available on Object.prototype as we previously learned via &lt;code&gt;call&lt;/code&gt; method. 🚀😇.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Will not break your code if 'a' has a null prototype. ✅&lt;/span&gt;
&lt;span class="nb"&gt;Object&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;hasOwnProperty&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="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true; &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Every object literal inherits from &lt;strong&gt;Object.prototype&lt;/strong&gt;. This allows you to invoke some of the methods available like &lt;code&gt;hasOwnProperty&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We can override/create the prototype chain with the help of &lt;strong&gt;Object.setPrototypeOf&lt;/strong&gt; method and via &lt;strong&gt;Object.create(prototype)&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Every function inherits from &lt;strong&gt;Function.prototype&lt;/strong&gt; allowing you to consume methods like &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;bind&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;An object can borrow other functions without implementing them or having them in their prototype chain. This can be achieved by using &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt; method available on &lt;code&gt;Function.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Object.prototype.hasOwnProperty.call(objRef, 'propName')&lt;/code&gt; to guard the &lt;strong&gt;TypeError&lt;/strong&gt; when objRef has &lt;code&gt;null&lt;/code&gt; prototype. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all for now. I hope you have enjoyed reading this article and learned a few things. Go and share this achievement with others 😍. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>prototype</category>
      <category>webdev</category>
      <category>inheritance</category>
    </item>
    <item>
      <title>Abstract Operations—the key to understand Coercion in JavaScript</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Tue, 27 Aug 2019 14:02:40 +0000</pubDate>
      <link>https://dev.to/aman_singh/abstract-operations-the-key-to-understand-coercion-in-javascript-453i</link>
      <guid>https://dev.to/aman_singh/abstract-operations-the-key-to-understand-coercion-in-javascript-453i</guid>
      <description>&lt;p&gt;I was intrigued by one the question asked on my &lt;a href="https://dev.to/aman_singh/so-you-think-you-know-javascript-5c26"&gt;&lt;strong&gt;So you think you know JavaScript&lt;/strong&gt;&lt;/a&gt; article.&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="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// returns 0 ?? 🤔&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I admit that I didn't know the correct answer at that moment, but instead of blaming and criticising the JavaScript coercion and type system, I delved into the specs to find the definition of &lt;a href="https://www.ecma-international.org/ecma-262/#sec-addition-operator-plus" rel="noopener noreferrer"&gt;&lt;strong&gt;Addition operator&lt;/strong&gt;&lt;/a&gt;. At first, the specs didn’t make much sense to me. I found it verbose. May be my brain wasn't trained on reading the specs. Yeah, let's be honest, how many of us read the specs when there's a question about JavaScript? We have our beloved &lt;em&gt;StackOverflow&lt;/em&gt;. Right? &lt;/p&gt;

&lt;p&gt;Well, I was desperate to know the answer. I didn't want to be in the category of those devs who consider coercion as some internal magic and dangerous, to be shunned or avoided. &lt;/p&gt;

&lt;p&gt;So, this article is to share my understanding of &lt;strong&gt;coercion&lt;/strong&gt; in JavaScript, and illustrate why coercion's bad reputation is exaggerated and somewhat undeserved—to flip your perspective so you can see its usefulness and power.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Type System
&lt;/h2&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;dynamically typed&lt;/strong&gt; language where variables don't have types—&lt;strong&gt;values have types&lt;/strong&gt;. JavaScript type system doesn't enforce that the variable always holds the same initial type it starts out with.&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="c1"&gt;// variable 'a' starts out with holding a string value type. &lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

  &lt;span class="c1"&gt;// you can change the type in the next line and it's completely valid&lt;/span&gt;
  &lt;span class="c1"&gt;// Now, the variable 'a' holds the value of type number&lt;/span&gt;
  &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I always see this as one of the strongest points of the JavaScript type system. But some devs from strongly typed language may find this as a flaw in the language and object the usage of word 'type'. And I think that's one of the many reasons we are perpetually exploring the ways (&lt;a href="https://flow.org/en/" rel="noopener noreferrer"&gt;Flow&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;) to put a layer of type system on the language. In my opinion, it's like we are duck-taping JavaScript into a system which is &lt;strong&gt;not&lt;/strong&gt; in the DNA of the language. &lt;/p&gt;

&lt;p&gt;I believe we should always strive to learn the fundamentals and think alike JavaScript. Instead of flowing against it, let's flow towards it and see why the aforementioned question shouldn't be overhyped thinking that JavaScript is weird. &lt;/p&gt;

&lt;p&gt;Let's quickly revisit what we know so far about &lt;strong&gt;JavaScript types&lt;/strong&gt; and then we will deep dive into &lt;strong&gt;coercion&lt;/strong&gt; in the later sections. &lt;/p&gt;

&lt;p&gt;JavaScript has seven built-in types: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;undefined.&lt;/li&gt;
&lt;li&gt;string&lt;/li&gt;
&lt;li&gt;number&lt;/li&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;li&gt;object&lt;/li&gt;
&lt;li&gt;symbol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Except &lt;strong&gt;object&lt;/strong&gt;, all other types are called 'Primitives'. &lt;strong&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-typeof-operator" rel="noopener noreferrer"&gt;typeof&lt;/a&gt;&lt;/strong&gt; operator is a nice built-in utility to check the types. Keep in mind that &lt;em&gt;typeof&lt;/em&gt; always returns a &lt;strong&gt;string&lt;/strong&gt; type.&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="k"&gt;typeof&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;you are awesome!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// 'string'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;                 &lt;span class="c1"&gt;// 'number'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;               &lt;span class="c1"&gt;// 'boolean'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;          &lt;span class="c1"&gt;// 'undefined'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aman&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}.&lt;/span&gt;    &lt;span class="c1"&gt;// 'object'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;           &lt;span class="c1"&gt;// 'symbol'&lt;/span&gt;

&lt;span class="o"&gt;------------------------&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(){}.&lt;/span&gt;  &lt;span class="c1"&gt;// 'function'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;                 &lt;span class="c1"&gt;// 'object'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be wondering why invoking &lt;em&gt;typeof&lt;/em&gt; on &lt;strong&gt;function&lt;/strong&gt; and &lt;strong&gt;array&lt;/strong&gt; return 'function' and 'object' respectively. The reason is that &lt;em&gt;functions&lt;/em&gt; and &lt;em&gt;array&lt;/em&gt; are subtypes of the &lt;em&gt;object&lt;/em&gt; type. And because of this, you are able to add properties to the function and invoke some of the methods which an object type has—&lt;strong&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-object.prototype.tostring" rel="noopener noreferrer"&gt;toString()&lt;/a&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-object.prototype.valueof" rel="noopener noreferrer"&gt;valueOf()&lt;/a&gt;&lt;/strong&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){}&lt;/span&gt;

&lt;span class="c1"&gt;// you can add any property on foo object. &lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someProperty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am a property on foo function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// function object has 'length' property set to number of formal params it takes when declared&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="c1"&gt;// invoke 'toString()' &lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "function foo(a,b){}"&lt;/span&gt;

&lt;span class="c1"&gt;// invoke 'valueOf'&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// return this -&amp;gt; the function itself &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;: According to the &lt;a href="https://www.ecma-international.org/ecma-262/#sec-typeof-operator" rel="noopener noreferrer"&gt;&lt;em&gt;typeof&lt;/em&gt;&lt;/a&gt; spec, if an object type implements an internal &lt;a href="https://www.ecma-international.org/ecma-262/#sec-ecmascript-function-objects-call-thisargument-argumentslist" rel="noopener noreferrer"&gt;[[Call]]&lt;/a&gt; method, then &lt;em&gt;typeof&lt;/em&gt; check returns "function". Object literals and array don't implement it, but the function does. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are a few gotchas you need to be aware of with &lt;em&gt;typeof&lt;/em&gt; operator. As you may have noticed that I have excluded &lt;em&gt;typeof null&lt;/em&gt; from the list above. The reason is that &lt;strong&gt;null&lt;/strong&gt; is a special case where &lt;em&gt;typeof&lt;/em&gt; operator returns 'object'. It's the only primitive in JavaScript which is 'falsy' and returns 'object' from &lt;em&gt;typeof&lt;/em&gt; check. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;null&lt;/em&gt; is the only primitive in JavaScript which is '&lt;strong&gt;falsy&lt;/strong&gt;' and returns an ’&lt;strong&gt;object&lt;/strong&gt;' from &lt;strong&gt;typeof&lt;/strong&gt; check.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'object'; &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, how would you go about checking the &lt;em&gt;null&lt;/em&gt; type explicitly? You may need a statement like:&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// Or you can use strict equality comparison&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider one more quirk with typeof operator:&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'undefined'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'undefined'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, &lt;strong&gt;var declared&lt;/strong&gt; variables get assigned a value of &lt;strong&gt;undefined&lt;/strong&gt; when they have no current value. And that's the reason &lt;em&gt;typeof&lt;/em&gt; operator returns '&lt;strong&gt;undefined&lt;/strong&gt;'. But if you see we haven't declared the variable &lt;strong&gt;b&lt;/strong&gt; anywhere, but &lt;em&gt;typeof&lt;/em&gt; operator still manages to print 'undefined'. It's because JavaScript engine is playing safe and instead of returning some error, it returns undefined. &lt;/p&gt;

&lt;p&gt;As I said knowing these difference is like aligning your mind with JavaScript engine. Every language has some corner cases. JavaScript is not an exception. Instead of making a joke about the language, I think it's crucial to understand them so that you can take better decisions in your program. &lt;/p&gt;

&lt;p&gt;Now, let's move on to the next part of understanding coercion in JavaScript.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Coercion
&lt;/h2&gt;

&lt;p&gt;Coercion aka 'type conversion' is a mechanism of converting one type to another. In statically (strongly) typed language this process happens at compile time whereas coercion is a run-time conversion for dynamically typed languages. &lt;/p&gt;

&lt;p&gt;In JavaScript, we can have two types of coercion: "implicit" and "explicit". As the name implies, &lt;strong&gt;implicit&lt;/strong&gt; coercion is the one which happens as a &lt;strong&gt;less obvious&lt;/strong&gt; side effect of some intentional operation. On the contrary, the &lt;strong&gt;explicit&lt;/strong&gt; conversion is obvious from the code that it is occurring intentionally.&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;programmer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// implicit coercion&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`you owe me &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; dollars`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="c1"&gt;// implicit coercion&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// explicit coercion&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;// explicit coercion &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Have you ever wonder how coercion works internally? That's where things get interesting. But before we can explore the internal procedures, we need to understand some of the operations which are defined in ECMAScript 2020 section 7 called &lt;strong&gt;&lt;a href="https://tc39.es/ecma262/#sec-abstract-operations" rel="noopener noreferrer"&gt;Abstract operation&lt;/a&gt;&lt;/strong&gt;. These operations are not part of the language but are used to aid the specification of the semantics of JavaScript language. You can think of these operations as conceptual operations. &lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract Operations
&lt;/h3&gt;

&lt;p&gt;Every time a value conversion happens, it is handled by one or more abstract operation with some rules defined in the spec. Here we will look into three abstract operations: &lt;strong&gt;ToString&lt;/strong&gt;, &lt;strong&gt;ToNumber&lt;/strong&gt; and &lt;strong&gt;ToPrimitive&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Abstract operations are used to aid the specification of the semantics of JavaScript language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  ToString
&lt;/h4&gt;

&lt;p&gt;Whenever we coerce a non-string value to a string value, &lt;strong&gt;ToString&lt;/strong&gt; handles the conversion as in &lt;a href="https://tc39.es/ecma262/#sec-tostring" rel="noopener noreferrer"&gt;section 7.1.12&lt;/a&gt; of the specification. Primitive types have natural stringification. The table looks like:&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="c1"&gt;// ToString abstract operation (string conversion)&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;       &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;           &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;false&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;52&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;              &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;52&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For regular &lt;em&gt;object&lt;/em&gt; and &lt;em&gt;array&lt;/em&gt;, the default &lt;em&gt;toString()&lt;/em&gt; is invoked which is defined on the &lt;a href="https://www.ecma-international.org/ecma-262/#sec-object.prototype.tostring" rel="noopener noreferrer"&gt;Object.prototype&lt;/a&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "[object Object]"&lt;/span&gt;

&lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also specify your own &lt;em&gt;toString&lt;/em&gt; method to override the default return value:&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I love JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 

&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "I love JavaScript"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ToNumber
&lt;/h4&gt;

&lt;p&gt;Whenever a non-number value is supplied in an operation where a number was expected, such as a mathematical operation, ES2020 defines a &lt;strong&gt;ToNumber&lt;/strong&gt; abstract operation in &lt;a href="https://tc39.es/ecma262/#sec-tonumber" rel="noopener noreferrer"&gt;section 7.1.3&lt;/a&gt;. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ToNumber abstract operation (number conversion)&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;           &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;          &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;      &lt;span class="nc"&gt;NaN &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;valid&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;           &lt;span class="mi"&gt;0&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;em&gt;object&lt;/em&gt; and &lt;em&gt;array&lt;/em&gt;, values are first converted to their &lt;strong&gt;primitive value&lt;/strong&gt; equivalent (via ToPrimitive operation) and the resulting value is then coerced into number according to the &lt;strong&gt;ToNumber&lt;/strong&gt; abstract operation. &lt;/p&gt;

&lt;h4&gt;
  
  
  ToBoolean
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;ToBoolean&lt;/strong&gt; is a little simpler than &lt;strong&gt;ToString&lt;/strong&gt; and &lt;strong&gt;ToNumber&lt;/strong&gt; operation as it doesn't do any internal conversion. It only performs a table lookup as mentioned in &lt;a href="https://tc39.es/ecma262/#sec-toboolean" rel="noopener noreferrer"&gt;section 7.1.2&lt;/a&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Argument type&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;false&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;false&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;td&gt;return &lt;strong&gt;argument&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;number&lt;/td&gt;
&lt;td&gt;if argument is +0, -0 or NaN, return &lt;strong&gt;false&lt;/strong&gt;; otherwise &lt;strong&gt;true&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;td&gt;if argument is empty string, return &lt;strong&gt;false&lt;/strong&gt;; otherwise &lt;strong&gt;true&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;symbol&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;object&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  ToPrimitive
&lt;/h4&gt;

&lt;p&gt;If we have non-primitive type (like function, object, array) and we need a primitive equivalent, ES2020 defines &lt;strong&gt;ToPrimitive&lt;/strong&gt; in &lt;a href="https://tc39.es/ecma262/#sec-toprimitive" rel="noopener noreferrer"&gt;section 7.1.1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ToPrimitve&lt;/strong&gt; operation takes two arguments: input and &lt;strong&gt;hint&lt;/strong&gt;(optional). If you are performing a numeric operation, the hint will be a 'number' type. And for string operation (like concatenation), the hint passed will be a string. Note that ToPrimitive is a recursive operation which means that if the result of invoking ToPrimitive is not a primitive, it will invoke again until we can get a primitive value or an error in some cases. &lt;/p&gt;

&lt;p&gt;Now let's look at the algorithm behind the ToPrimitive operations. &lt;/p&gt;

&lt;p&gt;Every non-primitive can have two methods available: &lt;strong&gt;toString&lt;/strong&gt; and &lt;strong&gt;valueOf&lt;/strong&gt;. If 'number' hint is sent, &lt;strong&gt;valueOf()&lt;/strong&gt; method is invoked first. And if we get a &lt;strong&gt;primitive type&lt;/strong&gt; from the result then we are done. But if the result is again a non-primitive, &lt;strong&gt;toString()&lt;/strong&gt; gets invoked. Similarly, in the case of 'string' hint type, the order of these operations is reversed. If the invocation of these two operations doesn't return a primitive, generally it's a &lt;strong&gt;TypeError&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Visually, The order can be seen as follows:&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="c1"&gt;// ToPrimitive Abstract Operation&lt;/span&gt;

&lt;span class="c1"&gt;// hint: "number" &lt;/span&gt;
&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// hint: "string"&lt;/span&gt;
&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make it more clear here's the flow chart diagram of the algorithm we discussed above: &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fskc2q8wj2t4zb6hovu5r.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fskc2q8wj2t4zb6hovu5r.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now armed with this new knowledge of abstract operations, it’s the time to answer a few questions confidently. &lt;/p&gt;

&lt;h2&gt;
  
  
  Testing our knowledge
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Why the following expression produces '5' as a result? &lt;/span&gt;
&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// '5'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As per the specification in section, the &lt;a href="https://www.ecma-international.org/ecma-262/#sec-addition-operator-plus" rel="noopener noreferrer"&gt;addition operator&lt;/a&gt;  ‘+’ performs string concatenation or numeric addition based on the argument type. If either of the argument is string, it will perform string concatenation. It's called &lt;a href="https://en.wikipedia.org/wiki/Operator_overloading" rel="noopener noreferrer"&gt;operator overloading&lt;/a&gt;. Now let see how did we end up getting the string &lt;strong&gt;”5”&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;We were expecting a &lt;strong&gt;primitive type&lt;/strong&gt; but end up getting an &lt;em&gt;array&lt;/em&gt; as one of the argument. Consequently, &lt;strong&gt;ToPrimitive&lt;/strong&gt; abstract operation is performed with &lt;strong&gt;"number"&lt;/strong&gt; passed as a hint. Referring to the ToPrimitive diagram above, we can assert the following steps will take place to get the result. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[].&lt;strong&gt;valueOf()&lt;/strong&gt; // returns []; &lt;/li&gt;
&lt;li&gt;As, [] is &lt;strong&gt;not&lt;/strong&gt; a primitive,  engine will invoke [].&lt;strong&gt;toString()&lt;/strong&gt; resulting in an &lt;strong&gt;empty&lt;/strong&gt; string. &lt;/li&gt;
&lt;li&gt;Now the expression reduces to &lt;strong&gt;"" + 5&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;As we mentioned that &lt;em&gt;addition operator performs string concatenation&lt;/em&gt; when either of &lt;strong&gt;argument&lt;/strong&gt; is a &lt;strong&gt;string type&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;So, 5 will be implicitly coerced to &lt;strong&gt;“5”&lt;/strong&gt; via &lt;strong&gt;ToString&lt;/strong&gt; abstract operation passing &lt;strong&gt;'string'&lt;/strong&gt; as a hint. &lt;/li&gt;
&lt;li&gt;Finally the expression reduces to &lt;strong&gt;"" + "5"&lt;/strong&gt; resulting in value &lt;strong&gt;"5"&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// ToPrimitive is invoked on []&lt;/span&gt;
&lt;span class="c1"&gt;// "" + 5; &lt;/span&gt;
&lt;span class="c1"&gt;// "" + "5"; &lt;/span&gt;
&lt;span class="c1"&gt;// "5"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, that's a moment of inner satisfaction. Isn't it? I don't know about you but when I figured this out, I was delighted💡😀.  &lt;/p&gt;

&lt;p&gt;Before we wrap up, let's quickly demystify some of the following expression to strengthen our grip. I am going to reduce the expression from top to bottom (via abstract operations) to reach the result.&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="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;            &lt;span class="c1"&gt;// ToPrimitive is invoked on both operands&lt;/span&gt;
&lt;span class="c1"&gt;// "" + "";&lt;/span&gt;
&lt;span class="dl"&gt;""&lt;/span&gt; 
&lt;span class="o"&gt;-----&lt;/span&gt;
&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;              &lt;span class="c1"&gt;// ToPrimitive is invoked on both operands&lt;/span&gt;
&lt;span class="c1"&gt;// "" + "[object Object]";&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[object Object]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="o"&gt;-----&lt;/span&gt;
&lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="o"&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;// There's no operator overloading for subtract operator. &lt;/span&gt;
&lt;span class="c1"&gt;//ToNumber is invoked on both the operands (already primitive)&lt;/span&gt;
&lt;span class="c1"&gt;// 0 - 1; &lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="o"&gt;------&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// (1 &amp;lt; 2) &amp;lt; 3;      &lt;/span&gt;
&lt;span class="c1"&gt;// true &amp;lt; 3;              // ToNumber is invoked on true -&amp;gt; 1&lt;/span&gt;
&lt;span class="c1"&gt;// 1 &amp;lt; 3;&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="o"&gt;------&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true ooops! &lt;/span&gt;
&lt;span class="c1"&gt;// (3 &amp;lt; 2) &amp;lt; 1; &lt;/span&gt;
&lt;span class="c1"&gt;// false &amp;lt; 1;             // ToNumber is invoked on false -&amp;gt; 0&lt;/span&gt;
&lt;span class="c1"&gt;// 0 &amp;lt; 1; &lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now is the right time to answer the question which basically led me to write this article.&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="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// 0 🤔??&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here '{}' is &lt;strong&gt;not an empty object&lt;/strong&gt; but just an empty block {}. So, JavaScript engine ignores it and left with &lt;code&gt;+ []&lt;/code&gt; statement to execute.  It’s a &lt;em&gt;numeric operation&lt;/em&gt; and hence a &lt;em&gt;‘number’&lt;/em&gt; hint will be passed to convert this empty array into a &lt;em&gt;primitive&lt;/em&gt; value, which is an &lt;em&gt;empty string&lt;/em&gt;.  Finally, the empty string is coerced again via &lt;strong&gt;ToNumber&lt;/strong&gt; operation leading to a value of &lt;strong&gt;0&lt;/strong&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="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;                 &lt;span class="c1"&gt;// empty block is ignored&lt;/span&gt;
&lt;span class="c1"&gt;// + [];&lt;/span&gt;
&lt;span class="c1"&gt;// + '';&lt;/span&gt;
&lt;span class="c1"&gt;// + 0 ;&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is a dynamically typed language where &lt;strong&gt;values have type&lt;/strong&gt;—not the variables. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coercion&lt;/strong&gt; aka “type conversion”  is a procedure of converting one value type to another; it happens at compile time for JavaScript. &lt;/li&gt;
&lt;li&gt;Coercion can be of two types: &lt;strong&gt;implicit&lt;/strong&gt; and &lt;strong&gt;explicit&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abstract operations&lt;/strong&gt; are the keys to understanding coercion. They are not actual operation in the language but are used to aid the specification of the semantics of JavaScript language. &lt;/li&gt;
&lt;li&gt;Whenever we receive a non-primitive value for an operation where a primitive type was expected, &lt;strong&gt;ToPrimitive&lt;/strong&gt; abstract operation is invoked. &lt;/li&gt;
&lt;li&gt;For any non-primitive, ToPrimitive invokes two methods: &lt;strong&gt;valueOf()&lt;/strong&gt; and &lt;strong&gt;toString()&lt;/strong&gt;. Depending upon the &lt;strong&gt;hint&lt;/strong&gt; passed, &lt;strong&gt;valueOf()&lt;/strong&gt; followed by &lt;strong&gt;toString()&lt;/strong&gt; is invoked for the ‘number’ hint , and vice versa for “string”. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Dynamic nature of JavaScript is one of its core features. Understanding how coercion works internally can help us write robust code. Every language has some quirks and it’s our responsibility as a developer to be mindful of these caveats. Instead of finding flaws, we need to strive for learning the semantics of the language and work towards it.   &lt;/p&gt;

&lt;p&gt;Hope you liked the article and if that’s a &lt;em&gt;boolean&lt;/em&gt; &lt;strong&gt;true&lt;/strong&gt;, a few ❤️ will make me smile 😍.  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Why do we need to bind methods inside our class component’s constructor?</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Mon, 19 Aug 2019 09:30:09 +0000</pubDate>
      <link>https://dev.to/aman_singh/why-do-we-need-to-bind-methods-inside-our-class-component-s-constructor-45bn</link>
      <guid>https://dev.to/aman_singh/why-do-we-need-to-bind-methods-inside-our-class-component-s-constructor-45bn</guid>
      <description>&lt;p&gt;If we go back into the time where the &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;hooks&lt;/a&gt; were not yet born, we will inevitably encounter this code while creating a React class &lt;a href="https://reactjs.org/docs/components-and-props.html#function-and-class-components"&gt;component&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyClassComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Assuming that you need to pass this as a prop to one of a child component&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;someMethod&lt;/span&gt; &lt;span class="o"&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;someMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="c1"&gt;// But why? 🤔&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;This is because whenever inside a class component when we need to pass a function as props to the child component, we have to do one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bind it inside the constructor function.&lt;/li&gt;
&lt;li&gt;Bind it inline (which can have some performance issues).&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;&lt;em&gt;arrow function&lt;/em&gt;&lt;/a&gt; (which is the same as &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-class-properties"&gt;&lt;em&gt;property initializer&lt;/em&gt;&lt;/a&gt; syntax).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you ever wondered why it has to be this way? Why we have to do this extra piece of work?&lt;/p&gt;

&lt;p&gt;Through this article, I will first try to explain the binding inside the constructor function. Once we acquire that knowledge, we will try to answer why arrow functions don’t follow the same ceremony.&lt;/p&gt;

&lt;p&gt;One thing we need to know that binding in the constructor has nothing to do with React. It’s purely related to how JavaScript implements &lt;strong&gt;this&lt;/strong&gt;. Let’s look at the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="p"&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="k"&gt;return&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;x&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;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 90&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;xGetter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;xGetter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 10;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When we initialised &lt;em&gt;x&lt;/em&gt; into a global scope, it becomes the property of the &lt;em&gt;window&lt;/em&gt; object (assuming that it’s a browser environment and not a strict mode). We can assert that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;this&lt;/strong&gt; will always point to the object onto which the method was invoked. So, in the case of &lt;em&gt;foo.getX()&lt;/em&gt;, &lt;strong&gt;this&lt;/strong&gt; points to &lt;em&gt;foo&lt;/em&gt; object returning us the value of 90. Whereas in the case of &lt;em&gt;xGetter()&lt;/em&gt;, &lt;strong&gt;this&lt;/strong&gt; points to &lt;em&gt;window&lt;/em&gt; object returning us the value of 10.&lt;/p&gt;

&lt;p&gt;To retrieve the value of &lt;em&gt;foo.x&lt;/em&gt;, we can create a new function by binding the value of &lt;strong&gt;this&lt;/strong&gt; to &lt;em&gt;foo&lt;/em&gt; object using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind"&gt;Function.prototype.bind&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;getFooX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;getFooX&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints 90&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Armed with this knowledge, let’s try to understand what happens when you pass a function prop into the child component.&lt;/p&gt;

&lt;p&gt;In the following code example, we have created a dummy &lt;em&gt;class&lt;/em&gt; component to mimic React Component mental model. Inside the &lt;em&gt;render&lt;/em&gt; function, we are returning a plain JS object which has a functional prop called &lt;em&gt;'onClick'&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://reactjs.org/blog/2015/12/18/react-components-elements-and-instances.html#elements-describe-the-tree"&gt;React element&lt;/a&gt; is just an immutable description object with&lt;br&gt;
two fields: type: (string | ReactClass) and props: Object&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;setState&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;state&lt;/span&gt;&lt;span class="dl"&gt;'&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;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&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;setState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// return a child component.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// pass functional props&lt;/span&gt;
        &lt;span class="na"&gt;onClick&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Click Me&lt;/span&gt;&lt;span class="dl"&gt;'&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 1. creating a component instance&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;componentInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 2. calling a render method on the instance&lt;/span&gt;
&lt;span class="c1"&gt;// ( In reality, React does the same thing for your class components)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;componentInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 3. calling the onClick function, which was passed as a  prop,&lt;/span&gt;
&lt;span class="c1"&gt;// will throw a 'TypeError: this.setState is not a function'.&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This TypeError is obvious now because this is pointing to the &lt;em&gt;props&lt;/em&gt; object which doesn’t know the existence of any setState function. The &lt;em&gt;setState&lt;/em&gt; function is only a property of &lt;em&gt;componentInstance&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, to fix this problem, we have to bind the handleClick function inside the constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// inside constructor function&lt;/span&gt;
&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// bind returns a new function&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;handleClick&lt;/span&gt; &lt;span class="o"&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="c1"&gt;// calling onClick will print 'state' this time.&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, the value of &lt;strong&gt;this&lt;/strong&gt; will always point to &lt;em&gt;componentInstance&lt;/em&gt; which has &lt;em&gt;setState&lt;/em&gt; as one of its property and it will not throw any TypeError.&lt;/p&gt;




&lt;p&gt;Now, that was the answer to our first question. It’s good progress so far. Moving forward, we shall try to find out the answer to our second question.&lt;/p&gt;

&lt;p&gt;Looking at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;someMethod&lt;/span&gt;&lt;span class="p"&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// print {someMethod: f}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints global 'window' object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;shows that arrow functions have no this of their own. It is always determined by the scope surrounding the arrow function when it was created.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Arrow functions have no &lt;em&gt;this&lt;/em&gt; of their own.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we use an &lt;em&gt;arrow&lt;/em&gt; function inside our class (using &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-class-properties"&gt;property initializer&lt;/a&gt; feature), it becomes the method property of the instance. As &lt;strong&gt;this&lt;/strong&gt; will always be determined by the outer scope, it will point to the &lt;em&gt;instance&lt;/em&gt; of the class. Let’s see that in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;setState&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;state&lt;/span&gt;&lt;span class="dl"&gt;'&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;span class="c1"&gt;// using fat arrow function; no binding require inside constructor&lt;/span&gt;
  &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// this will now point to the instance of Component class which knows about the setState method property&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;setState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// return a child component.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// pass functional props&lt;/span&gt;
        &lt;span class="na"&gt;onClick&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Click Me&lt;/span&gt;&lt;span class="dl"&gt;'&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;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// 1. creating a component instance&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;componenttInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//  2. calling a render method on the instance ( In reality, React does the same thing for your class components)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;componenttInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 3. calling onClick will now print 'state' to the console.&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I hope you have enjoyed reading this article and now will be able to answer the question confidently. I highly recommend reading this &lt;a href="http://2ality.com/2017/12/alternate-this.html"&gt;article&lt;/a&gt; by &lt;a href="https://twitter.com/rauschma"&gt;Dr. Axel Rauschmayer&lt;/a&gt; for a more detailed description of how &lt;code&gt;this&lt;/code&gt; works.&lt;/p&gt;

&lt;p&gt;Thank you for reading, and if you liked the article a few ❤️ will definitely make me smile 😍.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to write 90% cleaner code with Hooks 🎣</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 28 Jul 2019 11:16:01 +0000</pubDate>
      <link>https://dev.to/aman_singh/how-to-write-90-cleaner-code-with-hooks-1mmj</link>
      <guid>https://dev.to/aman_singh/how-to-write-90-cleaner-code-with-hooks-1mmj</guid>
      <description>&lt;p&gt;The year 2018 brought a lot of new features into the React Ecosystem. The addition of these features is helping developers to focus more on user experience rather than spending time writing code logic.&lt;/p&gt;

&lt;p&gt;It seems that React is investing more towards a functional programming paradigm, in a search of great tools for building a UI which is more robust and scalable.&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://conf.reactjs.org/" rel="noopener noreferrer"&gt;ReactConf&lt;/a&gt; October 2018, React announced a proposal API called Hooks which took the community by storm. Developers started exploring and doing experiments with them and it received great feedback in the &lt;a href="https://github.com/reactjs/rfcs/pull/68" rel="noopener noreferrer"&gt;RFC&lt;/a&gt; (Requests for comments). React 16.8.0 is the first release to support Hooks 🎉.&lt;/p&gt;

&lt;p&gt;This article is my attempt to explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Why hooks were introduced&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How can we prepare ourselves for this API&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How can we write 90% cleaner code by using React Hooks 🎣&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you just want to have a feel of this new API first, I have created a &lt;a href="https://codesandbox.io/s/6ymrnqn43w" rel="noopener noreferrer"&gt;demo&lt;/a&gt; to play with. Otherwise, let’s get started by looking at 3 major problems we are facing as of now:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Reusing Code Logic
&lt;/h2&gt;

&lt;p&gt;You all know that reusing code logic is hard and it requires a fair bit of experience to get your head around. When I started learning React about two years ago, I used to create class components to encapsulate all my logic. And when it comes to sharing the logic across different components I would simply create a similarly looking component which would render a different UI. But that was not good. I was violating the &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY&lt;/a&gt; principle and ideally was not reusing the logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;Slowly, I learned about &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;HOC&lt;/a&gt; pattern which allowed me to use functional programming for reusing my code logic. HOC is nothing but a simple &lt;a href="https://en.wikipedia.org/wiki/Higher-order_function" rel="noopener noreferrer"&gt;higher order function&lt;/a&gt; which takes another component(dumb) and returns a new enhanced component. This enhanced component will encapsulate your logic.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A higher order function is a function that takes a function as an argument, or returns a function.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;HOC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*
     Encapsulate your logic here...
   */&lt;/span&gt;

    &lt;span class="c1"&gt;// render the UI using Wrapped Component&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WrappedComponent&lt;/span&gt; &lt;span class="si"&gt;{&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;props&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="si"&gt;{&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;state&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// You have to statically create your&lt;/span&gt;
  &lt;span class="c1"&gt;// new Enchanced component before using it&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HOC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someDumbComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// And then use it as Normal component&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we moved into the trend of passing a function as props which marks the rise of the &lt;a href="https://reactjs.org/docs/render-props.html" rel="noopener noreferrer"&gt;render props&lt;/a&gt; pattern. Render prop is a powerful pattern where &lt;em&gt;“rendering controller”&lt;/em&gt; is in your hands. This facilitates the &lt;a href="https://en.wikipedia.org/wiki/Inversion_of_control" rel="noopener noreferrer"&gt;inversion of control(IoC)&lt;/a&gt; design principle. The React documentation describes it as a technique for sharing code between components using a &lt;strong&gt;prop&lt;/strong&gt; whose value is a &lt;strong&gt;function&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A component with a render prop takes a function that returns a&lt;br&gt;
&lt;a href="https://reactjs.org/blog/2015/12/18/react-components-elements-and-instances.html#elements-describe-the-tree" rel="noopener noreferrer"&gt;React element&lt;/a&gt; and calls it instead of implementing its own render logic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple words, you create a &lt;strong&gt;class&lt;/strong&gt; component to encapsulate your logic (side effects) and when it comes to rendering, this component simply calls your function by passing only the data which is required to render the UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RenderProps&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cm"&gt;/*
  Encapsulate your logic here...
*/&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// call the functional props by passing the data required to render UI&lt;/span&gt;
    &lt;span class="k"&gt;return&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&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;state&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;span class="c1"&gt;// Use it to draw whatever UI you want. Control is in your hand (IoC)&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RenderProps&lt;/span&gt; &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SomeUI&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though both of these patterns were resolving the reusing code logic issues, they left us with a wrapper hell problem as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnhk5gnheuwv6h3glopcx.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnhk5gnheuwv6h3glopcx.png" title="wrapper hell" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, to sum it up we can see there are a few problems associated with reusing code logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not very intuitive to implement&lt;/li&gt;
&lt;li&gt;Lots of code&lt;/li&gt;
&lt;li&gt;Wrapper hell&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Giant Components
&lt;/h2&gt;

&lt;p&gt;Components are the basic unit of code reuse in React. When we have to abstract more than one behaviour into our class component, it tends to grow in size and becomes hard to maintain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A class should have one and only one reason to change,&lt;br&gt;
meaning that a class should have only one job.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By looking at the code example below we can deduce the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GiantComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;//side effects&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;startTimer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// more ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentdidUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="c1"&gt;// extra logic here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentWillUnmount&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// clear all the side effects&lt;/span&gt;
    &lt;span class="nf"&gt;clearInterval&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;timerId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UI&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Code is spread across different life cycle hooks&lt;/li&gt;
&lt;li&gt;No single responsibility&lt;/li&gt;
&lt;li&gt;Hard to test&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Classes are Hard for Humans and Machines
&lt;/h2&gt;

&lt;p&gt;Looking at the human side of the problem, we all once tripped trying to call a function inside a child component and it says:&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="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="nx"&gt;read&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;setState&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then scratched our head trying to figure out the cause: that you have forgotten to bind it in the constructor. So, &lt;em&gt;this&lt;/em&gt; remains the topic of confusion even among some experienced developers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;this&lt;/strong&gt; gets the value of object who invokes the function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, you need to write lots of boilerplate code to even start implementing the first side effect:&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;extends&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;componentDidMount&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;componentWillUnmount&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Classes are also hard for machines for the following reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minified version won’t minify method names&lt;/li&gt;
&lt;li&gt;Unused methods won’t get stripped out&lt;/li&gt;
&lt;li&gt;Difficult with hot reloading and compiler optimisation&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;All the three problems we discussed above are not three distinct problems but these are symptoms of one single problem and that is React has &lt;strong&gt;no stateful primitive&lt;/strong&gt; simpler than class component.&lt;/p&gt;




&lt;p&gt;With the advent of the new React Hooks proposal API, we can solve this problem by abstracting our logic completely outside of our component. In fewer words, you can hook a stateful logic into the functional component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React Hooks allow you to use state and other React features without writing a class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s see that in the code example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MouseTracker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// useState accepts initial state and you can use multiple useState call&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      mouseX: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;, mouseY: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A call to &lt;a href="https://reactjs.org/docs/hooks-state.html" rel="noopener noreferrer"&gt;useState&lt;/a&gt; hook returns a pair of values: the current state and a function that updates it. In our case, the current state value is &lt;em&gt;mouseX&lt;/em&gt; and setter function is &lt;em&gt;setMouseX&lt;/em&gt;. If you pass an argument to useState, that becomes the initial state of your component.&lt;/p&gt;

&lt;p&gt;Now, the question is where do we call setMouseX. Calling it below the useState hook will cause an error. It will be the same as calling &lt;em&gt;this.setState&lt;/em&gt; inside &lt;em&gt;render&lt;/em&gt; function of class components.&lt;/p&gt;

&lt;p&gt;So, the answer is that React also provides a placeholder hook called &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;useEffect&lt;/a&gt; for performing all side effects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MouseTracker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// useState accepts initial state and you can use multiple useState call&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setMouseX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setMouseY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// side effect&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mousemove&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Every effect may return a function that cleans up after it&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mousemove&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&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;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      mouseX: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;, mouseY: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This effect will be called both after the first render and after every update. You can also return an optional function which becomes a &lt;em&gt;cleanup&lt;/em&gt; mechanism. This lets us keep the logic for adding and removing subscriptions close to each other.&lt;/p&gt;

&lt;p&gt;The second argument to useEffect call is an optional array . Your effect will only re-run when the element value inside the array changes. Think of this as how &lt;a href="https://reactjs.org/docs/react-component.html#shouldcomponentupdate" rel="noopener noreferrer"&gt;shouldComponentUpdate&lt;/a&gt; works. If you want to run an effect and clean it up only once (on mount and unmount), you can pass an &lt;strong&gt;empty array&lt;/strong&gt; ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run. This is close to the familiar mental model of &lt;em&gt;componentDidMount&lt;/em&gt; and &lt;em&gt;componentWillUnmount&lt;/em&gt;. If you want a deep dive into &lt;em&gt;useEffect&lt;/em&gt; hook, I've written another article &lt;a href="https://dev.to/aman_singh/why-effects-shouldn-t-lie-about-their-dependencies-1645"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But isn’t our &lt;em&gt;MouseTracker&lt;/em&gt; component still holding the logic inside? What if another component wants to share &lt;em&gt;mousemove&lt;/em&gt; behaviour as well? Also, adding one more effect (e.g window resize) would make it little hard to manage and we are back to the same problem as we saw in class components.&lt;/p&gt;

&lt;p&gt;Now, the real magic is you can create your custom hooks outside of your function component. It is similar to keeping the logic abstracted to a separate module and share it across different components. Let see that in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// you can write your custom hooks in this file&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useMouseLocation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMouseY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setMouseX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setMouseY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mousemove&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mousemove&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mouseY&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;And now we can clean up our MouseTracker component code (90%) to a newer version as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMouseLocation&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customHooks.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MouseTracker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// using our custom hook&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMouseLocation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      mouseX: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseX&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;, mouseY: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mouseY&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s kind of a “Eureka” moment! Isn’t it?&lt;/p&gt;

&lt;p&gt;But before settling down and singing praises of React Hooks, let’s see what rules we should be aware of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules of Hooks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Only call hooks at the top level&lt;/li&gt;
&lt;li&gt;Can’t use hooks inside a class component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Explaining these rules are beyond the scope of this article. If you’re curious, I would recommend reading React &lt;a href="https://reactjs.org/docs/hooks-rules.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; and this &lt;a href="https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e" rel="noopener noreferrer"&gt;article&lt;/a&gt; by Rudi Yardley.&lt;/p&gt;

&lt;p&gt;React also has released an ESLint plugin called &lt;a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" rel="noopener noreferrer"&gt;eslint-plugin-react-hooks&lt;/a&gt; that enforces these two rules. You can add this to your project by running:&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="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; 
&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;eslint&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;plugin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;hooks&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;yarn&lt;/span&gt; 
&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;eslint&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;plugin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;hooks&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This article was part of my &lt;a href="https://twitter.com/reactsydney/status/1069498781324914689?s=19" rel="noopener noreferrer"&gt;talk&lt;/a&gt; at the ReactSydney meetup December 2018. I hope this article has intrigued you to give React hooks a try. I am super excited about the React &lt;a href="https://reactjs.org/blog/2018/11/27/react-16-roadmap.html" rel="noopener noreferrer"&gt;roadmap&lt;/a&gt; which looks very promising and has the potential to change the way we use React currently.&lt;/p&gt;

&lt;p&gt;You can find the source code and demo at this &lt;a href="https://codesandbox.io/s/6ymrnqn43w" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you liked the article, a few ❤️ will definitely make me smile 😀. There's more to come.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Hidden gems of debugging 💎 </title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Tue, 23 Jul 2019 07:49:18 +0000</pubDate>
      <link>https://dev.to/aman_singh/hidden-gems-of-debugging-1f11</link>
      <guid>https://dev.to/aman_singh/hidden-gems-of-debugging-1f11</guid>
      <description>&lt;p&gt;Anything that cuts some of your arduous task time is good for your productivity. Debugging is one of those strenuous tasks. Developers strive to write bug-free code, but we all know that it’s not easy. No one likes to see bugs in their code—those red color lines are not very appealing, right? Nonetheless, errors are good if you happen to catch them early. Debugging them properly will give you new learning and a feeling of satisfaction. There were countless moments when I learned a lot while debugging my own code and seeing how it could challenge my own concepts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We all learn by making mistakes, and make mistakes again to learn more.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Enough of motivation. Let's get to business.&lt;/p&gt;

&lt;p&gt;Today, I am going to share some of the tricks and tips you could use to supercharge your debugging skills. I've only tried these tricks in chrome dev tools (sorry FireFox users), but some of them are browser agnostic.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a id="short_hand"&gt;&lt;/a&gt; 1. Using shorthand object property value notation in console.log
&lt;/h3&gt;

&lt;p&gt;This is my favourite &lt;em&gt;console.log&lt;/em&gt; trick and has to be on top of the list. You must have written torrents of &lt;em&gt;console&lt;/em&gt; statements in your code base trying to figure out the value of a variable. The Convenience of use has made "&lt;strong&gt;console&lt;/strong&gt;" the leader of the debugging championship 🏆. The golden line looks like this:&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
This is awesome and you get to open up your console dev tool to see the value. But, as the number of lines grows, it becomes hard to trace the reference of that particular value.&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 20&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 30&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 40&lt;/span&gt;
&lt;span class="c1"&gt;// Sorry what was the reference variable for '30'? I am kind of lost😕&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;A very obvious solution to this would be to dump in some string representation along with your variable. Thanks to the versatile nature of &lt;strong&gt;console&lt;/strong&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints a 20&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints b 30&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints c 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;But as I said, we need to cut time and write fewer lines to be more efficient and productive, right? So, the trick is to wrap your variable inside the curly '&lt;strong&gt;{}&lt;/strong&gt;' braces. This is really not a trick but a language feature know as object property value shorthand notation. You can learn more about it &lt;a href="https://alligator.io/js/object-property-shorthand-es6/" rel="noopener noreferrer"&gt;here&lt;/a&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="c1"&gt;// use object shorthand property value notation&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// prints { a: 20 };&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// prints { b: 30 };&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;Doing so, will give you the reference variable along with its value. Less code more value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;: Use 'var' instead of 'const' or 'let' when declaring variables inside your chrome's console tab to avoid annoying &lt;strong&gt;Syntax Error&lt;/strong&gt;. 'var' is your scratchpad buddy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. No more wrapping of curly braces for fat &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noopener noreferrer"&gt;arrow&lt;/a&gt; functions
&lt;/h3&gt;

&lt;p&gt;You must have used &lt;strong&gt;fat arrow&lt;/strong&gt; function by now (if you haven't, you should start using it). They are short and precise and omit the requirement of the &lt;em&gt;curly&lt;/em&gt; braces and &lt;em&gt;return&lt;/em&gt; statement when writing your function definition.&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;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// how awesome is that 😊&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;A popular use case would be inside the array utility callbacks (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#" rel="noopener noreferrer"&gt;map&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#" rel="noopener noreferrer"&gt;filter&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#" rel="noopener noreferrer"&gt;reduce&lt;/a&gt;, etc)&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 4, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;Now, let say if you want to debug the map callback function, how would you go about printing the &lt;em&gt;el&lt;/em&gt; value? A simple answer would be to refactor the callback and throw in some curly braces around and inject return statement.&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

 &lt;span class="c1"&gt;// using curly braces and return statement&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// prints {el: 1}, {el: 2}, {el: 3}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;el&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;What! are you kidding me 🙈? This is not ideal. Not only it's time-consuming, but reverting back will consume more time too. A better solution is something like this:&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

   &lt;span class="c1"&gt;// more concise and inline 🎉&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;  &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;Whoa!! what was that? Don't worry, it's just JavaScript. I'll explain:&lt;/p&gt;

&lt;p&gt;There are two things going on here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;console.log()&lt;/strong&gt; always returns &lt;strong&gt;undefined&lt;/strong&gt; which is a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy" rel="noopener noreferrer"&gt;falsy value&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Given a statement like &lt;strong&gt;expr1 || expr2&lt;/strong&gt;,  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical" rel="noopener noreferrer"&gt;Logical OR operator&lt;/a&gt; (||) returns &lt;strong&gt;expr1&lt;/strong&gt; if it can be converted to &lt;strong&gt;true&lt;/strong&gt;; otherwise, returns &lt;strong&gt;expr2&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, return value of &lt;em&gt;console.log({ el })&lt;/em&gt; was &lt;em&gt;undefined&lt;/em&gt; and thus second expression &lt;em&gt;el * el&lt;/em&gt; was executed. The beauty about this change is that you get to see the value &lt;strong&gt;inline&lt;/strong&gt;. No debugger or cumbersome &lt;em&gt;curly&lt;/em&gt; ceremony is required. A big win, right? 😊&lt;/p&gt;

&lt;p&gt;This is very handy when you are a fan of functional programming and use a library like &lt;a href="https://ramdajs.com/" rel="noopener noreferrer"&gt;Ramda&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add logpoints—less sprinkling of console.log in your code
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;console.log&lt;/em&gt; has become an indispensable part of our debugging journey. We tend to sprinkle it everywhere in our code; it has become our first line of defense against bugs.&lt;br&gt;
The problem is &lt;em&gt;time consumption&lt;/em&gt; while cleaning up these logs statements. You can obviously go and find each console statement and remove them manually. But what if I say there's a better solution? Embrace new kid in the town—&lt;strong&gt;The logpoint&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Google has added this &lt;a href="https://developers.google.com/web/updates/2019/01/devtools" rel="noopener noreferrer"&gt;feature&lt;/a&gt; in Chrome 73. It allows you to add console statement in your &lt;strong&gt;source&lt;/strong&gt; code straight from your dev tool.&lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;strong&gt;Sources&lt;/strong&gt; section of your dev tool and locate your source code.&lt;/li&gt;
&lt;li&gt;Write click on any of the line numbers you want to add your console statements.&lt;/li&gt;
&lt;li&gt;A list of options will come up; select &lt;strong&gt;Add logpoint&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Breakpoint&lt;/strong&gt; editor will show up allowing you to enter the variable name. It's similar to how you write your expression inside &lt;em&gt;console.log()&lt;/em&gt; function.&lt;/li&gt;
&lt;li&gt;Enter a shorthand property notation trick I showed you above. Click outside the &lt;strong&gt;editor&lt;/strong&gt; or hit &lt;strong&gt;Enter&lt;/strong&gt; to save.&lt;/li&gt;
&lt;li&gt;An orange badge will represent your Logpoint and you are all set.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, every time your script is executed, you will see the values being logged out at the console section of your dev tool. You can add multiple log points.&lt;/p&gt;

&lt;p&gt;Action speaks louder than words:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F274an9y5c9uerghyf4lh.gif" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F274an9y5c9uerghyf4lh.gif" title="add log points" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;: You can also use conditional breakpoint feature to insert a breakpoint when the condition evaluates to true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  4. Store and copying
&lt;/h3&gt;

&lt;p&gt;Chrome dev tool is where I spend most of my time debugging. I use it as my JavaScript scratchpad. It has lots of in-built features at our disposal. One of the cool feature, that has been so helpful for me, is &lt;strong&gt;copying&lt;/strong&gt; the variable value into my &lt;strong&gt;clipboard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To copy any reference variable, for example, &lt;em&gt;x&lt;/em&gt;, into your clipboard, just type &lt;strong&gt;copy(x)&lt;/strong&gt; into the console tab and hit Enter. Chrome will copy that variable into your clipboard and you can paste it anywhere by using normal &lt;em&gt;paste&lt;/em&gt; command.&lt;/p&gt;

&lt;p&gt;The big benefit comes into picture when you are playing around with network response. Here you can combine this &lt;strong&gt;copy&lt;/strong&gt; feature with another excellent feature called &lt;strong&gt;store as global variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see that in action:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your &lt;strong&gt;Network&lt;/strong&gt; tab and select a &lt;strong&gt;request&lt;/strong&gt; from the list.&lt;/li&gt;
&lt;li&gt;Open up the &lt;strong&gt;Preview&lt;/strong&gt; section on your right.&lt;/li&gt;
&lt;li&gt;Right-click the property name and select &lt;strong&gt;Store as a global variable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The variable will be shown in your console tab, and you can use &lt;strong&gt;copy()&lt;/strong&gt; function to have it in your clipboard.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kill two birds with one stone.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxqd3qfehpjqshlv85rh0.gif" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxqd3qfehpjqshlv85rh0.gif" title="store as a global variable and then use copy command" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;: You can use 'Ctrl + f' to search for a particular string match over the entire response object in the preview section.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  5. The Breakpoint for your network requests
&lt;/h3&gt;

&lt;p&gt;Ideally, you should strive to keep your HTTP requests as minimum as possible while building your client. Not only it gives you a smooth &lt;strong&gt;performance&lt;/strong&gt; but also helps to avoid &lt;strong&gt;bugs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In one of our project at Insurance Company, we were building a &lt;strong&gt;data-driven&lt;/strong&gt; UI. Every next question in the form was driven by the user's previous actions and was fetched subsequently over the wire.&lt;br&gt;
We needed to analyse that when a particular kind of request was triggered, we could debug the request &lt;strong&gt;payload&lt;/strong&gt; accurately. I ended up finding this awesome solution called &lt;strong&gt;XHR/fetch Breakpoints&lt;/strong&gt;. It allowed me to put a conditional breakpoint on my fetch requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XHR Breakpoints&lt;/strong&gt; allow you to put breakpoints on all of your network requests. But the real power is the ability to apply breakpoint via '&lt;strong&gt;URL contains&lt;/strong&gt;' section. You can throw in any keyword there and chrome will put a &lt;strong&gt;pause&lt;/strong&gt; when it encounters the request URL containing the matching word. How awesome is that!!&lt;/p&gt;

&lt;p&gt;Steps are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;strong&gt;Sources&lt;/strong&gt; tab of your dev tool and on the right-side panel.&lt;/li&gt;
&lt;li&gt;You should see &lt;strong&gt;XHR/fetch Breakpoint&lt;/strong&gt; section below &lt;strong&gt;Breakpoints&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Hit &lt;strong&gt;+&lt;/strong&gt; button and a &lt;strong&gt;Breakpoint&lt;/strong&gt; editor should come up asking you to enter the keyword.&lt;/li&gt;
&lt;li&gt;Enter your keyword and hit &lt;strong&gt;Enter&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffc5d8mw10m9keudxtvsm.gif" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffc5d8mw10m9keudxtvsm.gif" title="fetch breakpoint" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Copying your request as cURL
&lt;/h3&gt;

&lt;p&gt;This is handy when you want to trigger your request from a &lt;strong&gt;command line&lt;/strong&gt; too like &lt;strong&gt;cmd&lt;/strong&gt; or &lt;strong&gt;bash&lt;/strong&gt;. You can also paste the copied link into your &lt;a href="https://www.getpostman.com/" rel="noopener noreferrer"&gt;Postman app&lt;/a&gt; and it will fill up all the request headers for you. Very handy in visualising your request.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open you &lt;strong&gt;Network&lt;/strong&gt; tab inside your chrome dev tool.&lt;/li&gt;
&lt;li&gt;Locate the &lt;strong&gt;request&lt;/strong&gt; you want to copy and right-click to bring multiple options.&lt;/li&gt;
&lt;li&gt;Hovering over the &lt;strong&gt;Copy&lt;/strong&gt; options will show you a list of a different format you can copy your request as.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Copy as cURL&lt;/strong&gt; and it will be saved into your &lt;strong&gt;clipboard&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjl54fll4xn5jya2yjw5e.gif" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjl54fll4xn5jya2yjw5e.gif" title="copy as cURL" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Don't let go off your CSS changes—persist styling when refreshing
&lt;/h3&gt;

&lt;p&gt;This tip is dedicated to our &lt;strong&gt;UI/Designers&lt;/strong&gt; friends. We know that via &lt;strong&gt;Styles&lt;/strong&gt; section of &lt;strong&gt;Elements&lt;/strong&gt; tab, we can modify the &lt;strong&gt;CSS&lt;/strong&gt;. But these changes &lt;strong&gt;don't&lt;/strong&gt; persist when you &lt;strong&gt;refresh&lt;/strong&gt; the page. If you wish to persist the modification you made to the existing stylesheet, you can do this via the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;strong&gt;Sources&lt;/strong&gt; tab and select &lt;strong&gt;Overrides&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Select folder for overrides&lt;/strong&gt; and select a folder where you want to save your changes.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;permission&lt;/strong&gt; dialogue would pop up asking for permission. Go ahead and allow.&lt;/li&gt;
&lt;li&gt;Select your folder and you are all set to rock 'n' roll.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now every time you make changes to your existing styles, they won't disappear even when you refresh the page.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: Adding new style changes won't persist though!.&lt;/p&gt;




&lt;h3&gt;
  
  
  Final thoughts
&lt;/h3&gt;

&lt;p&gt;You can't change the fact that it's practically &lt;strong&gt;impossible&lt;/strong&gt; to write a 100% &lt;strong&gt;bug-free&lt;/strong&gt; code. Rather than slamming your head against your desk, let's adapt and develop our debugging skills. Believe me, you will learn a lot by exploring the root causes of your bugs.&lt;/p&gt;

&lt;p&gt;If you liked the article, a few ❤️ will definitely make me smile 😀. Now, go and &lt;del&gt;kill&lt;/del&gt; remove some bugs now 🐞.&lt;/p&gt;

</description>
      <category>debugging</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
