Skip to main content

The arrow function syntax

Arrow functions are a shorter way to write functions. They were added in ES6 and are now the most common function syntax in modern JavaScript — especially in React.
Drop the function keyword, add => after the parameters. That’s it.

Implicit return

When your function body is a single expression, you can skip the curly braces and the return keyword:
This works because JavaScript automatically returns the expression after => when there are no curly braces.
If your function does one thing and returns a value, use the implicit return. If it has multiple lines or side effects (like console.log), use curly braces with an explicit return.

Single parameter shorthand

When an arrow function has exactly one parameter, you can drop the parentheses:
Some teams require parentheses around all parameters for consistency. Either style is fine — Prettier will handle this for you based on your config.

Where you’ll use arrow functions

Arrow functions shine as callbacks — functions you pass to other functions. You’ll write these constantly:
In React, arrow functions are everywhere:

Arrow functions vs regular functions

For this course, here’s the practical rule:
Arrow functions have a technical difference with this binding, but you won’t encounter it in this course. React function components and hooks don’t rely on this, so it’s a non-issue. If you’re curious, MDN has a detailed explanation.

Comparing to Python lambdas

JavaScript’s arrow functions are more versatile than Python’s lambdas. They can have multiple lines, multiple statements, and they’re used far more often because JavaScript relies heavily on callbacks.

Common mistakes

With curly braces, you must use return. Without curly braces, the return is implicit. Mixing these up is the most common arrow function mistake.
When implicitly returning an object, wrap it in parentheses (). Without them, JavaScript interprets the curly braces as a function body, not an object.

What’s next?

You can write functions in both styles. Now let’s look at what goes inside the parentheses — parameters, default values, and destructuring.

Parameters and arguments

Default values, rest parameters, and destructuring