Arrow Functions

Duration: 10 min  •  Difficulty: Medium

In addition to the classic function() framework, ES6 introduces Arrow Functions which are more agile and avoid classic _binding context_ issues (the this keyword).

How it Works

Traditional:

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

Arrow Function:

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

Implicit Return

If your function content is very simple and compact enough to fit in a single line of code, the return keyword and {} structure can be removed entirely.

main.js
// One-shot. Most often used in map/filter callbacks.
const greet = name => `Hello ${name}!`;
console.log(greet("Raffi")); // Output: Hello Raffi!