Arrow Functions

Arrow functions are a type of function expression, an alternative with nicer syntax

alt

They are ideal for use in callback functions, as they are compact.

JS

Copy

function performAction(action) {
    action()
}

performAction(() => console.log("Hello, World!")) // Hello, World!

But they can also be used as a function expression.

JS

Copy

const sum = (a, b) => a + b; // one-line implied return

const squareNumber = (number) => {
    return number * number; // multi-line / inside brackets, explicit return.
}

console.log(sum(2, 3)); // 5
console.log(squareNumber(5)); // 25

When used as one-line there is no need for brackets or explicit return statement.

Explicit return statement is only needed inside brackets.