📌 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:

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 ✍️

2. this Binding 🧭

3. Arguments Object 📦

4. Constructor Usage 🏗️

📘 Best Practices

📝 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.