<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishek Saxena on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Saxena on Medium]]></description>
        <link>https://medium.com/@abhi-saxena1809?source=rss-315329741456------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_OKLRLM4hHdPDcc1.jpg</url>
            <title>Stories by Abhishek Saxena on Medium</title>
            <link>https://medium.com/@abhi-saxena1809?source=rss-315329741456------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 18:48:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhi-saxena1809/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optional Chaining (But reading this article is not optional…)]]></title>
            <link>https://abhi-saxena1809.medium.com/optional-chaining-but-reading-this-article-is-not-optional-6e56e65b6f9f?source=rss-315329741456------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e56e65b6f9f</guid>
            <category><![CDATA[ecmascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[objects-in-javascript]]></category>
            <dc:creator><![CDATA[Abhishek Saxena]]></dc:creator>
            <pubDate>Wed, 02 Dec 2020 22:21:21 GMT</pubDate>
            <atom:updated>2020-12-02T22:26:55.443Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey Folks!!!</p><p>Today we are going to explore a new topic of modern javascript (polyfills are always there 😅)</p><p>Optional chaining (‘<strong>?.’</strong>)</p><p>So javascript is very clever but at some point , it is just a piece of code which don’t know what to do (it occurs rarely 😎)</p><p>So there is a problem named “non-existing-property”… Wait what is this?? 😕…let’s dive into an example</p><p>As an example, let’s say we have user objects that hold the information about our users.</p><p>Most of our users have addresses in user.address property, with the street user.address.street, but some did not provide them.</p><p>In such case, when we attempt to get user.address.street, and the user happens to be without an address, we get an error:</p><pre>let user = {}; // a user without &quot;address&quot; property</pre><pre>alert(user.address.street); // Error!</pre><p>That’s the expected result. JavaScript works like this. As user.address is undefined, an attempt to get user.address.street fails with an error.</p><p>In many practical cases we’d prefer to get undefined instead of an error here (meaning “no street”).</p><p>…And another example. In the web development, we can get an object that corresponds to a web page element using a special method call, such as document.querySelector(&#39;.elem&#39;), and it returns null when there’s no such element.</p><pre>// document.querySelector(&#39;.elem&#39;) is null if there&#39;s no element let html = document.querySelector(&#39;.elem&#39;).innerHTML; // error if it&#39;s null</pre><p>But Wait there is already a solution for this (using If Condition Or &amp;&amp; operator😊)<br>Let us see an example using the &amp;&amp; operator:</p><pre>let user = {}; // user has no address</pre><pre>alert( user.address &amp;&amp; user.address.street &amp;&amp; user.address.street.name ); // undefined (no error)</pre><p>AND’ing the whole path to the property ensures that all components exist (if not, the evaluation stops), but also isn’t ideal.</p><p>As you can see, property names are still duplicated in the code. E.g. in the code above, user.address appears three times.</p><p>That’s why the optional chaining ?. was added to the language. To solve this problem once and for all!</p><p>The optional chaining ?. stops the evaluation if the part before ?. is undefined or null and returns that part.</p><p>Another example ?😛</p><p>So let us take same example (👆)</p><p>Here’s the safe way to access user.address.street using ?.:</p><pre>let user = {}; // user has no address</pre><pre>alert( user?.address?.street ); // undefined (no error)</pre><p>The code is short and clean, there’s no duplication at all.</p><p>Reading the address with user?.address works even if user object doesn’t exist:</p><pre>let user = null;</pre><pre>alert( user?.address ); // undefined<br>alert( user?.address.street ); // undefined</pre><p>Please note: the ?. syntax makes optional the value before it, but not any further. But there is a catch (😧)</p><p><strong>The variable before </strong><strong>?. must be declared</strong></p><p>If there’s no variable user at all, then user?.anything triggers an error:</p><pre>// ReferenceError: user is not defined<br>user?.address;</pre><p>The variable must be declared (e.g. let/const/var user or as a function parameter). The optional chaining works only for declared variables.</p><h3><a href="https://javascript.info/optional-chaining#other-variants">Other variants: ?.(), ?.[]</a></h3><p>The optional chaining ?. is not an operator, but a special syntax construct, that also works with functions and square brackets.</p><p>For example, ?.() is used to call a function that may not exist.</p><p>In the code below, some of our users have admin method, and some don’t:</p><pre>let userAdmin = {<br>  admin() {<br>    alert(&quot;I am admin&quot;);<br>  }<br>};</pre><pre>let userGuest = {};</pre><pre>userAdmin.admin?.(); // I am admin</pre><pre>userGuest.admin?.(); // nothing (no such method)</pre><p>Here, in both lines we first use the dot (object.admin) to get admin property, because the user object must exist, so it’s safe read from it.</p><p>Then ?.() checks the left part: if the admin function exists, then it runs (that’s so for userAdmin). Otherwise (for userGuest) the evaluation stops without errors.</p><p>The ?.[] syntax also works, if we’d like to use brackets [] to access properties instead of dot .. Similar to previous cases, it allows to safely read a property from an object that may not exist.</p><pre>let person1 = {<br>  firstName: &quot;John&quot;<br>};</pre><pre>let person2 = null; // Imagine, we couldn&#39;t authorize the user</pre><pre>let key = &quot;firstName&quot;;</pre><pre>alert( person1?.[key] ); // John<br>alert( person2?.[key] ); // undefined</pre><pre>alert( person1?.[key]?.something?.not?.existing); // undefined</pre><p>Also we can use ?. with delete:</p><pre>delete person?.name; // delete person.name if person exists</pre><h4><strong>Summary</strong></h4><pre>obj.val?.prop<br>obj.val?.[expr]<br><em>obj.arr</em>?.[index]<br>obj.func?.(args)</pre><p>Thank you for reading 🙌</p><p>Clap! if this was useful for you 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e56e65b6f9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Journey of Nullish coalescing operator (?? )]]></title>
            <link>https://abhi-saxena1809.medium.com/journey-of-nullish-coalescing-operator-80963b9fd546?source=rss-315329741456------2</link>
            <guid isPermaLink="false">https://medium.com/p/80963b9fd546</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ternary-operator]]></category>
            <category><![CDATA[operator-or]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Abhishek Saxena]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 06:37:10 GMT</pubDate>
            <atom:updated>2020-06-24T07:07:25.115Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey folks!!!</p><p>Today we are going into one new topic of javascript or i can say modern javascript 🤨.</p><p>Nullish coalescing operator…wait!! even i am not able to pronounce it 😶, but who cares 😋.</p><p>Let’s start the journey !!!!!!😎</p><p><em>The nullish coalescing operator </em><em>?? provides a short syntax for selecting a first “defined” variable from the list.</em></p><p>The result of a ?? b is:</p><ul><li>a if it’s not null or undefined,</li><li>b, otherwise.</li></ul><p>So, x = a ?? b is a short equivalent to:</p><pre>x = (a !== null &amp;&amp; a !== undefined) ? a : b;</pre><p>Yeahhh!!!!! we decoded the mystery 😎….</p><p>But why to use this, we already have different methods for this.🤐</p><p>So, this came into picture when we have some issues (not exactly the issues.. i guess🙄 ) with OR (||) operator.</p><p><strong>Comparison with OR (||)</strong></p><p>The OR || operator can be used in the same way as ??. Actually, we can replace ?? with || in the code above and get the same result.</p><p>The important difference is that:</p><ul><li>|| returns the first <em>truthy</em> value.</li><li>?? returns the first <em>defined</em> value.</li></ul><p>This matters a lot when we’d like to treat null/undefined differently from 0😯.</p><p>Lot of theory now. Let’s look a example 😇</p><pre>money = money ?? 100;</pre><p>This sets money to 100 if it’s not defined.</p><p>Let’s compare it with ||:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c9ba94866036bb5ce99f43fabc76d3d/href">https://medium.com/media/5c9ba94866036bb5ce99f43fabc76d3d/href</a></iframe><p>Here, money || 100 treats zero money as unset, same as null, undefined or any other falsy value. So the result is 100.</p><p>The money ?? 100 returns 100 only if money is exactly null or undefined. So the alert shows the money value 0 “as is”.</p><p>Which behavior is better depends on a particular use case. When zero money is a valid value😥 , then ?? is preferable.</p><p>Waitt.. It will not work in IE😥.But no issues ‘Polyfills are the saviours😇’</p><p>Thank you for reading 🙌</p><p>Clap! if this was useful for you 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80963b9fd546" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A ride to closure]]></title>
            <link>https://abhi-saxena1809.medium.com/a-ride-to-closure-9082e82d29e?source=rss-315329741456------2</link>
            <guid isPermaLink="false">https://medium.com/p/9082e82d29e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[closures-functions]]></category>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Abhishek Saxena]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 20:14:06 GMT</pubDate>
            <atom:updated>2020-06-21T20:14:06.832Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Hey!!!!<br>Learning JS &amp; every time a thing comes Closure, Ahhhhh i know listening closer is easy and enjoyable but learning it , it is a bit boring &amp; also bit sleepy, But i just want to say it is very IMPORTANT!!!!!. So just start the journey of closure .</blockquote><p>So Let’s start with a very Basic Example…Wait waitttt!!!!! 😮<br>First lets have some theory part 👹—</p><p>Closure is a property(Still confused😨…) in JS which enables us to use parent scope variables after the execution of parent is over!!!! 😜.</p><p>Wait What……This actually confused me 😢…..</p><p>In simple language…In nested function you can use the parent variables in child function and return the computed value from both.</p><p>Still not clear ?😥<br>Lets look a example🤞</p><pre>function addFunction(x){<br>return function(y) {<br>return x + y;<br>}}</pre><pre>let a = addFunction(5)(10); // result will be 15<br></pre><p>Ok Lets Explain this!!!</p><p>Firstly we created a function (not using ES6, but i prefer it) addFunction<br>which is returning a new function(😮)……..which returns finally x + y; <br>But Let’s see the function calling .</p><p>Here we declared a variable a (very bad in choosing name🥺) which calls the addFunction twice (don’t confuse with twice function calling) in the function it will return something like this 👐.</p><pre>dummy = addFunction(5);</pre><pre>// Above line will return something like this.<br>dummy = <strong>ƒ(y) {<br>return x + y;<br>}</strong></pre><p>Here we used dummy to see the first function calling, so when we call the add function with parameter 5 it returns a function like above🤷‍♀.</p><p>Now this function is used to call the addFunction again with parameter 10😋</p><p>Which still holds the value of x in the scope(that’s the power of closure)</p><p>Now when we call the function it will return something like this.</p><pre>var sum = dummy(10);<br>console.log(sum) // 15</pre><p>Thank you for reading 🙌</p><p>Clap! if this was useful for you 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9082e82d29e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>