Why Arrow Functions Were Introduced

Taher MaimoonTaher Maimoon
2 min read

Arrow functions were not introduced to replace regular functions β€” they were added to solve specific problems that arose with this and function expressions in functional-style programming and callbacks.


πŸ”‘ Key Reasons for Arrow Functions

1. βœ… Lexical this binding

In many cases, especially with event handlers, callbacks, or setTimeout, regular functions caused problems with this.

🧱 Problem with this in regular functions:

function Timer() {
  this.seconds = 0;
  setInterval(function () {
    this.seconds++;
    console.log(this.seconds); // ❌ NaN or undefined
  }, 1000);
}
  • Here, this inside setInterval refers to the window object, not the Timer instance.

βœ… Arrow functions fix this:

function Timer() {
  this.seconds = 0;
  setInterval(() => {
    this.seconds++;
    console.log(this.seconds); // βœ… Works as expected
  }, 1000);
}
  • Arrow functions capture this from the surrounding context (lexical scoping), solving a long-standing headache.

2. βœ… Shorter syntax for inline functions

Functional programming patterns use a lot of small functions like:

const doubled = arr.map(function(n) {
  return n * 2;
});

With arrow functions:

const doubled = arr.map(n => n * 2);

It’s more concise, especially for one-liners.


3. βœ… Better for callbacks / promises

In chaining or async code, they reduce clutter:

fetch('/api')
  .then(res => res.json())
  .then(data => console.log(data));

This would be unnecessarily verbose with traditional functions.


🚫 The Downsides

Arrow functions:

  • ❌ Can make this confusing when used inside objects.

  • ❌ Reduce readability for beginners.

  • ❌ Are not suitable for object methods.

  • ❌ Have no arguments object, which sometimes matters.


🧠 Summary

FeatureArrow FunctionRegular Function
this bindingLexical (from context)Dynamic (based on caller)
Short syntaxβœ… Yes❌ No
Use as object method❌ No (avoid)βœ… Yes
Use in callbacks/promisesβœ… Recommendedβœ… Also fine, but wordier
Has arguments object❌ Noβœ… Yes

πŸ’¬ Final Thought

Arrow functions should not be used blindly.
Use them:

  • For callbacks, short functions, and functional programming patterns.

  • Avoid them for object methods where you rely on this.

In short: arrow functions are a tool, not a replacement.

0
Subscribe to my newsletter

Read articles from Taher Maimoon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Taher Maimoon
Taher Maimoon