Arrow Functions

JavaScript ES2023 · ✓ verified by execution on 2026-08-11

Arrow functions offer a more compact syntax compared to traditional function expressions. They are often called “fat arrow” functions because they use the => token to separate the parameters from the function body.

An arrow function expression is a compact alternative to a traditional function expression. Here is a basic example of an arrow function:

javascript
const add = (a, b) => {
  return a + b;
};
console.log(add(2, 3));
Output
5

Notice that the function keyword is entirely gone. We simply define the parameters in parentheses, followed by the arrow =>, and then the block body.

Implicit Returns

If an arrow function has a single expression as its body, the return keyword and curly braces can be omitted. In a concise body, only an expression is specified, which becomes the implicit return value.

javascript · visualize
const add = (a, b) => a + b;
console.log(add(2, 3));

Step through the visualizer above to see how the expression a + b evaluates and returns directly, without needing a block body.

Visualizing the Difference

Below is a diagram showing the structural differences between explicit block bodies and implicit returns.

Explicit Return (Block Body): (a, b) => { return a + b; } Implicit Return (Concise Body): (a, b) => a + b
Explicit vs Implicit Returns

Single Parameters

If an arrow function has only one parameter, the parentheses around the parameter can be omitted. Parentheses are optional when there’s only one parameter name, which makes the syntax even cleaner.

javascript
const double = n => n * 2;
console.log(double(4));
Output
8

Edge Cases: Returning Objects

To implicitly return an object literal, it must be wrapped in parentheses. If you just use curly braces, the JavaScript engine interprets them as a block body, not an object literal!

See if you can spot the common mistake. Returning an object literal requires wrapping the braces in parentheses like => ({}) so the engine doesn’t treat the braces as a block body.

Predict the output javascript

Predict what happens when you forget to wrap the object literal in parentheses:

const makeUser = name => { user: name };
console.log(makeUser('Alice'));
Output
undefined

Because the curly braces were treated as a block body, the function executed the label statement user: name and then implicitly returned undefined because it lacked a return statement.

To fix this, wrap the object literal in parentheses:

javascript
const makeUser = name => ({ user: name });
console.log(makeUser('Alice').user);
Output
Alice

The this Binding

Arrow functions do not have their own bindings to this or super, and should not be used as methods. Instead, they inherit this from the enclosing scope.

javascript
const obj = {
  id: 42,
  counter: () => {
    return typeof this;
  }
};
console.log(obj.counter());
Output
undefined

In the example above, the arrow function inside the object inherits this from the global scope (which is undefined in ES modules or strict mode). If it were a traditional function expression, this would be bound to obj.

Because they lack their own this, arrow functions cannot be used as constructors and will throw a TypeError if invoked with new.

Check yourself

Why do we still need traditional function declarations if we have arrow functions?

Reveal answer

Function declarations are hoisted and can be used as constructors, which arrow functions cannot. — Arrow functions completely replace the need for function declarations in many cases, but they are not hoisted and cannot be used as constructors, so function declarations still have their place.

When can you omit the parentheses around the parameters in an arrow function?

Reveal answer

Only when there is exactly one parameter. — Parentheses can only be omitted if there is exactly one parameter. Zero parameters or multiple parameters require parentheses.

What happens if you use curly braces for an arrow function's body without a return statement?

Reveal answer

The function implicitly returns undefined. — Curly braces define a block body. If you use curly braces, you MUST use the return keyword to return a value. Otherwise, it implicitly returns undefined.

How does the 'this' keyword behave differently in arrow functions?

Reveal answer

Arrow functions do not have their own bindings to 'this', making them uniquely suited for callbacks. — Arrow functions are not just syntactic sugar. They actually behave differently regarding the 'this' keyword because they do not have their own bindings, which makes them uniquely suited for callbacks.

Challenges

Challenge 1 +50 XP

Convert this traditional function expression into an arrow function with an implicit return.

javascript
  • Test 1 — expects "12\n"
Need a hint? (−25% XP)

Remove the function keyword and use => between parameters and the body.

Show solution (0 XP)
const multiply = (a, b) => a * b;
console.log(multiply(3, 4));

Challenge 2 +50 XP

Write an arrow function called `greet` that takes a single parameter `name` and implicitly returns 'Hello ' + name. Do not use parentheses around the parameter.

javascript
  • Test 1 — expects "Hello World\n"
Need a hint? (−25% XP)

You can omit the parentheses around name.

Show solution (0 XP)
const greet = name => 'Hello ' + name;
console.log(greet('World'));