Arrow vs Regular Functions
IntermediateWhy 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
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
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 thethisof the place it was written.arguments: a regular function has anargumentsobject; an arrow function does not, so use rest parameters (...args).new: a regular function can be a constructor; an arrow function has noprototypeand calling it withnewthrows aTypeError.- 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)andarrow.bind(obj)all leave itsthisunchanged. - That makes arrows the natural choice for callbacks inside a method (
forEach,setTimeout,then): they see the method'sthiswithoutbindorconst self = this. - Regular functions remain the right choice for methods, constructors, and handlers that need
thisto 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)); // 6Quick check
What happens when you run new (() => {})()?
Further reading
All steps as text
- Step 1:
The script runs in strict mode, as modules and classes always do. The global context starts.
- Step 2:
Creation phase:
teamandArroware 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
- Step 3:
The
teamobject is created with two methods,regularandarrow.Lines 3–14
- Step 4:
team.regular()is called throughteam., so itsthisis team.Line 16
- Step 5:
A regular function expression is created and stored in
inner.Lines 5–7
- Step 6:
inner()has nothing before the dot. A regular function always gets its ownthisfrom the call: here undefined, in strict mode.Line 8
- Step 7:
return thislooks upthis. inner has its own, so the search stops right there, at undefined.Line 6
- Step 8:
inner returns undefined. The team that
regular()was called on never reached it.Lines 6–8
- Step 9:
regular()returns undefined, so false is printed. Itsinnerfunction is no longer reachable and is freed.Line 16
- Step 10:
team.arrow()is also called throughteam., so itsthisis team too.Line 17
- Step 11:
This time
innerholds an arrow function, written insidearrow().Line 11
- Step 12:
inner()is called the same way, but an arrow function gets nothisfrom the call. Its frame has none.Line 12
- Step 13:
So
thisis looked up like a variable: out toarrow(), where the arrow was written. Therethisis team.Line 11
- Step 14:
inner returns team, borrowed from
arrow().Lines 11–12
- Step 15:
arrow()returns team, so true is printed. Itsinnerfunction is freed.Line 17
- Step 16:
Arrowgets an arrow function and leaves the TDZ.Line 19
- Step 17:
An arrow function cannot be a constructor: it has no
prototypeand nothisof its own.newthrows a TypeError.Line 20
- Step 18:
A regular function gets its
thisfrom how it is called. An arrow function has none, so it uses thethisof the code it was written in.