Arrow Functions — JavaScript

An arrow function is a shorter way to write a function: const name = (parameters) => { return result; };. When the function's body is a single expression…

function timesThree(n) { return n * 3; } and const timesThree = n => n * 3; are the same function. Call timesThree(5) and both give 15. The difference is that the second form is much shorter: it has no function keyword and no return.

The second form, the one with the arrow =>, is called an arrow function: a shorter way to write a function. It works the same way. A function takes values through its parameters, works with them, then gives back the result. Only the way you write it changes, plus one difference about this, which the last part of this lesson covers.

From a regular function to an arrow function

Here is a regular function that adds two numbers:

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

console.log(add(3, 5)); // 8

Here is the same function, written as an arrow function:

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

console.log(add(3, 5)); // 8

Both versions print 8.

The one-line version

When the function's body is a single expression (code that produces a value), you can leave out the curly braces and return:

const add = (a, b) => a + b;
const timesThree = n => n * 3;
const greet = () => "Hello!";

console.log(add(3, 5));      // 8
console.log(timesThree(5));  // 15
console.log(greet());        // Hello!

The result of the expression after => is returned right away, without the word return. This is called an implicit return (an automatic return).

The rules for the parentheses around the parameters:

Curly braces need a return

Both functions here use curly braces, but only the second one writes return:

const doubleA = (n) => { n * 2 };
const doubleB = (n) => { return n * 2 };

console.log(doubleA(4)); // undefined
console.log(doubleB(4)); // 8

As soon as there are { }, JavaScript treats what is inside them as a regular function body. A regular function body gives back no value until you write return. That is why the first function gives undefined, which means "no value", and there is no error message. So choose one: no braces and no return, or braces with return.

One important difference: this

this is a keyword. When a regular function is called through an object, this inside that function points to that object. In this example, the object cat has a method greet (a method is a function stored inside an object):

const cat = {
  name: "Oyen",
  greet: function () {
    return "Hello " + this.name;
  },
};

console.log(cat.greet()); // Hello Oyen

On the last line, greet is called through cat. So this is cat, and the function can read the name Oyen.

An arrow function has no this of its own. It uses the this of the code around it. If greet were written as an arrow function, this would no longer be cat, so the function could not read the name Oyen. That is why object methods are written as regular functions. Arrow functions are a good fit for small functions that you pass as arguments to other functions. As for speed, the two forms are equally fast.

⚠️ Why does the arrow function throw an error?

TL;DR: An arrow function is a shorter way to write a function: const name = (parameters) => { return result; };. When the function's body is a single expression (code that produces a value), leave out the curly braces and return: const timesThree = n => n * 3;. Braces without return give undefined (no value). Two or more parameters need parentheses (x, y), and no parameters means (). Inside a method (a function stored in an object), the keyword this points to that object. An arrow function has no this of its own, so methods are still written as regular functions.

⚡ => takes the place of function

Write:

const add = (a, b) => a + b;
const timesThree = n => n * 3;
const greet = () => "Hello!";

Meaning: These three lines are arrow functions, a short way to write a function. When the function's body is a single expression (code that produces a value), you write no curly braces and no return, and the result after => is returned right away. The rules for parentheses: one parameter can go without them, no parameters means (), and two or more parameters need them.

⚡ Curly braces need a return

Write:

const doubleA = (n) => { n * 2 };
const doubleB = (n) => { return n * 2 };
Guess first: what does doubleA(4) give?

undefined (no value), with no error message. As soon as an arrow function uses { }, it gives back no value until you write return. doubleB(4) gives 8.

Meaning: Choose one: no braces and no return, or braces with return.

⚡ An arrow function has no this of its own

Write:

const cat = {
  name: "Oyen",
  greet: function () {
    return "Hello " + this.name;
  },
};
Guess first: if greet were an arrow function, could it still read the name Oyen?

No. When a regular function is called through an object, as in cat.greet(), the keyword this points to that object: cat. An arrow function uses the this of the code around it, so this would no longer be cat.

Meaning: Methods (functions stored inside an object) are written as regular functions, so that this points to their object. Arrow functions are a good fit for small functions that you pass as arguments to other functions. As for speed, the two forms are the same.

What you will learn