# Arrow functions VS Regular functions (scoping of this in function)

<span class="s"></span>

# Arrow functions VS Regular functions (scoping of this in function)


Yesterday I was going thru HackerRank tutorials of 10 Days of Javascript and had stuck at a problem statement.

The problem statement was far too very easy to solve (probably why it was for beginners 😜)

[**Problem Statement**](https://bit.ly/HakerRank-Rectangle) **:**

![Image for post](https://miro.medium.com/max/60/1*Ky0LyReM73vC9eOIwmbJhQ.png?q=20)

<noscript><img alt="Image for post" class="fc ep el io w" src="https://miro.medium.com/max/2278/1*Ky0LyReM73vC9eOIwmbJhQ.png" width="1139" height="531" srcSet="https://miro.medium.com/max/552/1*Ky0LyReM73vC9eOIwmbJhQ.png 276w, https://miro.medium.com/max/1104/1*Ky0LyReM73vC9eOIwmbJhQ.png 552w, https://miro.medium.com/max/1280/1*Ky0LyReM73vC9eOIwmbJhQ.png 640w, https://miro.medium.com/max/1400/1*Ky0LyReM73vC9eOIwmbJhQ.png 700w" sizes="700px"/></noscript>

The problem statement I am referring

At first the problem statement looks very trivial and in fact it is.

If you are not very highly skilled and seasoned JS dev then you would make the same mistake as me 😓.

![Image for post](https://miro.medium.com/max/60/1*Un2fdLGYAaNOoofzGj_JPQ.png?q=20)

<noscript><img alt="Image for post" class="fc ep el io w" src="https://miro.medium.com/max/3640/1*Un2fdLGYAaNOoofzGj_JPQ.png" width="1820" height="1298" srcSet="https://miro.medium.com/max/552/1*Un2fdLGYAaNOoofzGj_JPQ.png 276w, https://miro.medium.com/max/1104/1*Un2fdLGYAaNOoofzGj_JPQ.png 552w, https://miro.medium.com/max/1280/1*Un2fdLGYAaNOoofzGj_JPQ.png 640w, https://miro.medium.com/max/1400/1*Un2fdLGYAaNOoofzGj_JPQ.png 700w" sizes="700px"/></noscript>

Image of the problematic code

You can know your skills if you do/don’t find the above code wrong.

I believe just <span id="rmm"><span id="rmm"></span></span> like me, you know where the problem lies. At one point in your life of js development you have come across this and would just require to be reminded of how functions are scoped.

I soon found out the mistake, when I saw the output of the execution and had console logged “this” as a debugging method, the very **Brahmāstr** (the ultimate weapon) of the developers 😆.

If you are still asking why it is wrong then please let me explain it to you.

<span class="jj gl bn jk jl jm"></span><span class="jj gl bn jk jl jm"></span><span class="jj gl bn jk jl"></span>

# Problem Description

The problem lies in as how the two types of function declaration essentially work, moreover how internally the scoping of functions is done.

## [Arrow Function](https://bit.ly/hacks-arrow-function)

There are a bunch of different ways in which arrow functions differ from normal/regular functions (not only in syntax).

1.  Arrow functions don’t have their own `this`, `arguments`, `super` , `.prototype` .
2.  Arrow functions cannot be used as constructors, hence cannot be used with `new` keyword.
3.  Since arrow function follow normal variable lookup and also they don’t have their own `this`, `this` in arrow function is looked up in the enclosing scope (that is the scope where the arrow function is **called**).
4.  Given that `this` comes from the surrounding lexical context, strict mode rules with regard to `this` are ignored.
5.  Arrow functions cannot be bound to any other scopes, since they don’t have their own bindings of `this` , hence `.apply()`, `.call()`, `.bind()` do not apply (considering their primary usage in concern to `this` ), what they would do is just pass the parameter (ignoring any value passing to `this` ).
6.  The `[yield](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield)` keyword may not be used in an arrow function's body (except when permitted within functions further nested within it). As a consequence, arrow functions cannot be used as generators. (Referenced from MDN)

So what do all this mean in our issue?

> Just for explanation purpose I have saved this in a variable abc and passed it as a parameter to the area function. This helps us to realise that the parameter `_object_` and `_this_` are equal.

![GIF of the working of problematic code](https://miro.medium.com/freeze/max/60/1*GfyD2x6WZYW5H7ew3-Ppbg.gif?q=20)

<noscript><img alt="GIF of the working of problematic code" class="fc ep el io w" src="https://miro.medium.com/max/1600/1*GfyD2x6WZYW5H7ew3-Ppbg.gif" width="800" height="450" srcSet="https://miro.medium.com/max/552/1*GfyD2x6WZYW5H7ew3-Ppbg.gif 276w, https://miro.medium.com/max/1104/1*GfyD2x6WZYW5H7ew3-Ppbg.gif 552w, https://miro.medium.com/max/1280/1*GfyD2x6WZYW5H7ew3-Ppbg.gif 640w, https://miro.medium.com/max/1400/1*GfyD2x6WZYW5H7ew3-Ppbg.gif 700w" sizes="700px"/></noscript>

**Visualizing the problem**

> **TLDR: "** `this` ” in the arrow function is not set as the scope of it’s parent as in regular functions, but is set to the reference of the scope in which it is called. (ie the scope of if block)

<span class="jj gl bn jk jl jm"></span><span class="jj gl bn jk jl jm"></span><span class="jj gl bn jk jl"></span>

# **_SOLUTION_**

## Regular Function

How regular functions work and differ from arrow functions besides the obvious syntactical difference?

1.  Regular functions have their own bindings of `this`, `arguments`, `super` , `.prototype` .
2.  These functions can be used as constructor because they have their own bindings, due to which these can be used with the `new` keyword.
3.  `this` in context to function body, have a little different behavior when used in `strict` mode as of that when used not used in strict mode.
4.  When not used in `strict` mode then the value of `this` of function is set to the the context in which the function is declared in. That is, if a function is called as a method of an object, its `this` is set to the object the method is called on. (Didn’t understand ? Refer the codes below extracted from MDN).

## [**Gist**](https://gist.github.com/Omkaragrawal/be908395f2a273606c08b16178091e55)

![Example 1 without using strict mode](https://miro.medium.com/max/60/1*Er6XJxhhT7TXaJ7njb2_ZQ.png?q=20)

<noscript><img alt="Example 1 without using strict mode" class="fc ep el io w" src="https://miro.medium.com/max/1364/1*Er6XJxhhT7TXaJ7njb2_ZQ.png" width="682" height="552" srcSet="https://miro.medium.com/max/552/1*Er6XJxhhT7TXaJ7njb2_ZQ.png 276w, https://miro.medium.com/max/1104/1*Er6XJxhhT7TXaJ7njb2_ZQ.png 552w, https://miro.medium.com/max/1280/1*Er6XJxhhT7TXaJ7njb2_ZQ.png 640w, https://miro.medium.com/max/1364/1*Er6XJxhhT7TXaJ7njb2_ZQ.png 682w" sizes="682px"/></noscript>

![Example 2 without using strict mode](https://miro.medium.com/max/60/1*pgnPDw4Oicj4ToK7xe1TDg.png?q=20)

<noscript><img alt="Example 2 without using strict mode" class="fc ep el io w" src="https://miro.medium.com/max/1268/1*pgnPDw4Oicj4ToK7xe1TDg.png" width="634" height="514" srcSet="https://miro.medium.com/max/552/1*pgnPDw4Oicj4ToK7xe1TDg.png 276w, https://miro.medium.com/max/1104/1*pgnPDw4Oicj4ToK7xe1TDg.png 552w, https://miro.medium.com/max/1268/1*pgnPDw4Oicj4ToK7xe1TDg.png 634w" sizes="634px"/></noscript>

5\. When a function is used as a constructor (with the `new` keyword), its `this` is bound to the new object being constructed.

6\. When used in strict mode then the value of `this` of function is set to `undefined` . (Didn’t understand? Refer the code below extracted from MDN).

> **Note:** That if a function is called as a method of an object, its `this` is set to the object the method is called on even though it is in strict mode.

## [Gist](https://gist.github.com/Omkaragrawal/7865bcdcd50b6c4bfcd45b053fb518ca)

![Image for post](https://miro.medium.com/max/60/1*HnF5-2WUDRKicYJSkTZxXg.png?q=20)

<noscript><img alt="Image for post" class="fc ep el io w" src="https://miro.medium.com/max/1700/1*HnF5-2WUDRKicYJSkTZxXg.png" width="850" height="478" srcSet="https://miro.medium.com/max/552/1*HnF5-2WUDRKicYJSkTZxXg.png 276w, https://miro.medium.com/max/1104/1*HnF5-2WUDRKicYJSkTZxXg.png 552w, https://miro.medium.com/max/1280/1*HnF5-2WUDRKicYJSkTZxXg.png 640w, https://miro.medium.com/max/1400/1*HnF5-2WUDRKicYJSkTZxXg.png 700w" sizes="700px"/></noscript>

7\. Value of `this` can be custom set when calling the function by using `.apply()`, `.call()` .
(Didn’t understand? Refer the codes below)

> **Note:** That in non–strict mode, with call and apply, if the value passed as this is not an object, an attempt will be made to convert it to an object using the internal toObject operation. So if the value passed is a primitive like 7 or ‘foo’, it will be converted to an Object using the related constructor, number 7 is converted by new Number(7) and the string ‘foo’ by new String(‘foo’).

## [**Gist**](https://gist.github.com/Omkaragrawal/c6d8de530cb9d5011938bb3cd07cafb3)

![Example 1 for .call() and .apply() methods](https://miro.medium.com/max/60/1*K2yTcdSSQo6A7WO-i3zFzA.png?q=20)

<noscript><img alt="Example 1 for .call() and .apply() methods" class="fc ep el io w" src="https://miro.medium.com/max/3568/1*K2yTcdSSQo6A7WO-i3zFzA.png" width="1784" height="776" srcSet="https://miro.medium.com/max/552/1*K2yTcdSSQo6A7WO-i3zFzA.png 276w, https://miro.medium.com/max/1104/1*K2yTcdSSQo6A7WO-i3zFzA.png 552w, https://miro.medium.com/max/1280/1*K2yTcdSSQo6A7WO-i3zFzA.png 640w, https://miro.medium.com/max/1400/1*K2yTcdSSQo6A7WO-i3zFzA.png 700w" sizes="700px"/></noscript>

![Example 2 for .call() and .apply() methods](https://miro.medium.com/max/60/1*HGTGmAs0NeUsKNkFPXiM-A.png?q=20)

<noscript><img alt="Example 2 for .call() and .apply() methods" class="fc ep el io w" src="https://miro.medium.com/max/2120/1*HGTGmAs0NeUsKNkFPXiM-A.png" width="1060" height="720" srcSet="https://miro.medium.com/max/552/1*HGTGmAs0NeUsKNkFPXiM-A.png 276w, https://miro.medium.com/max/1104/1*HGTGmAs0NeUsKNkFPXiM-A.png 552w, https://miro.medium.com/max/1280/1*HGTGmAs0NeUsKNkFPXiM-A.png 640w, https://miro.medium.com/max/1400/1*HGTGmAs0NeUsKNkFPXiM-A.png 700w" sizes="700px"/></noscript>

8\. Using `.bind()` creates a new function which which has same body and scope as the function it is called upon but the value of `this` is permanently set to the value of parameter of `.bind()` and then returns the new function. (One more point in the example below, look for examples below)

## [**Gist**](https://gist.github.com/Omkaragrawal/35b2aea42e351f87923bb679aaa3436d)

![Image for post](https://miro.medium.com/max/60/1*GVO52G8gysSSPV-sqYbCNw.png?q=20)

<noscript><img alt="Image for post" class="fc ep el io w" src="https://miro.medium.com/max/1856/1*GVO52G8gysSSPV-sqYbCNw.png" width="928" height="720" srcSet="https://miro.medium.com/max/552/1*GVO52G8gysSSPV-sqYbCNw.png 276w, https://miro.medium.com/max/1104/1*GVO52G8gysSSPV-sqYbCNw.png 552w, https://miro.medium.com/max/1280/1*GVO52G8gysSSPV-sqYbCNw.png 640w, https://miro.medium.com/max/1400/1*GVO52G8gysSSPV-sqYbCNw.png 700w" sizes="700px"/></noscript>

9\. A function used as getter or setter has its `this` bound to the object from which the property is being set or gotten. (Didn’t understand? refer to the code below from MDN).

## [**Gist**](https://gist.github.com/Omkaragrawal/cd6b694aecfaec330dd050e01235ed61)

![Image for post](https://miro.medium.com/max/60/1*iMYsRPObNbwhu5LygesqIA.png?q=20)

<noscript><img alt="Image for post" class="fc ep el io w" src="https://miro.medium.com/max/2100/1*iMYsRPObNbwhu5LygesqIA.png" width="1050" height="850" srcSet="https://miro.medium.com/max/552/1*iMYsRPObNbwhu5LygesqIA.png 276w, https://miro.medium.com/max/1104/1*iMYsRPObNbwhu5LygesqIA.png 552w, https://miro.medium.com/max/1280/1*iMYsRPObNbwhu5LygesqIA.png 640w, https://miro.medium.com/max/1400/1*iMYsRPObNbwhu5LygesqIA.png 700w" sizes="700px"/></noscript>

> TLDR: The value of “ `this` “ of function is set to the the context in which the function is called in. That is if a function is called as a method of an object, its “ `this` “ is set to the object the method is called on.

![GIF of working of the solution code](https://miro.medium.com/freeze/max/60/1*RZsaKhjXYlBahqTQq0isXw.gif?q=20)

<noscript><img alt="GIF of working of the solution code" class="fc ep el io w" src="https://miro.medium.com/max/1600/1*RZsaKhjXYlBahqTQq0isXw.gif" width="800" height="450" srcSet="https://miro.medium.com/max/552/1*RZsaKhjXYlBahqTQq0isXw.gif 276w, https://miro.medium.com/max/1104/1*RZsaKhjXYlBahqTQq0isXw.gif 552w, https://miro.medium.com/max/1280/1*RZsaKhjXYlBahqTQq0isXw.gif 640w, https://miro.medium.com/max/1400/1*RZsaKhjXYlBahqTQq0isXw.gif 700w" sizes="700px"/></noscript>

Working of the solution code![Image of the solution code](https://miro.medium.com/max/60/1*Mi8BvGDK_umJxSJZElmw3w.png?q=20)

<noscript><img alt="Image of the solution code" class="fc ep el io w" src="https://miro.medium.com/max/3644/1*Mi8BvGDK_umJxSJZElmw3w.png" width="1822" height="1374" srcSet="https://miro.medium.com/max/552/1*Mi8BvGDK_umJxSJZElmw3w.png 276w, https://miro.medium.com/max/1104/1*Mi8BvGDK_umJxSJZElmw3w.png 552w, https://miro.medium.com/max/1280/1*Mi8BvGDK_umJxSJZElmw3w.png 640w, https://miro.medium.com/max/1400/1*Mi8BvGDK_umJxSJZElmw3w.png 700w" sizes="700px"/></noscript>

Solution for the problem

This is where I end my article. Any suggestions are most welcome.

Contact me on:

*   [Linkedin](https://bit.ly/omkaragrawal-linkedin)
*   [Github](https://bit.ly/omkaragrawal-github)
*   [Twitter](https://bit.ly/omkaragrawal-twitter)
*   [Facebook](https://bit.ly/omkaragrawal-facebook)
