Lesson

JS Arrow Function

10 min reading
Free Course

JavaScript Arrow Functions: Concise Syntax and Lexical this

JavaScript arrow functions (=>) are a compact way to write function expressions, and they keep the this value of the code around them. Introduced in ES6, they fit naturally into array callbacks such as map() and filter(), and into timers and event handlers.

This guide covers the syntax shortcuts, how lexical this works, and the mistakes that trip up most developers when they start using arrow functions.

Arrow Function Syntax and Implicit Returns

An arrow function with a single expression can drop both the curly braces and the return keyword. The result of the expression is returned automatically.

  • One parameter: const double = n => n 2; Parentheses are optional here, and many style guides, including Prettier's default, add them: (n) => n 2.

  • Multiple parameters: const add = (a, b) => a + b;

  • No parameters: const now = () => Date.now(); Empty parentheses are required.

  • Returning an object literal: wrap the braces in parentheses, as in () => ({ ok: true }). Without them, JavaScript reads { as the start of a function body, not an object.

If you want a refresher on regular functions first, see our lesson on JavaScript functions.

Lexical this in Arrow Functions

A regular function decides its own this based on how it is called. An arrow function never creates its own this. It uses the one from the surrounding code where it was written, which is what "lexical this" means. This removes the old const self = this; workaround in timers and callbacks. Our lesson on the JavaScript this keyword explains the full set of binding rules.

const prices = [10, 25, 40];
const withTax = prices.map((p) => Number((p * 1.1).toFixed(2)));
console.log("With tax:", withTax);

// Returning an object literal requires parentheses around {}
const buildTag = (label) => ({ label, slug: label.toLowerCase() });
console.log("Tag object:", buildTag("JavaScript"));

// Lexical this preserved inside callback
const timerBox = {
seconds: 3,
start() {
setTimeout(() => {
console.log(Timer finished after ${this.seconds}s);
}, 10);
},
};
timerBox.start();

Common Mistakes to Avoid

  • Writing x => { id: x } without parentheses: JavaScript parses { id: x } as a labeled block returning undefined. Always write x => ({ id: x }).

  • Using arrow functions for constructors: Arrow functions cannot be called with new; doing so throws TypeError: Fn is not a constructor.

  • Relying on arguments inside arrow functions: Arrow functions have no arguments object. Use rest parameters (...args) instead.

Frequently Asked Questions

When should I avoid arrow functions?

Avoid them for object methods that need this to point at the object, for constructor functions, and for generators, since arrow functions cannot be written as function*. They are also a poor fit for event handlers where you want this to be the element, although event.currentTarget solves that.

Are arrow functions hoisted like function declarations?

No. An arrow function is usually assigned to a const or let variable, so it sits in the temporal dead zone until that line runs. Calling it earlier throws a ReferenceError. Our lesson on JavaScript const covers this.

What is the difference between an arrow function and a regular function?

Arrow functions are shorter, inherit this, have no arguments object, and cannot be used with new. Regular functions get their own this from the call site.

Can an arrow function be async?

Yes. Put async before the parameters: const load = async (url) => { ... }. The this rules stay the same.

Conclusion

Arrow functions shine in short callbacks, and anywhere you need to keep the surrounding this. Reach for regular functions or method shorthand when you need your own this, arguments, or new. Next, put them to work in JavaScript array methods such as map(), filter(), and reduce(), or read MDN's guide to functions.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum