Difficulty: Beginner
What are arrow functions in JavaScript, and how do they differ from regular functions?
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.
x => x * 2.(a, b) => a + b.return: (a, b) => { return a + b; }.() => ({ ok: true }). Without them, the braces are read as a block and the function returns undefined.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.
| Feature | Regular function | Arrow function |
|---|---|---|
this | Set by the call site | Inherited from enclosing scope |
arguments | Available | Not available; use ...args |
new | Allowed (function declarations/expressions) | TypeError |
prototype property | Yes | No |
| Hoisting (declarations) | Fully hoisted | Behaves like the variable it is assigned to |
| Duplicate parameter names in sloppy mode | Allowed | SyntaxError |
| Can be a generator | Yes (function*) | No |
this: const counter = { n: 0, inc: () => this.n++ } does not work, because this is the outer scope (the module or undefined), not counter. Use method shorthand: inc() { this.n++; }.this to be the element (function () { this.classList.toggle('open') }). With an arrow, use event.currentTarget instead.arguments or a generator.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.
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.
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.
Arrow Functions, Lexical this, arguments Object, Constructors, Implicit Return