Is there any reason to use const this = () => {} over function (this) {} to call a function?

So i personally just find it easier to remember the function (function) {} method in order to make a function, however codecademy seems to favour the other way. does that way have any advantage over this way?

In some situations one is more useful than the other, they each have their place. It’s good to get used to arrow notation because the important “functional-style” built-in functions are in that form. Oftentimes I find that I’m using arrow notation not where I would write functions, but where I’d use for-loops instead.

For example, for reactivity you often need to change objects. This is because the way arrays (and objects) are handled in js, if you change an element of an array you technically do not change the array itself (the reference to be more precise). So instead of using a for-loop to do a single action to each element, i can just

 const actuallyChangeArray = oldArray.map(element => foo(element));

and be sure that this is a safe transformation.

This is true if you use a framework like React or Vue, and I think Angular is moving towards that style as well. This will generally be true in modern js frameworks past React/Vue/Angular as well.

1 Like