Ticker

6/recent/ticker-posts

➡️ Arrow Functions in JavaScript – Small Syntax, Big Behavior Change

➡️ Arrow Functions in JavaScript – Small Syntax, Big Behavior Change

At first glance, arrow functions look like just shorter syntax.

Like:

const add = (a, b) => a + b;

Nice… clean… less typing.

But here’s the thing 👇

👉 Arrow functions are not just shorter functions
👉 They behave differently in ways that will break your code if you don’t get it

Let’s talk about how they actually work in real-world JavaScript.

⚡ What Makes Arrow Functions Different?

The biggest difference isn’t syntax.

👉 It’s this

Arrow functions:

  • Don’t have their own this
  • They inherit this from the surrounding scope

That sounds small… but it changes everything.

🔎 Basic Syntax (Quick Recap)

const greet = (name) => {
  return `Hello ${name}`;
};

Short version:

const greet = name => `Hello ${name}`;

No issues here. Pretty straightforward.

🔥 Real Use Case: Callbacks (Where They Shine)

Arrow functions are perfect for callbacks.

const numbers = [1, 2, 3];

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

console.log(doubled);

Why this works well:

👉 No need to worry about this
👉 Cleaner, more readable

This is where arrow functions really shine.

💡 Real Developer Insight

Here’s something every dev runs into at some point:

❌ Using regular function in async callback

function Timer() {
  this.seconds = 0;

  setInterval(function () {
    this.seconds++;
    console.log(this.seconds);
  }, 1000);
}

👉 This breaks.

Why?

Because this inside the function is not the instance.

function Timer() {
  this.seconds = 0;

  setInterval(() => {
    this.seconds++;
    console.log(this.seconds);
  }, 1000);
}

👉 Now this works correctly.

This is one of the biggest real-world uses of arrow functions.

⚠️ Where Arrow Functions Will Bite You

1. ❌ Not good for object methods

const Person = (name) => {
  this.name = name;
};

new Person("John"); // ❌ error

👉 this is not the object.

2. ❌ Cannot be used as constructors

const Person = (name) => {
  this.name = name;
};

new Person("John"); // ❌ error

Arrow functions don’t have prototype.

3. ❌ No arguments object

const test = () => {
  console.log(arguments); // ❌ not available
};

Use rest params instead:

const test = (...args) => console.log(args);

🧠 When You Should Use Arrow Functions

Use them when:

  • Writing small utility functions
  • Working with array methods (map, filter, reduce)
  • Handling callbacks
  • You want lexical this

Avoid them when:

  • Defining object methods
  • Creating constructors
  • When dynamic this is required

⚠️ Common Developer Mistakes

Using arrow functions everywhere blindly
→ leads to this bugs

Using them inside object methods
→ unexpected undefined values

Forgetting implicit return rules

() => { value: 10 } // ❌ undefined

Correct:

() => ({ value: 10 })

🚀 Best Practice Summary

✅ Use arrow functions for callbacks and short functions
✅ Use arrow functions when you need lexical this
✅ Avoid arrow functions for object methods
✅ Avoid arrow functions for constructors
✅ Use rest parameters instead of arguments

Reactions

Post a Comment

0 Comments