Chapter 12 of 31

JavaScript Arrow Functions

In JavaScript, we often need to write small functions that perform simple tasks. Writing the traditional function syntax every time can sometimes feel a little repetitive.

That's why modern JavaScript introduced arrow functions.

Arrow functions provide a shorter and cleaner syntax for writing functions.

What is an Arrow Function?

An arrow function is a shorter syntax for creating a function using the => arrow operator.

For example, a traditional function expression looks like this:

const add = function(a, b) {
    return a + b;
};

The same function can be written as an arrow function:

const add = (a, b) => {
    return a + b;
};

The => is what gives arrow functions their name.


Basic Syntax

The basic syntax is:

const functionName = (parameters) => {
    // code
};

For example:

const greet = (name) => {
    console.log("Hello, " + name);
};

greet("John");

Output:

Hello, John

Arrow Functions with One Parameter

If an arrow function has only one parameter, parentheses around the parameter are optional.

So instead of:

const greet = (name) => {
    console.log("Hello, " + name);
};

We can write:

const greet = name => {
    console.log("Hello, " + name);
};

Both work the same way.

However, using parentheses consistently can sometimes make the code easier to read, especially when functions become more complex.


Arrow Functions with No Parameters

If the function doesn't have any parameters, we need empty parentheses:

const greet = () => {
    console.log("Hello!");
};

greet();

Output:

Hello!

So:

No parameters → ()
One parameter → () optional
Multiple parameters → () required

Arrow Functions with Multiple Parameters

When there are multiple parameters, parentheses are required.

const add = (a, b) => {
    return a + b;
};

console.log(add(10, 20));

Output:

30

Implicit Return

One of the most useful features of arrow functions is their implicit return.

Suppose we have:

const add = (a, b) => {
    return a + b;
};

Because the function only returns one expression, we can remove the curly braces and return:

const add = (a, b) => a + b;

This is called an implicit return.

For example:

const square = number => number * number;

console.log(square(5));

Output:

25

This makes arrow functions particularly convenient for short operations.


Returning an Object

There's one small syntax detail to remember.

If an arrow function needs to implicitly return an object, wrap the object in parentheses:

const createUser = () => ({
    name: "John",
    age: 20
});

Without the parentheses, JavaScript would interpret the {} as a function body instead of an object being returned.

You don't need to memorize this immediately, but you'll see this pattern frequently in modern JavaScript.


Arrow Functions vs Regular Functions

Arrow functions aren't just shorter versions of regular functions. They also behave differently in some important situations.

The biggest difference beginners should know is how they handle this.

Regular functions have their own this depending on how they are called, while arrow functions do not create their own this. Instead, they inherit this from the surrounding scope.

For example, arrow functions are commonly useful inside callbacks because they naturally preserve the surrounding this.

We'll understand this properly when we reach objects and more advanced JavaScript.


Arrow Functions and Callbacks

Arrow functions are used heavily with callbacks.

For example:

setTimeout(() => {
    console.log("Hello!");
}, 2000);

The arrow function is passed to setTimeout() and runs after approximately 2 seconds.

They are also very common when working with arrays.

For example:

let numbers = [1, 2, 3, 4];

let doubled = numbers.map(number => number * 2);

console.log(doubled);

Output:

[2, 4, 6, 8]

Don't worry if map() is unfamiliar right now. We'll cover array methods later.


A Practical Example

Let's create an arrow function that calculates the final price after applying a discount:

const calculatePrice = (price, discount) =>
    price - (price * discount / 100);

console.log(calculatePrice(1000, 20));

Output:

800

The function is short, readable, and reusable.


When Should You Use Arrow Functions?

Arrow functions are especially useful when:

  • The function is short and simple.

  • You're working with callbacks.

  • You're using array methods such as map(), filter(), and reduce().

  • You want a concise function syntax.

  • You want the function to inherit this from its surrounding scope.

For larger functions, a regular function can sometimes be easier to understand, so don't feel like every function must be an arrow function.

Conclusion

Arrow functions provide a modern and concise way to write JavaScript functions.

A regular function expression:

const add = function(a, b) {
    return a + b;
};

Can become:

const add = (a, b) => a + b;

The main things to remember are:

  • => is used to create an arrow function.

  • Parentheses are optional for a single parameter.

  • Curly braces can be omitted for a single-expression function.

  • When curly braces are omitted, the expression is returned automatically.

  • Arrow functions don't have their own this.

Arrow functions are everywhere in modern JavaScript, so getting comfortable with this syntax will make the rest of your JavaScript journey much easier.