thisJavaScript arrow functions (=>) offer a compact way to write function expressions while preserving the outer scope's this value. Introduced in ES6, they shine inside array callbacks like map(), filter(), and event handlers.
When an arrow function has one expression, you can omit both {} braces and the return keyword. Notice the clean variants:
const double = n => n * 2; (or (n) => n * 2 with Prettier).const add = (a, b) => a + b;{ ... } in parentheses () => ({ ok: true }) so JavaScript does not mistake { for a function body block.this in ActionUnlike regular function declarations, arrow functions never create their own this binding. They capture this from the surrounding function where they are defined, which eliminates older const self = this; workarounds inside timers and callbacks.
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();
x => { id: x } without parentheses: JavaScript parses { id: x } as a labeled block returning undefined. Always write x => ({ id: x }).new; doing so throws TypeError: Fn is not a constructor.arguments inside arrow functions: Arrow functions have no arguments object. Use rest parameters (...args) instead.Avoid arrow functions for object methods that need this to point to the object, constructor functions, and generator functions (function*).
function declarations?No, because arrow functions are assigned to const or let variables, they stay in the Temporal Dead Zone until their declaration line executes.
Yes, const greet = ({ name = "Guest" } = {}) => \Hi, ${name}`;` works identically to standard functions.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.