What is an arrow function?

Arrow functions, introduced in ES6. An arrow function is a shorter way to write JavaScript functions.

Vivek Rastogi

8/20/20251 min read

yellow and white rectangular frame
yellow and white rectangular frame
Why do we use arrow functions?
  1. Shorter syntax – Less typing, cleaner code.

  2. Automatic this binding – Arrow functions don’t have their own this.

    • In normal functions, this changes based on how you call the function.

    • In arrow functions, this stays the same as the surrounding context.

Instead of writing:

function greet(name) {

return "Hello, " + name;

}

You can write:

const greet = (name) => {

return "Hello, " + name;

};

Advantages
  1. Cleaner and more readable code

// Regular function

const numbers = [1, 2, 3];

const squares = numbers.map(function(n) {

return n * n;

});

// Arrow function

const squares = numbers.map(n => n * n);