<?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: Arpit Batra</title>
    <description>The latest articles on DEV Community by Arpit Batra (@arpitbatra123).</description>
    <link>https://dev.to/arpitbatra123</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%2F165091%2F095ef607-dd39-4a43-bd97-4befe6f4ed7d.jpeg</url>
      <title>DEV Community: Arpit Batra</title>
      <link>https://dev.to/arpitbatra123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpitbatra123"/>
    <language>en</language>
    <item>
      <title>the importance of web accessibility</title>
      <dc:creator>Arpit Batra</dc:creator>
      <pubDate>Thu, 04 Feb 2021 10:17:12 +0000</pubDate>
      <link>https://dev.to/arpitbatra123/the-importance-of-web-accessibility-h48</link>
      <guid>https://dev.to/arpitbatra123/the-importance-of-web-accessibility-h48</guid>
      <description>&lt;p&gt;I believe the “web” is one of the most important inventions of humankind. We take being connected to the world 24x7 for granted now, but it’s the web that has empowered us to achieve this feat. But, while we are empowering the world through the web, some people are still being left out because our web interfaces are not accessible.&lt;/p&gt;

&lt;p&gt;Let me explain by telling you a personal anecdote.&lt;/p&gt;

&lt;p&gt;Some months ago when COVID was not a word, I used to travel to a market near my home on a Metro train. The stairs that lead to the entrance of the Metro station have a very peculiar architecture. They are still stairs but a slope of some kind runs in a zig-zag fashion throughout them. See the picture below to have an idea of what I am saying.&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%2Fy1ms8qgsfpr7x6hysey4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy1ms8qgsfpr7x6hysey4.jpeg" alt="Indiranagar Metro Station"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a very long time, I did not think very much of it, until one day I saw a guy drag a man in a wheelchair across that slope. Then it hit me, the slope was there to facilitate users who were unable to use the stairs because of a disability.&lt;/p&gt;

&lt;p&gt;The short story you just read was an example of accessibility in the real world. The stairs facilitate the movement of able people, the slope does the same for those who are unable to. Both of these blend together so well that it’s almost difficult to think of them separately.&lt;/p&gt;

&lt;p&gt;Accessibility on the web are the same principles but applied to web development. In other words, people’s disabilities should not affect their usage of the web. That of course, is a very ambitious statement, but that is precisely the aim of technology. Things that were unthinkable of yesterday shall be possible tomorrow.&lt;/p&gt;

&lt;p&gt;Let me now give you some reasons why I have started caring about accessibility:&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Accessibility empowers all users
&lt;/h1&gt;

&lt;p&gt;The role of technology is to take humanity to the next level. The invention of the World Wide Web is the driving force for that. The internet has made a student in a remote city in India learn from the best Entrepreneurs in Silicon Valley. But when we don’t think of accessibility, a lot of people with disabilities are unable to reap the benefits of the web.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Technology is meant to empower people, and inaccessible interfaces leave disabled users feeling powerless.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  2. The absence of accessibility deeply affects people’s emotions
&lt;/h1&gt;

&lt;p&gt;(alt- You don’t wanna make Larene’s dad cry)&lt;/p&gt;

&lt;p&gt;The internet has brought about an information revolution but what good is a revolution which leaves a lot of people out deliberately?&lt;/p&gt;

&lt;p&gt;As Software Engineers, we like to think that we are “Changing the world” but not all of these changes are positive. See this tweet by the user LareneLg for an example of the adverse effects of an inaccessible web:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1270578058714443776-818" src="https://platform.twitter.com/embed/Tweet.html?id=1270578058714443776"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1270578058714443776-818');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1270578058714443776&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;A few months ago the below video was making rounds on Twitter. It shows how a blind user Kristy_Viersis able to use her iPhone and tweet through the use of accessibility features. Everybody commended the accessibility features that were able to provide her this experience which did not exclude her because of her disability.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1287189581926981634-445" src="https://platform.twitter.com/embed/Tweet.html?id=1287189581926981634"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1287189581926981634-445');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1287189581926981634&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;The two tweets you just read demonstrate the wide contrast that exists due to the absence of accessibility on the web.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always remember all your users are not young healthy people in their 20s, so design for a wide range of users and make sure your web interfaces are accessible&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  3. Accessibility is not just about blind users
&lt;/h1&gt;

&lt;p&gt;Due to the tweets you read in the previous point, you must be thinking that accessibility mostly focuses on making interfaces usable for blind users. However, that is not the case. In reality, people’s experience on the web is affected by a wide range of disabilities, ranging from visual (complete blindness, colour blindness, etc.) to auditory (hearing impaired), etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thus, accessibility is making sure that the web should be usable by the widest range of people possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Color Contrast is one of the things we need to make sure while designing interfaces, as we can see clearly (pun intended), people with good vision can also be adversely affected if we don’t incorporate accessibility in design.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Accessibility encourages best practices
&lt;/h1&gt;

&lt;p&gt;Some techniques which are useful for making the web usable also bring in some added benefits. For example, if you write HTML that follows proper semantics, you also reap some benefits in the form of SEO as the search crawlers can extract proper headings from your pages.&lt;/p&gt;

&lt;p&gt;If Accessibility is taken care of in the design step itself, then it can enhance the experience of other users as well. For example, if colors that have good contrast are chosen for the designs, even sighted users will benefit from avoiding strain while reading.&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Accessibility saves your company from Lawsuits
&lt;/h1&gt;

&lt;p&gt;Don’t believe me? &lt;a href="https://www.cnbc.com/2019/10/07/dominos-supreme-court.html" rel="noopener noreferrer"&gt;See&lt;/a&gt; for yourself.&lt;/p&gt;

&lt;h1&gt;
  
  
  Closing thoughts
&lt;/h1&gt;

&lt;p&gt;I hope I was able to open your mind to how important accessibility on the web is. Your next steps from here should be how to incorporate accessibility during development and design. Do remember that small steps towards accessibility can make a big difference. Awareness is the first step, Action the second. The web is full of information on how you can write code that makes sure interfaces on the web are accessible. In the upcoming weeks, I am planning to write some blog posts with quick tips that can help you with accessibility.&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is the 'box-sizing' property in css and why should it have a value of 'border-box'?</title>
      <dc:creator>Arpit Batra</dc:creator>
      <pubDate>Mon, 13 Jul 2020 16:11:48 +0000</pubDate>
      <link>https://dev.to/arpitbatra123/what-is-the-box-sizing-property-in-css-and-why-should-it-have-a-value-of-border-box-gbe</link>
      <guid>https://dev.to/arpitbatra123/what-is-the-box-sizing-property-in-css-and-why-should-it-have-a-value-of-border-box-gbe</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;TLDR: By default, css adds padding and margin values to the width and height of your elements. A 200px wide box with 10px padding will have an effective width of 220px. Use &lt;code&gt;box-sizing: border-box;&lt;/code&gt; to prevent this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine yourself embarking on a journey towards learning front-end web development. You are pretty excited as you should be! One day, you will turn beautiful designs into code.&lt;/p&gt;

&lt;p&gt;You've read countless blog posts on writing effective CSS. You don't remember what most of them were about, but you do know that the golden rule of writing CSS is that “Everything is a box”. Therefore the first thing you will draw using CSS is, well, a box.&lt;/p&gt;

&lt;p&gt;Should be fairly easy!&lt;/p&gt;

&lt;p&gt;You start by writing some HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you add some CSS&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;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;lightgoldenroadyellow&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;200px&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;200px&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;iframe height="600" src="https://codepen.io/arpitbatra123/embed/rNxKyEo?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;



&lt;p&gt;And sure enough, you see a box on the screen.&lt;/p&gt;

&lt;p&gt;You've done well so far. Pat yourself on the back. That box does look lonely though! How about we add some text inside the box?&lt;/p&gt;

&lt;p&gt;We add a span with some text to our HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt;&lt;span class="nt"&gt;&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;"text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hey There!&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's also add some styles to the text we just added.&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;.text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&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;iframe height="600" src="https://codepen.io/arpitbatra123/embed/NWxzjKw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Cool! That sure does look fine but the text is stuck on the corner. Let's add some padding to the box.&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;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;lightgoldenrodyellow&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;200px&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;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&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://res.cloudinary.com/practicaldev/image/fetch/s--wHpW7ZhU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i9a5ty5wvg4z2kwqx9pk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wHpW7ZhU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i9a5ty5wvg4z2kwqx9pk.gif" alt="gif showing an increase in the width of the box due to extra padding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WHAT? The dimensions of the box have changed? What's happening? After furiously digging in the browser's dev tools, you find out the padding value of &lt;code&gt;10px&lt;/code&gt; is being added to the width and height of the box. The browser is doing this and is just ignoring the width and height you've specified.&lt;/p&gt;

&lt;p&gt;So you have to calculate the actual width of all boxes in your brain now. You get anxiety by thinking of all the math you will have to do from now on. If you are somewhat like me, you are wanting to just quit and get back to browsing twitter or watching &lt;strong&gt;#productivity&lt;/strong&gt; videos on youtube. You are also thinking of transitioning to learning back end development where at least the code you write will be respected and the language won't do any magic on its own. (This is far from the truth, but you don't know that yet)&lt;/p&gt;

&lt;p&gt;All of your feelings are valid. At this point, you might start to not like CSS so much.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Whenever there is a divergence between your thinking model and the workings of a language, bugs, and frustration are bound to happen&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Is there a way to make CSS respect the widths and height you specify?&lt;/p&gt;

&lt;p&gt;Yes, there is. Let us add the &lt;code&gt;box-sizing&lt;/code&gt; property with a value of &lt;code&gt;border-box&lt;/code&gt; to the styles of the box element.&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;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;lightgoldenrodyellow&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;200px&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;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&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;iframe height="600" src="https://codepen.io/arpitbatra123/embed/NWxzjKw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Lo and behold! The box has the padding, and also has the values for the widths and heights that you specified!&lt;/p&gt;

&lt;p&gt;How did this work? Well, by default, all elements have the value &lt;code&gt;content-box&lt;/code&gt; for &lt;code&gt;box-sizing&lt;/code&gt;. This means any padding and margins you add are added to the width and height of elements. &lt;code&gt;border-box&lt;/code&gt; prevents that behavior.&lt;/p&gt;

&lt;p&gt;You want &lt;code&gt;border-box&lt;/code&gt; to be the default value for all elements to eradicate the frustration you were dealing with a few minutes ago! This can be done easily by using universal selectors in CSS. &lt;/p&gt;

&lt;p&gt;Here is how this css-tricks &lt;a href="https://css-tricks.com/box-sizing/"&gt;article&lt;/a&gt; recommends we do it at a global level:&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="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&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="o"&gt;*&lt;/span&gt;&lt;span class="nd"&gt;:before&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nd"&gt;:after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inherit&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;Once this is done, you should not struggle with weird widths anymore. The browser will now obey you.&lt;/p&gt;

&lt;p&gt;These are the articles that helped me out in understanding and imparting this box-sizing wisdom to you. Visit them for more information on box-sizing and some examples of how this property can be helpful in a world real use case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/box-sizing/"&gt;https://css-tricks.com/box-sizing/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.paulirish.com/2012/box-sizing-border-box-ftw/"&gt;https://www.paulirish.com/2012/box-sizing-border-box-ftw/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>coding</category>
      <category>css</category>
      <category>frontend</category>
    </item>
    <item>
      <title>replacing lodash.get with new js features</title>
      <dc:creator>Arpit Batra</dc:creator>
      <pubDate>Tue, 24 Dec 2019 18:18:11 +0000</pubDate>
      <link>https://dev.to/arpitbatra123/replacing-lodash-get-with-new-js-features-5fie</link>
      <guid>https://dev.to/arpitbatra123/replacing-lodash-get-with-new-js-features-5fie</guid>
      <description>&lt;p&gt;&lt;a href="https://lodash.com/"&gt;lodash&lt;/a&gt; is a popular javascript library with a set of utilities. One of the utilities that lodash provides is the &lt;a href="https://lodash.com/docs/4.17.15#get"&gt;get&lt;/a&gt; function which, as the name suggests is used to get a value from an object. Let us see an example:&lt;/p&gt;

&lt;p&gt;Suppose there is an object&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&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;Bojack&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you wanted to get the &lt;code&gt;name&lt;/code&gt; from the above object, you would typically do it 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this will work and output the name &lt;code&gt;Bojack&lt;/code&gt;. No Worries, Right?&lt;/p&gt;

&lt;p&gt;Well, for the most part, it is. Let me explain. Say, for example, this object is a response from an API and because the coding gods are angry on you, the object has a structure different from what you think it will be. The key &lt;code&gt;options&lt;/code&gt; in the &lt;code&gt;data&lt;/code&gt; object is now called &lt;code&gt;user_options&lt;/code&gt; and the code you've written does not account for that. Now If you run the code to get the value of name this is what will happen:&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;data.options&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt; and you are basically trying to access the &lt;code&gt;name&lt;/code&gt; property of &lt;code&gt;undefined&lt;/code&gt;, you end up with a &lt;code&gt;TypeError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To make sure you do not end up in such a situation, you have to put up safe checks in your code. For example, to avoid the situation described above, we can do 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&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="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Todd&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;This will make sure that if at any level of the object, the property you are trying to access is &lt;code&gt;undefined&lt;/code&gt;, you do not try to access properties further down the chain and thus, do not end up with a &lt;code&gt;TypeError&lt;/code&gt;. Also In the above statement, if the property we are trying to access is undefined, &lt;code&gt;Todd&lt;/code&gt; gets assigned to the name which kind of acts as a fallback value.&lt;/p&gt;

&lt;p&gt;However, as you can clearly see in the code, for large objects, the above code segment can be very cumbersome to implement.&lt;/p&gt;

&lt;p&gt;Enter lodash.&lt;/p&gt;

&lt;p&gt;lodash's &lt;code&gt;get&lt;/code&gt; function lets you easily implement safe checks while getting data from objects. If we use lodash for the above example, we can do it 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data.options.name&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;This will make sure that you do not end up with errors and also don't have to write complex accessor chains like in the example before this one.&lt;/p&gt;

&lt;p&gt;Another beauty of the lodash &lt;code&gt;get&lt;/code&gt; function is that you can specify an optional 3rd argument which is the default value to return when the property you are trying to access is falsy. 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data.options.name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Todd&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;will return Todd if the &lt;code&gt;options&lt;/code&gt; key on &lt;code&gt;data&lt;/code&gt; does not exist.&lt;/p&gt;

&lt;p&gt;So we solved both of the problems we ran into while writing property accessors.&lt;/p&gt;

&lt;p&gt;Win-Win right?&lt;/p&gt;

&lt;p&gt;Well, not exactly. lodash is essentially a 3rd party dependency and you must have heard the saying &lt;code&gt;mo dependencies, mo problems&lt;/code&gt;. Some of these problems include increased bundle size and the responsibility of keeping dependencies up to date.&lt;/p&gt;

&lt;p&gt;However, there seems to be a solution coming up in javascript itself. There have been two proposals to add the following two features to the language itself - &lt;a href="https://v8.dev/features/optional-chaining"&gt;optional chaining&lt;/a&gt; and &lt;a href="https://v8.dev/features/nullish-coalescing"&gt;nullish coalescing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let us see how these language features can help us in replacing lodash with native javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optional Chaining:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As also shown above, this is how you would safely access the &lt;code&gt;name&lt;/code&gt; property from the response object.&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data.options.name&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;This is how you can do it without lodash by using optional chaining:&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;options&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement behaves similar to how our code with a lot of &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; safe checks would behave, but looks a lot cleaner!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nullish Coalescing:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we now have safe checks in property accessors, but what about default values, If I am unable to find the name property, I want to assign the name &lt;code&gt;Todd&lt;/code&gt; to the &lt;code&gt;name&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Here is how you can do it by using Nullish coalescing:&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;options&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Todd&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;In the above statement, we combined the use of &lt;em&gt;optional chaining&lt;/em&gt; and &lt;em&gt;nullish coalescing&lt;/em&gt; to achieve the same result that we were getting by using lodash or making the use of &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; operators. If I am unable to access the property &lt;code&gt;name&lt;/code&gt; from the &lt;code&gt;response&lt;/code&gt; object, due to optional chaining, the name will have the default property &lt;code&gt;Todd&lt;/code&gt; thanks to nullish coalescing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So Can I go ahead and replace &lt;code&gt;lodash.get&lt;/code&gt; in my app?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, not really. As of today, 15th of December, 2019 Both the new JS features we talked about in this article are only at stage 3 of the &lt;a href="https://tc39.es/process-document/"&gt;tc39&lt;/a&gt; proposal. This means that it will take a while before it ships in all browsers and we can use it. However, you can also use the following babel polyfills to use these features ahead of time - &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining"&gt;1&lt;/a&gt; and &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator"&gt;2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Further Reading/References -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://v8.dev/features/nullish-coalescing"&gt;https://v8.dev/features/nullish-coalescing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://2ality.com/2019/08/nullish-coalescing.html"&gt;https://2ality.com/2019/08/nullish-coalescing.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v8.dev/features/optional-chaining"&gt;https://v8.dev/features/optional-chaining&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://2ality.com/2019/07/optional-chaining.html"&gt;https://2ality.com/2019/07/optional-chaining.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What was the first interesting program that you remember writing?</title>
      <dc:creator>Arpit Batra</dc:creator>
      <pubDate>Tue, 21 May 2019 15:27:56 +0000</pubDate>
      <link>https://dev.to/arpitbatra123/what-was-the-first-interesting-program-that-you-remember-writing-5enf</link>
      <guid>https://dev.to/arpitbatra123/what-was-the-first-interesting-program-that-you-remember-writing-5enf</guid>
      <description>&lt;p&gt;I'm Reading this book called "Coders at Work" where the author asks this question to the developers/coders he's interviewing. &lt;em&gt;What was the first interesting program that you remember writing?&lt;/em&gt; I'm very interested in knowing what's the first interesting program the DEV community wrote. Let me know in the comments? 🤔&lt;/p&gt;

</description>
      <category>code</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
