<?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 Shubhamcomputer on Medium]]></title>
        <description><![CDATA[Stories by Shubhamcomputer on Medium]]></description>
        <link>https://medium.com/@shubhamcomputer33_32044?source=rss-649ddd713535------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hX7Z0ojAupMV8WA7</url>
            <title>Stories by Shubhamcomputer on Medium</title>
            <link>https://medium.com/@shubhamcomputer33_32044?source=rss-649ddd713535------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:04:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shubhamcomputer33_32044/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[Why Were Arrow Functions Introduced in JavaScript?]]></title>
            <link>https://medium.com/@shubhamcomputer33_32044/why-were-arrow-functions-introduced-in-javascript-eb13503457f4?source=rss-649ddd713535------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb13503457f4</guid>
            <category><![CDATA[javascript-scope]]></category>
            <category><![CDATA[javascript-arrow-function]]></category>
            <category><![CDATA[javascript-this-keyword]]></category>
            <dc:creator><![CDATA[Shubhamcomputer]]></dc:creator>
            <pubDate>Sat, 06 Jun 2026 19:54:30 GMT</pubDate>
            <atom:updated>2026-06-06T19:54:30.288Z</atom:updated>
            <content:encoded><![CDATA[<h3>Most JavaScript developers use arrow functions every day:</h3><pre>const add = (a, b) =&gt; a + b;</pre><p>But arrow functions were not introduced just to save a few keystrokes.</p><p>In fact, their primary purpose was to solve one of JavaScript’s most confusing problems: the behavior of this.</p><p>Let’s understand why arrow functions were added to the language and what problems they were designed to solve.</p><h3>JavaScript Before Arrow Functions</h3><p>Before ES6, developers primarily used regular functions:</p><pre>function add(a, b) {<br>    return a + b;<br>}</pre><p>There was nothing wrong with this syntax. The real issue appeared when functions were used as callbacks.</p><p>Consider this example:</p><pre>function Timer() {<br>    this.seconds = 0;</pre><pre>    setInterval(function () {<br>        this.seconds++;<br>        console.log(this.seconds);<br>    }, 1000);<br>}</pre><pre>new Timer();</pre><p>Most developers would expect the output to be:</p><pre>1<br>2<br>3<br>4<br>...</pre><p>Instead, it either throws an error or behaves unexpectedly.</p><p>Why?</p><p>Because in JavaScript, this is determined by <strong>how a function is called</strong>, not where it is written.</p><p>The callback passed to setInterval is invoked by the browser, so this no longer refers to the Timer instance.</p><p>This became one of the most common sources of bugs in JavaScript applications.</p><h3>The Workarounds</h3><p>Developers came up with several solutions.</p><h3>Solution 1: Store this in a Variable</h3><pre>function Timer() {<br>    this.seconds = 0;</pre><pre>    const self = this;</pre><pre>    setInterval(function () {<br>        self.seconds++;<br>        console.log(self.seconds);<br>    }, 1000);<br>}</pre><p>You may have seen variables named:</p><pre>var self = this;<br>var that = this;</pre><p>This pattern was extremely common before ES6.</p><h3>Solution 2: Use bind</h3><p>ES5 introduced bind.</p><pre>function Timer() {<br>    this.seconds = 0;</pre><pre>    setInterval(function () {<br>        this.seconds++;<br>        console.log(this.seconds);<br>    }.bind(this), 1000);<br>}</pre><p>This works, but it adds extra complexity and is easy to forget.</p><h3>Enter Arrow Functions</h3><p>ES6 introduced arrow functions.</p><pre>function Timer() {<br>    this.seconds = 0;</pre><pre>    setInterval(() =&gt; {<br>        this.seconds++;<br>        console.log(this.seconds);<br>    }, 1000);<br>}</pre><p>Now everything works as expected.</p><p>Why?</p><p>Because arrow functions do not have their own this.</p><p>Instead, they inherit this from the surrounding scope.</p><p>This behavior is called <strong>lexical this</strong>.</p><p>When the arrow function is created inside Timer, it captures the this value of Timer.</p><p>As a result:</p><pre>this.seconds++;</pre><p>correctly updates the instance property.</p><h3>Lexical this Explained</h3><p>Regular function:</p><pre>const obj = {<br>    name: &quot;Shubham&quot;,</pre><pre>    greet: function () {<br>        console.log(this.name);<br>    }<br>};</pre><pre>obj.greet();</pre><p>Output:</p><pre>Shubham</pre><p>Now:</p><pre>const obj = {<br>    name: &quot;Shubham&quot;,</pre><pre>    greet: () =&gt; {<br>        console.log(this.name);<br>    }<br>};</pre><pre>obj.greet();</pre><p>Output:</p><pre>undefined</pre><p>Why?</p><p>Arrow functions do not get their own this.</p><p>They inherit it from the surrounding scope, which is not obj in this case.</p><p>This is one reason why arrow functions should generally not be used as object methods.</p><h3>Other Benefits</h3><h3>Shorter Syntax</h3><p>Regular Function:</p><pre>const square = function (x) {<br>    return x * x;<br>};</pre><p>Arrow Function:</p><pre>const square = x =&gt; x * x;</pre><h3>Cleaner Array Operations</h3><p>Before:</p><pre>const result = numbers.map(function (n) {<br>    return n * 2;<br>});</pre><p>After:</p><pre>const result = numbers.map(n =&gt; n * 2);</pre><p>This made functional programming patterns much more readable.</p><h3>What Arrow Functions Do Not Have</h3><p>Arrow functions intentionally do not have:</p><h3>Their Own this</h3><pre>() =&gt; {}</pre><p>Uses surrounding this.</p><h3>arguments</h3><pre>const fn = () =&gt; {<br>    console.log(arguments);<br>};</pre><p>This does not work as expected.</p><p>Use rest parameters instead:</p><pre>const fn = (...args) =&gt; {<br>    console.log(args);<br>};</pre><h3>Constructor Capability</h3><p>Regular function:</p><pre>function Person(name) {<br>    this.name = name;<br>}</pre><pre>new Person(&quot;John&quot;);</pre><p>Arrow function:</p><pre>const Person = (name) =&gt; {<br>    this.name = name;<br>};</pre><pre>new Person(&quot;John&quot;);</pre><p>Throws:</p><pre>TypeError: Person is not a constructor</pre><p>Arrow functions cannot be used with new.</p><h3>When Should You Use Arrow Functions?</h3><p>Use arrow functions for:</p><ul><li>Array methods (map, filter, reduce)</li><li>Callbacks</li><li>Event handlers inside classes</li><li>Closures that need access to the outer this</li></ul><p>Example:</p><pre>setTimeout(() =&gt; {<br>    console.log(&quot;Hello&quot;);<br>}, 1000);</pre><h3>When Should You Avoid Arrow Functions?</h3><p>Avoid them for:</p><ul><li>Object methods</li><li>Constructors</li><li>Prototype methods</li><li>Situations where dynamic this is required</li></ul><p>Example:</p><pre>const user = {<br>    name: &quot;Shubham&quot;,</pre><pre>    greet() {<br>        console.log(this.name);<br>    }<br>};</pre><p>A regular method is the better choice here.</p><h3>Conclusion</h3><p>Arrow functions were not introduced merely to reduce typing.</p><p>Their biggest contribution was solving the long-standing confusion around JavaScript’s this keyword through lexical binding.</p><p>The concise syntax was a bonus. The real innovation was making callbacks and asynchronous code easier to write, easier to understand, and far less error-prone.</p><p>Understanding this design decision helps you know not just how to use arrow functions, but when to use them and when not to.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb13503457f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>