Skip to content
FunctionsChapter 14 / 47Beginner5 min read

Arrow Functions

Learn JavaScript arrow function syntax, implicit return, and how they differ from normal functions, especially the behavior of this.

On this page

1. What are arrow functions?

Arrow functions arrived in ES2015 so that small functions, especially callbacks, could be written in fewer words. They are usually stored in a variable, like const add = (a, b) => a + b.

2. Why does it matter?

In callbacks, writing the function keyword and return again and again makes code long. On top of that, this was a problem in older code.

Normal function callbackArrow function callback
The function keyword and return every timeOne line, implicit return
this changed inside the callbackYou get the outer this
A workaround like const self = thisNo workaround needed
Long map and filter chainsShort, clean chains

3. Where is it used?

AreaExample
Array methodsprices.map((p) => p * 2)
Event handlersbutton.addEventListener("click", () => save())
Promisesfetch(url).then((res) => res.json())
ReactonClick={() => setCount(count + 1)}
TimerssetTimeout(() => showToast(), 2000)

4. How does it work?

The syntax of an arrow function has three parts: parameters, =>, and the body. If the body is a single expression, it is returned automatically.

From a normal function to an arrow
function (a, b) return a + b
Remove the function keyword, put an arrow in between
(a, b) arrow, return a + b in curly braces
If it is one expression, remove the braces and return too
(a, b) arrow a + b
FormMeaning
() => 42No parameters, parentheses are required
n => n * 2One parameter, parentheses are optional
(a, b) => a + bSeveral parameters, implicit return
(a, b) => { return a + b; }Block body, you must write return yourself
() => ({ id: 1 })Parentheses to return an object

5. Code example

Short syntax and implicit return

js
const add = (a, b) => a + b;
const double = (n) => n * 2;
const toUser = (name) => ({ name, createdAt: Date.now() });

const prices = [100, 250, 400];
const withTax = prices.map((p) => Math.round(p * 1.17));

console.log(add(2, 3));
console.log(withTax);
console.log(toUser("Ali").name);

The difference in this

js
const timer = {
  seconds: 0,
  start() {
    setInterval(() => {
      this.seconds += 1;
      console.log(this.seconds);
    }, 1000);
  },
};

timer.start();

How does the code work?

ItemMeaning
(a, b) => a + bAn expression body, a + b is returned automatically
({ name, ... })Without the parentheses, the braces would be taken as a block
prices.map((p) => ...)The arrow callback runs on every price
start()A normal method, its this is the timer object
setInterval(() => ...)The arrow took this from start, so this.seconds works

Interview tip

Arrow functions have no this, arguments or prototype of their own, and you cannot call them with new. So for object methods and constructors, use a normal function or method syntax. Details: The this Keyword.

6. Common mistakes

Mistake: Making an object method an arrow function

Why? An arrow's this is the outer one, so this.name is undefined and you do not get the object.

Better: For object methods, write method syntax like greet() and use this inside.

Mistake: Forgetting parentheses when returning an object

Why? In () => { id: 1 }, the braces are taken as the function body, and the result is undefined.

Better: Wrap the object in parentheses: () => ({ id: 1 }).

Mistake: Not writing return in a block body

Why? As soon as you add curly braces, the implicit return is gone, and the result of map fills up with undefined values.

Better: If you use braces, write return yourself, otherwise remove the braces and keep one expression.

Mistake: Forcing arrow functions everywhere

Why? Arrows do not work when an event listener needs the element through this, or when you need a constructor.

Better: Arrows for callbacks, normal functions for methods and places that need this.

7. What to learn before and after

Suggested order
Functions
Arrow Functions
Scope & Hoisting
The this Keyword
Array Methods

Final quick revision

PointDetail
Syntax(params) => expression
Implicit returnOnly in a body without braces
Object return() => ({ ... })
thisComes from the outer scope
When not to useObject methods, constructors, when you need arguments