Learn JavaScript
❯ functions.arrow_functions

Arrow Functions in JavaScript

$concept

Arrow functions ((a, b) => a + b) are compact function expressions with two defining traits: concise bodies (expression bodies return implicitly, no return keyword) and lexical this (inheriting this from the enclosing scope instead of rebinding). They dominate callbacks, array methods, and short utilities.

They are not universal replacements: arrows can't be constructors (new throws), have no arguments object (use rest), and lexical this backfires for object methods and event handlers needing dynamic receivers. Rule: arrows for callbacks and closures, function for methods and constructors.

$how it works
1

Concise syntax

x => x * 2 (single param, expression body) through (a, b) => { ... } — braces mean statements plus explicit return.

2

Implicit return

Expression bodies return their value automatically; returning object literals needs parens: () => ({x: 1}).

3

Lexical this

Arrows capture this from definition scope — setTimeout callbacks and .then chains see the outer this correctly.

4

Know the limits

No new, no arguments, no own this — methods needing dynamic receivers and constructors stay function expressions.

$example
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
console.log(doubled);
Output
[ 2, 4, 6 ]
1n => n * 2 defines the transform inline — single param, implicit return.
2map applies it per element, building [2, 4, 6].
3Console prints arrays in [ 2, 4, 6 ] notation in Node.
$use cases
Array method callbacks (map/filter/reduce one-liners)
Promise chains and timers preserving outer this
Short utilities where brevity aids reading
Avoiding arrows for object methods and constructors

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.