Skip to content

Arrow Functions (ES6)ΒΆ

Arrow functions were introduced in ES6. They allow us to write shorter function syntax.

SyntaxΒΆ

let myFunction = (a, b) => a * b;

ComparisonsΒΆ

1. Before ES6 (Traditional)ΒΆ

hello = function() {
  return "Hello World!";
}

2. With Arrow FunctionΒΆ

hello = () => {
  return "Hello World!";
}

3. Even Shorter (Implicit Return)ΒΆ

If the function has only one statement, and the statement returns a value, you can remove the brackets {} and the return keyword:

hello = () => "Hello World!";

4. With ParametersΒΆ

let hello = (val) => "Hello " + val;

this BindingΒΆ

The biggest difference between Arrow Functions and regular functions is how they handle the this keyword. - Regular Functions: this represents the object that called the function. - Arrow Functions: this represents the object that defined the arrow function (Lexical Scoping).

When NOT to use

Do not use Arrow Functions as methods in objects if you need this to refer to the object itself.