Skip to content
YouTurkey Tech
All JavaScript topics

Arrow vs Regular Functions

Intermediate

Why an arrow function takes this from the code around it while a regular function gets its own from each call, and what else arrows leave out: arguments, new and hoisting.Builds on The this Keyword

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/18Press Play, or step through with the arrows, to begin.
Step 0 of 18

Explanation

3 min read

On this page

What is the difference between arrow and regular functions in JavaScript?

An arrow function, (n) => n * 2, is a shorter way to write a function, but the difference goes beyond syntax. A regular function (a declaration, function expression or method) gets several things from each call. An arrow function gets none of them and uses those of the code around it:

  • this: a regular function gets its own on every call; an arrow function uses the this of the place it was written.
  • arguments: a regular function has an arguments object; an arrow function does not, so use rest parameters (...args).
  • new: a regular function can be a constructor; an arrow function has no prototype and calling it with new throws a TypeError.
  • Hoisting: a function declaration can be called before its line. An arrow function is always an expression, usually stored in a const, so it stays in the TDZ until its line runs.

Arrow functions also have a concise body: (n) => n * 2 returns n * 2 without return.

How does an arrow function decide what this is?

It doesn't decide: it looks this up, the way it looks up a variable. When an arrow function needs this, JavaScript goes out through the scopes where the arrow was written until it reaches a regular function (or the top level), and uses that function's this for the current call.

  • The answer depends on where the arrow is written, not how it is called. obj.arrow(), arrow.call(obj) and arrow.bind(obj) all leave its this unchanged.
  • That makes arrows the natural choice for callbacks inside a method (forEach, setTimeout, then): they see the method's this without bind or const self = this.
  • Regular functions remain the right choice for methods, constructors, and handlers that need this to be the element.

What mistakes do people make with arrow functions?

Mini example

const clock = {  ticks: 0,  start() {    // The arrow borrows `this` from start(), so it is clock.    setInterval(() => {      this.ticks++;    }, 1000);  },};clock.start(); const sum = (...numbers) => numbers.reduce((total, n) => total + n, 0);console.log(sum(1, 2, 3)); // 6

Quick check

What happens when you run new (() => {})()?

Further reading

All steps as text
  1. Step 1:

    The script runs in strict mode, as modules and classes always do. The global context starts.

  2. Step 2:

    Creation phase: team and Arrow are consts, so both wait in the TDZ. An arrow function in a const cannot be called before its line, unlike a function declaration.

    Lines 3–20

  3. Step 3:

    The team object is created with two methods, regular and arrow.

    Lines 3–14

  4. Step 4:

    team.regular() is called through team., so its this is team.

    Line 16

  5. Step 5:

    A regular function expression is created and stored in inner.

    Lines 5–7

  6. Step 6:

    inner() has nothing before the dot. A regular function always gets its own this from the call: here undefined, in strict mode.

    Line 8

  7. Step 7:

    return this looks up this. inner has its own, so the search stops right there, at undefined.

    Line 6

  8. Step 8:

    inner returns undefined. The team that regular() was called on never reached it.

    Lines 6–8

  9. Step 9:

    regular() returns undefined, so false is printed. Its inner function is no longer reachable and is freed.

    Line 16

  10. Step 10:

    team.arrow() is also called through team., so its this is team too.

    Line 17

  11. Step 11:

    This time inner holds an arrow function, written inside arrow().

    Line 11

  12. Step 12:

    inner() is called the same way, but an arrow function gets no this from the call. Its frame has none.

    Line 12

  13. Step 13:

    So this is looked up like a variable: out to arrow(), where the arrow was written. There this is team.

    Line 11

  14. Step 14:

    inner returns team, borrowed from arrow().

    Lines 11–12

  15. Step 15:

    arrow() returns team, so true is printed. Its inner function is freed.

    Line 17

  16. Step 16:

    Arrow gets an arrow function and leaves the TDZ.

    Line 19

  17. Step 17:

    An arrow function cannot be a constructor: it has no prototype and no this of its own. new throws a TypeError.

    Line 20

  18. Step 18:

    A regular function gets its this from how it is called. An arrow function has none, so it uses the this of the code it was written in.