Arrow Functions

We'll cover the following...

In ES6 there is a new function syntax. There are some nuances to the syntax that we will be going over.

Before we get into the syntax for an Arrow Function, let’s look at where the inspiration came from. CoffeeScript has had Arrow Functions for a while, there is a talk from Brendan Eich and Jeremy Ashkenas (1) and a post from Brendan (2) where they talk about how developers helped invent the future of JS. CoffeeScript was one of those languages that helped push JavaScript forward.

The syntax in CoffeeScript looks like this:

add = (a,b) -> 
    a + b

For ES6 they settled on this syntax:

Press + to interact
const add = (a,b) => {
return a + b;
};
console.log(add(5, 10));

From an initial look at the syntax, you might be thinking “So, no function keyword?”. That is not quite accurate. The Arrow function can take many different forms. You can write an Arrow Function like an anonymous function.

Press + to interact
const add = function(a,b) {
return a + b;
}
const add = (a,b) => {
return a + b;
}

You call the functions the same way add(2,3) ...