Arrow Functions vs Regular Functions

Difficulty: Beginner

Question

What are arrow functions in JavaScript, and how do they differ from regular functions?

Answer

Arrow functions (ES2015) are a shorter function syntax, (a, b) => a + b, that also behave differently from regular functions. Most importantly, an arrow function has no this of its own: it uses the this of the surrounding scope at the point where it is defined. Arrows also have no arguments object, cannot be used as constructors with new, and have no prototype property.

Syntax shortcuts

The real difference: lexical this

A regular function's this is decided by how it is called (obj.method(), new Fn(), fn.call(x), or a plain call). An arrow function's this is fixed when it is created and copied from the enclosing scope. call, apply and bind cannot change it.

This is why arrows are ideal for callbacks inside methods. Before ES2015, people wrote var self = this or .bind(this) to keep access to the object inside a setTimeout or forEach. With an arrow, the callback simply sees the method's this.

Side-by-side comparison

FeatureRegular functionArrow function
thisSet by the call siteInherited from enclosing scope
argumentsAvailableNot available; use ...args
newAllowed (function declarations/expressions)TypeError
prototype propertyYesNo
Hoisting (declarations)Fully hoistedBehaves like the variable it is assigned to
Duplicate parameter names in sloppy modeAllowedSyntaxError
Can be a generatorYes (function*)No

When not to use an arrow

Where arrows shine

Array callbacks (items.map(i => i.price)), promise chains, React function components and handlers, and class fields like handleClick = () => { ... } in class components, which give each instance a bound handler. Note that class-field arrows are created per instance, so they cost a little more memory than prototype methods.

Arrow functions and this. An arrow has no this of its own. Try other call styles: none of them can change it.
  1. Arrow check comes first: If the function is an arrow, this is taken from the scope where it was written.
  2. call, apply and bind are ignored: Passing admin to .call() does not change an arrow's this.
  3. new throws: Arrows are not constructors, so new on an arrow is a TypeError.
  4. Regular functions differ: A function expression gets this from the call site: method call, explicit binding or the default rule.

Code examples

Lexical this in callbacks vs methods

const timer = {
  seconds: 0,
  start() {
    // arrow inherits this from start(), which is timer
    [1, 2, 3].forEach(() => { this.seconds++; });
    return this.seconds;
  },
  broken: () => typeof this, // arrow as a method: this is NOT timer
};

console.log(timer.start());
console.log(timer.broken());

Inside start, the arrow callback sees timer as this. The broken arrow method takes this from the module scope instead (module.exports in CommonJS, undefined in ESM), so it never refers to timer.

arguments, new and object literals

function regular() { return arguments.length; }
const arrow = (...args) => args.length;   // use rest instead of arguments
console.log(regular(1, 2, 3), arrow(1, 2, 3));

const makeUser = (name) => ({ name, active: true }); // parens around object
console.log(makeUser('Kiran'));

const Arrow = () => {};
try { new Arrow(); } catch (e) { console.log(e.constructor.name); }
console.log(Arrow.prototype);

Arrows have no arguments object, need parentheses to return an object literal, and cannot be called with new because they have no prototype.

Key points

Common follow-up questions

Interview tips

Concepts covered

Arrow Functions, Lexical this, arguments Object, Constructors, Implicit Return

More JavaScript Interview questions