DEV Community

Roman Moravcik
Roman Moravcik

Posted on

Arrow Function is not Replacement For function(){}

I have seen so many low effort articles that pretend to teach you about arrow functions and then they end up just show you how function was written before ES6 and then glorified new arrow function.

function(x) { return x }  // regarded as old 
x => x // hip new function in ES6
Enter fullscreen mode Exit fullscreen mode

It is so frustrating, because you can get wrong impression that there is no difference in how they work.

I am making this post just so people are aware that there are more differences between these two functions and they should take deeper look into documentation.

MDN

Top comments (2)

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

There are differences. The only (important) ones are this and related ones like call, apply, super, etc. not existing right?

Collapse
 
shyclown profile image
Roman Moravcik

I think my main point is only that you can not always use arrow function, and it should be presented as different kind of function and not replacement... and people should evaluate if its possible.

As you pointed out the arrow function does not have its own scope, prototypes... and that is why you can not use it as constructor or have "new SomeArrowFunction()".

In many cases you can not change type of function without changes to code inside function.
-> for example when using d3 library you need to use function(){}, when you need this to refer to element.