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
const add =stores the function in a variable. The variable's name becomes the function's name, so you still call it withadd(3, 5).(a, b)are the parameters, the same as in a regular function.=>is the arrow: an equals sign=followed by a greater-than sign>, with no space between them. The arrow takes the place of the wordfunction.{ return a + b; }is the function's body.returngives back the result.
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:
- One parameter: the parentheses are not needed.
n => n * 3is the same as(n) => n * 3. - No parameters: write empty parentheses,
() => "Hello!". - Two or more parameters: the parentheses are needed.
(x, y) => x + yworks,x, y => x + yis an error.
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?
returnin the one-line version.n => return n * 2is a SyntaxError (an error in the way the code is written), so the code doesn't run at all. Without braces, don't writereturn.- Calling it before it's written. A regular function can be called from a line above it. An arrow function stored in a
constcan't.timesThree(5)written before the lineconst timesThree = ...throws a ReferenceError. Write it first, then call it. - Names that differ by one letter.
timestwoandtimesTwoare two different names, because JavaScript treats capital and small letters as different. - Returning an object from the one-line version.
() => { name: "Sam" }givesundefined, because{is read as the start of the function body, not the start of an object. Wrap the object in parentheses:() => ({ name: "Sam" }).
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.