Lesson 10 of 34 · Functions
JavaScript Arrow Functions Explained
Open any modern JavaScript codebase and you'll see => everywhere. Arrow functions are a compact way to write functions, perfect for the small, single-purpose helpers you'll pass around constantly in the coming chapters.
Before we get to arrows, there's one idea to absorb: in JavaScript, functions are values, just like numbers and strings.
Functions are values
You can store a function in a variable. This is called a function expression:
const add = function (a, b) {
return a + b;
};
console.log(add(2, 3)); // 5
The function itself has no name here. add is just a variable that holds it, and calling add(2, 3) works exactly like calling a declared function.
Arrow function syntax
An arrow function is a shorter way to write a function expression. Drop the function keyword and put an arrow => between the parameters and the body:
const add = (a, b) => {
return a + b;
};
When the body is a single expression, you can go further: remove the braces and the return. The value of the expression is returned automatically. This is called an implicit return:
const add = (a, b) => a + b;
const isEven = (n) => n % 2 === 0;
const shout = (text) => text.toUpperCase() + "!";
console.log(add(2, 3)); // 5
console.log(isEven(7)); // false
console.log(shout("charge")); // CHARGE!
A few syntax details:
const rollDie = () => Math.floor(Math.random() * 6) + 1; // no parameters: empty ()
const triple = n => n * 3; // one parameter: () optional
const describe = (name, level) => { // several lines: braces + return
const rank = level >= 10 ? "veteran" : "rookie";
return `${name} the ${rank}`;
};
Many style guides (and code formatters like Prettier) always keep the parentheses, even around a single parameter: (n) => n * 3. That's the style used in this course.
Arrow functions versus regular functions
For everyday helpers, arrows and regular functions are interchangeable. There are a few real differences, though:
- No hoisting. An arrow stored in a
constcan't be called before the line that defines it. - No own
this. Arrows use thethisof the surrounding code. That's great inside callbacks but wrong for object methods, as you'll see in the Objects chapter. - Not constructors. You can't use
newwith an arrow function.
A common convention: use function declarations for important top-level functions, and arrows for short helpers and callbacks.
Common mistakes
- Braces without
return.const double = (n) => { n * 2 };returnsundefined. Once you add braces, you need an explicitreturn. - Returning an object literal.
() => { level: 1 }is read as a function body, not an object. Wrap the object in parentheses:() => ({ level: 1 }). - Calling an arrow before it exists. Define
const helper = () => ...above the code that uses it.
Assignment
Write four functions, all as arrow functions stored in const variables:
double: the starter has it as a regular function declaration. Convert it to an arrow function.double(21)→42.isAdult(age)returnstruewhenageis 18 or more.isAdult(17)→false.clamp(value, min, max)returnsvalue, but limited to the range frommintomax.clamp(150, 0, 100)→100,clamp(-5, 0, 100)→0,clamp(42, 0, 100)→42.fullName(first, last)returns the two names joined by a space.fullName("Ada", "Lovelace")→"Ada Lovelace".
The tests check that every one of them really is an arrow function.
Premium lesson
Unlock the full Learn JavaScript course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.
Lesson last updated on .