CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

🧭 Arrow Functions vs Function Declarations in JavaScript

Arrow functions and regular functions differ in how they bind this context. Understanding this distinction is crucial for callbacks, methods, and event handlers.

Why and not an arrow function? Here's the full breakdown. -- 1️⃣ Introduction JavaScript offers multiple ways to define functions: Function Declarations: Function Expressions: Arrow Functions: While arrow functions offer brevity and clarity, they do not have their own , which is why choosing between the two depends entirely on your use case. -- 2️⃣ The Key Difference: Binding Function Expressions Function expressions define their own context depending on how they're called: Arrow Functions Arrow functions lexically bind , meaning they inherit from the surrounding context: πŸ“Œ Why Not Use Arrow in ? Because refers to the object instance ( in this case), and the fire method needs to explicitly bind or use the current instance as context. If you used an arrow function, it would ignore and would not refer to the instance or a provided scope β€” breaking functionality like : -- 3️⃣ When to Use Which Prefer Function βœ… Yes βœ… Yes βœ… Yes βœ… Usually ❌ Overhead -- 4️⃣ Real Examples βœ… Good Use of Arrow: βœ… Good Use of Function Declaration: ⚠️ Misuse of Arrow in Object Method: -- 5️⃣ Interview Q&A Q1: Why don’t arrow functions have their own ? A: Because arrow functions are designed for lexical scoping. They inherit from the context in which they are defined, rather than from how they are called. Q2: When would using an arrow function inside a class break things? A: When the method depends on referring to the class instance. Arrow functions don’t have their own , so they won’t work properly in instance methods. Q3: Can you replace all function expressions with arrow functions? A: No. If the function needs a dynamic or context-based , you must use a traditional function. Q4: How does or behave with arrow functions? A: It has no effect. You cannot change the value of an arrow function with or . -- πŸ”š Conclusion Use function declarations or expressions when you rely on dynamic , such as in methods or callbacks that depend on object context. Use arrow functions for everything else β€” especially in callbacks, map/reduce logic, and stateless utilities. -- <!-quiz-start --Q1: What does refer to inside an arrow function? [ ] The object that called the function [ ] The global object (window) [x] The value from the enclosing lexical scope [ ] undefined always Q2: Can you change the value of an arrow function using or ? [ ] Yes, just like regular functions [x] No, arrow functions ignore explicit binding [ ] Only with , not [ ] Only in strict mode Q3: Which scenario is best suited for arrow functions? [ ] Object methods that need to access [ ] Event handlers that need to reference the clicked element via [x] Callbacks inside methods like , , or [ ] Constructor functions <!-quiz-end --
JavaScriptCore Concepts
πŸ›‘ AbortController: Canceling Async Operations in JavaScript
medium
πŸ”’ Closures in JavaScript β€” The Complete Guide
hard
πŸ“¦ Understanding ES6 Modules in JavaScript
medium
⚑ JavaScript Event Loop: Complete Guide to Asynchronous Execution
hard
🧭 Arrow Functions vs Function Declarations in JavaScript
easy
πŸ—‘οΈ Garbage Collection in JavaScript β€” Memory Management & Leak Prevention
hard
πŸ—οΈ Constructor Functions in JavaScript
medium
πŸ‘οΈ MutationObserver: Watching DOM Changes in JavaScript
medium
πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive
hard
πŸ”— Prototype and Prototype Inheritance in JavaScript
medium
πŸ•΅οΈ What Are Proxies in JavaScript? (With Practical Use Cases)
medium
🎯 Scope in JavaScript β€” The Complete Guide
hard
πŸ”„ Script Loading: async vs defer vs Both
hard
πŸ“€ JavaScript Spread Operator (...) Explained
easy
🎯 The JavaScript `this` Keyword: Complete Guide to Context Binding
medium
5 of 15
LibraryJavaScriptCore Concepts5 of 61

🧭 Arrow Functions vs Function Declarations in JavaScript

jsgeneral-conceptseasy

Why this.fire = function(...) and not an arrow function? Here's the full breakdown.


1️⃣ Introduction

JavaScript offers multiple ways to define functions:

  • Function Declarations: function greet() {}
  • Function Expressions: const greet = function() {}
  • Arrow Functions: const greet = () => {}

While arrow functions offer brevity and clarity, they do not have their own this , which is why choosing between the two depends entirely on your use case.


2️⃣ The Key Difference: this Binding

Function Expressions

Function expressions define their own this context depending on how they're called:

const obj = {
  count: 0,
  increment: function () {
    console.log(this); // refers to `obj`
  },
};

Arrow Functions

Arrow functions lexically bind this , meaning they inherit this from the surrounding context:

const obj = {
  count: 0,
  increment: () => {
    console.log(this); // refers to global object (or undefined in strict mode)
  },
};

πŸ“Œ Why Not Use Arrow in this.fire = function (...)?

Because this refers to the object instance (Move in this case), and the fire method needs to explicitly bind thisObj or use the current instance as context.

If you used an arrow function, it would ignore thisObj and this would not refer to the Move instance or a provided scope β€” breaking functionality like .call():

this.fire = (o, thisObj) => {
  const scope = thisObj || window;
  // `this` here is lexically bound and can't be overridden with `.call()`
  this.handlers.forEach((item) => item.call(scope, o)); // ❌ unpredictable
};

3️⃣ When to Use Which

Use CasePrefer FunctionPrefer Arrow Function
Needs dynamic thisbindingβœ… Yes❌ No
Callback inside methodβœ… Yesβœ… Yes (if no thisdependency)
Inside classes or object methodsβœ… Yes❌ No (unless explicitly static)
Event handlersβœ… Usually❌ Only if you bind explicitly
Simple one-liners / pure funcs❌ Overheadβœ… Perfect match

4️⃣ Real Examples

βœ… Good Use of Arrow:

const numbers = [1, 2, 3];
const squares = numbers.map(n => n * n);

βœ… Good Use of Function Declaration:

class Timer {
  constructor() {
    this.seconds = 0;
    setInterval(function () {
      this.seconds++; // `this` needs to refer to Timer β€” so we bind
    }.bind(this), 1000);
  }
}

⚠️ Misuse of Arrow in Object Method:

const counter = {
  value: 0,
  increment: () => {
    this.value++; // ❌ `this` is not `counter`
  },
};

5️⃣ Interview Q&A

Q1: Why don’t arrow functions have their own this?

A: Because arrow functions are designed for lexical scoping. They inherit this from the context in which they are defined, rather than from how they are called.

Q2: When would using an arrow function inside a class break things?

A: When the method depends on this referring to the class instance. Arrow functions don’t have their own this, so they won’t work properly in instance methods.

Q3: Can you replace all function expressions with arrow functions?

A: No. If the function needs a dynamic or context-based this, you must use a traditional function.

Q4: How does .call() or .apply() behave with arrow functions?

A: It has no effect. You cannot change the this value of an arrow function with .call() or .apply().


πŸ”š Conclusion

Use function declarations or expressions when you rely on dynamic this, such as in methods or callbacks that depend on object context. Use arrow functions for everything else β€” especially in callbacks, map/reduce logic, and stateless utilities.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `this` refer to inside an arrow function?
Q2Can you change the `this` value of an arrow function using `.call()` or `.apply()`?
Q3Which scenario is best suited for arrow functions?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna