JS JavaScript

JavaScript: Functions & Arrow Functions

What you will learn

Two ways to define functions in modern JS, plus how closures capture variables.

Traditional function declaration

function greet(name) {
    return `Hello, ${name}!`;
}
console.log(greet("Alice"));

Functions defined this way are hoisted — you can call them before the line that defines them.

Arrow functions (modern)

const greet = (name) => `Hello, ${name}!`;
const double = (n) => n * 2;

Arrow functions with one expression auto-return (no return keyword needed). Multiple parameters need parentheses: (a, b) => a + b. No parameters: () => 42.

Arrow functions do not have their own this — they inherit it from the surrounding scope, which makes them ideal for callbacks in classes or event handlers.

Callbacks

Functions are first-class values — you can pass them as arguments:

const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6]

Quick check below!