Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 156 ยท Functions

Arrow Functions

Write short JavaScript functions with the arrow syntax, use implicit returns, return object literals safely and learn when a regular function is better.

Arrow functions are a compact way to write a function expression. Instead of the function keyword, you write the parameters, an arrow => and the body. They shine in short callbacks, such as the functions you hand to array methods or timers, where the classic syntax adds more noise than meaning.

Arrows are not just shorter, though. They behave differently from regular functions in a few important ways: they have no this of their own, no arguments object, and they can't be used with new. This lesson covers every syntax variant, the implicit return, and the situations where you should reach for a regular function instead.

From function expression to arrow

You met function expressions earlier. Here is one, followed by the same function as an arrow:

JavaScript
const area = function (width, height) {
  return width * height;
};

const areaArrow = (width, height) => {
  return width * height;
};

Drop the function keyword, put => between the parameter list and the body, and you have an arrow function. It is always an expression, so you store it in a variable (usually a const), pass it as an argument or return it from another function.

The syntax variants

Arrow functions have a few shapes, depending on how many parameters they take and how big the body is:

  • No parameters: empty parentheses are required โ€” () => 42.
  • Exactly one simple parameter: the parentheses are optional โ€” n => n * 2 or (n) => n * 2.
  • Two or more parameters, or any parameter with a default value, rest syntax or destructuring: parentheses are required โ€” (a, b) => a + b, (n = 1) => n, (...nums) => nums.length.
  • Expression body: no braces; the value of the expression is returned automatically.
  • Block body: curly braces; you need an explicit return, just like in a regular function.
JS Arrow function shapes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Many code formatters, such as Prettier, add parentheses around a single parameter by default โ€” (n) => n * 2. Both forms are fine; pick one style and stay consistent.

Implicit returns

When the body is a single expression without braces, the arrow function returns that expression's value. This is called an implicit return, and it is the main reason arrows read so well inside array methods:

JavaScript
const prices = [4.5, 12, 7.25];
const withTax = prices.map(price => price * 1.2);
const cheap = prices.filter(price => price < 10);

The moment you add curly braces, the implicit return is gone. This is one of the most common arrow bugs:

JS Braces turn off the implicit return

Press Run (or Ctrl + Enter) to see the console output here.

No output.

squareB computes n * n and throws the result away. With a block body, a function without return returns undefined, exactly like the regular functions in Return Values.

Returning an object literal

Curly braces after => always start a block, never an object. So this function doesn't return what it seems to:

JavaScript
const makePoint = (x, y) => { x: x, y: y };   // SyntaxError

JavaScript reads { as the start of a block, sees x: as a label, and then trips over the comma. To return an object with an implicit return, wrap it in parentheses:

JS Returning objects from arrows

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The parentheses turn the braces into an expression, so the object literal is what gets returned.

Two small syntax rules

  • The arrow must be on the same line as the parameter list. A line break before => is a syntax error. A line break after it is fine.
  • An arrow function is an expression with low precedence. If you want to call one immediately, wrap it: (() => console.log("now"))().

What arrows don't have

Regular functions get a few things automatically every time they are called. Arrow functions skip all of them:

Feature Regular function Arrow function
Its own this Yes, set by how it is called No โ€” uses this from the surrounding code
arguments object Yes No โ€” use rest parameters
Can be called with new Yes (declarations and expressions) No โ€” throws a TypeError
prototype property Yes No
Can be a generator (function*) Yes No
Hoisted Declarations are No โ€” they live in variables

The this difference is the big one, and the this keyword lesson covers it in depth. In short: an arrow function looks up this in the code around it, the same way it looks up any other variable. That is a feature in callbacks and a bug in object methods.

JS Arrows have no arguments and no new

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Rest parameters are covered in Parameters and Arguments. They are a real array, so you can call reduce(), map() and friends on them directly โ€” something the old arguments object couldn't do.

When not to use an arrow function

Arrows are great, but they are not a drop-in replacement for every function. Use a regular function (or method syntax) in these cases.

Object methods that use this

A method usually needs this to point at its object. An arrow can't provide that, because it borrows this from wherever the object literal was written:

JS Arrow methods lose their object

Press Run (or Ctrl + Enter) to see the console output here.

No output.

At the top level of a classic script, this is the global window object, which has no label or seconds property, so you get undefined: undefineds. Use method shorthand โ€” describe() { โ€ฆ } โ€” for methods.

Constructors and generators

You can't call an arrow with new, and there is no arrow form of function*. Use a class or a regular function for those.

Event handlers that rely on this

With addEventListener(), a regular function receives the element as this. An arrow doesn't. That's fine if you use event.currentTarget instead, which is clearer anyway โ€” but don't mix the two up when converting old code.

When a name and hoisting help readability

A function declaration is hoisted, so you can call it before the line where it is written, and it always has a clear name in error messages. For big, top-level building blocks of a program, many developers prefer declarations and keep arrows for small callbacks. Arrows stored in a const get the variable's name too (double.name is "double"), but they can't be used before that line runs.

A simple rule of thumb: use arrows for short callbacks and for functions inside methods that should keep the outer this. Use method shorthand inside objects and classes, and function declarations for the main named functions of your program.

Arrows inside methods: the good case

The flip side of "arrows don't have their own this" is that an arrow inside a method sees the method's this. That makes callbacks inside methods easy:

JavaScript
const cart = {
  items: ["pen", "ink"],
  owner: "Lee",
  list() {
    // The arrow uses list()'s this, which is cart.
    return this.items.map(item => `${this.owner}'s ${item}`);
  },
};

cart.list(); // ["Lee's pen", "Lee's ink"]

Before arrows existed, people wrote const self = this; above the callback or reached for bind. You will still see those patterns in older code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does const f = n => { n + 1 }; return when you call f(1)?

    The braces make a block body, which needs an explicit return. Without it, the function returns undefined.
  2. 2. Which arrow function correctly returns an object with an implicit return?

    Wrapping the object literal in parentheses tells JavaScript it is an expression, not a block. The last option returns an array, not an object.
  3. 3. Which parameter list needs parentheses?

    A single plain parameter can skip the parentheses, but a default value, rest syntax, destructuring or zero or several parameters all require them.
  4. 4. Why is an arrow function a poor choice for an object method that reads this.name?

    Arrows take this from where they are written, not from the object they are called on. Use method shorthand instead.
  5. 5. What replaces the arguments object in an arrow function?

    Arrows have no arguments of their own. A rest parameter collects the extra arguments into a real array.

Summary

  • An arrow function is a short function expression: (params) => body.
  • Parentheses are optional only for exactly one plain parameter; braces turn the body into a block that needs return.
  • Without braces, the expression is returned implicitly. Wrap object literals in parentheses: () => ({ โ€ฆ }).
  • Arrows have no own this, no arguments, no prototype, and can't be used with new or as generators.
  • Use arrows for short callbacks and inside methods; use method shorthand for methods and declarations for main named functions.

Next: Scope and Hoisting โ€” where your variables live and when they become available.