Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript

A Simpler Way to Write Functions

Updated
Arrow Functions in JavaScript

In the previous blog, we discussed the difference between function declaration and function expression. You saw how functions can behave differently depending on how they are defined.

Now we move to something that looks simple on the surface but has deeper implications underneath. Arrow functions.

Most beginners think arrow functions are just a shorter way to write functions. And that is partially true. But if you stop there, you miss the actual point. Arrow functions are not just about syntax. They change how JavaScript handles things like this, arguments, and even function behavior in certain situations. If you understand arrow functions properly, a lot of confusing JavaScript behavior starts making sense.


What Are Arrow Functions?

Arrow functions are a shorter syntax for writing function expressions.

const greet = () => {
  console.log("Hello");
};

At first glance, this looks like a cleaner version of:

const greet = function() {
  console.log("Hello");
};

And for simple cases, they behave the same.

But the difference shows up when things get slightly complex.


Breaking Down the Syntax

Let’s start simple.

Basic Arrow Function

const greet = () => {
  console.log("Hello");
};

With Parameters

const greet = (name) => {
  console.log("Hello " + name);
};

If there is only one parameter, you can skip parentheses:

const greet = name => {
  console.log("Hello " + name);
};

Single Line Functions

If the function has only one statement, you can remove {} and return.

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

This automatically returns the result.

Returning Objects (Common Confusion)

const getUser = () => ({ name: "Shikhar" });

If you don’t wrap the object in parentheses, JavaScript thinks {} is a block, not an object.


Key Difference 1: Arrow Functions Do Not Have Their Own this

This is the most important concept.

Regular functions have their own this. While arrow functions do not. Instead, arrow functions use the this value from their surrounding context.

Example with Regular Function

const user = {
  name: "Shikhar",
  greet: function() {
    console.log(this.name);
  }
};

user.greet(); // Shikhar

Here, this refers to the user object.

Now with Arrow Function

const user = {
  name: "Shikhar",
  greet: () => {
    console.log(this.name);
  }
};

user.greet(); // undefined

Why?

Because arrow functions do not have their own this. They inherit it from the surrounding scope.

In this case, the surrounding scope is not the object, so this.name becomes undefined.

Mental Model for this in Arrow Functions

Think of it like this:

  • Regular function → "I define my own this"

  • Arrow function → "I borrow this from outside"

Once you understand this, a lot of confusion disappears.

Where Arrow Functions Are Actually Useful

Arrow functions are not meant to replace all functions. They are especially useful when you want to preserve the outer this.

Example: Inside Callbacks

function Timer() {
  this.seconds = 0;

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

new Timer();

Here, the arrow function keeps the correct this.

If you used a regular function, this would not refer to the Timer instance.


Key Difference 2: No arguments Object

Regular functions have access to an arguments object.

function test() {
  console.log(arguments);
}

Arrow functions do not.

const test = () => {
  console.log(arguments); // Error
};

Instead, you use rest parameters:

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

Key Difference 3: Cannot Be Used as Constructors

Arrow functions cannot be used with new.

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

const p = new Person("Shikhar"); // Error

Why?

Because arrow functions do not have their own this.


Key Difference 4: No Hoisting Like Declarations

Arrow functions behave like variables.

greet(); // Error

const greet = () => {};

This is because they are function expressions.


When to Use Arrow Functions

Use arrow functions when:

  • You are writing short functions

  • You are using callbacks

  • You want to preserve outer this

  • You want cleaner syntax

When Not to Use Arrow Functions

Avoid arrow functions when:

  • You need your own this

  • You are working with objects and methods

  • You are creating constructors

  • You need the arguments object


Real-World Example

const numbers = [1, 2, 3, 4];

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

console.log(doubled);

This is where arrow functions shine. Short, readable, and expressive.


Comparing Everything Together

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

// Function Expression
const addExp = function(a, b) {
  return a + b;
};

// Arrow Function
const addArrow = (a, b) => a + b;

All three can do the same thing.

But their behavior is different.


Common Mistakes

  1. Using arrow functions as object methods

  2. Forgetting parentheses while returning objects

  3. Expecting this to behave like regular functions

  4. Trying to use arguments


Putting It All Together

function createUser(name) {
  return {
    name,
    greet: function() {
      setTimeout(() => {
        console.log("Hello " + this.name);
      }, 1000);
    }
  };
}

const user = createUser("Shikhar");
user.greet();

Here:

  • Regular function defines method

  • Arrow function preserves this inside callback

This is a very common real-world pattern.


Final Thought

Arrow functions are not just a shorter syntax. They are a different way of thinking about functions. If you try to use them everywhere, you will run into problems.

If you understand where and why to use them, they become one of the most powerful tools in JavaScript.

JavaScript Fundamentals (The Absolute Basics )

Part 5 of 5

“Learn JavaScript from scratch with this beginner-friendly series covering variables, functions, arrays, objects, and core fundamentals with practical examples.”

Start from the beginning

Understanding Variables and Datatypes in Java

Why do we need to understand variables and datatypes in any programming language .