JavaScript  

What are arrow functions and how are they different from regular functions?

📌 Introduction

In JavaScript, functions are like small machines that do specific tasks. Before ES6 (a big update to JavaScript), developers mostly wrote regular functions using the function keyword. ES6 introduced a new type of function called arrow functions, which are shorter and easier to read. They also behave differently in some important ways, especially with how this works. Let’s break them down step by step.

📝 Regular Functions

Regular functions are the traditional way to write functions in JavaScript. They can be created in two main ways:

  • Function Declaration: This is when you write the function with the function keyword and give it a name.
  • Function Expression: This is when you store a function inside a variable.

Example:

// Function Declaration
function add(a, b) {
  return a + b;
}

// Function Expression
const multiply = function(a, b) {
  return a * b;
};

console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20

👉 Regular functions get their own this value (depending on how they are called) and also have access to the special arguments object that stores all function parameters.

⚡ Arrow Functions

Arrow functions were introduced in ES6 to make function writing simpler. They are written using the => (arrow) symbol. They don’t need the function keyword and usually take up less space.

Example:

// Arrow Function (short form)
const add = (a, b) => a + b;

// Arrow Function (long form)
const multiply = (a, b) => {
  return a * b;
};

console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20

👉 Unlike regular functions, arrow functions do not create their own this. Instead, they use the this from the place where they were written. This is called lexical scoping.

🔑 Key Differences Between Arrow and Regular Functions

1. Syntax ✍️

  • Regular functions: Use the function keyword and can be longer to write.
  • Arrow functions: Use the => symbol and are shorter, making code cleaner.

2. this Binding 🧭

  • Regular functions: The value of this depends on how the function is called (it changes based on the context).
  • Arrow functions: Do not have their own this. They take this from the surrounding code where they are written.

3. Arguments Object 📦

  • Regular functions: Have a built-in arguments object that stores all the values passed to the function.
  • Arrow functions: Do not have their own arguments, but you can still use rest parameters (...args) to collect arguments.

4. Constructor Usage 🏗️

  • Regular functions: Can be used as constructors with the new keyword to create objects.
  • Arrow functions: Cannot be used as constructors, and will throw an error if used with new.

📘 Best Practices

  • ✅ Use arrow functions for small tasks, callbacks, and simple one-line functions.
  • ✅ Use regular functions when you need your own this or the arguments object.
  • ⚠️ Avoid using arrow functions inside object methods if you want to use the object’s own this value.

📝 Summary

Arrow functions are a modern way to write functions in JavaScript with shorter syntax and simpler behavior for this. Regular functions, however, are still useful because they support their own this, arguments object, and can be used as constructors. By knowing the differences, developers can choose the right type of function depending on the situation.