Arrow Functions (ES6)ΒΆ
Arrow functions were introduced in ES6. They allow us to write shorter function syntax.
SyntaxΒΆ
ComparisonsΒΆ
1. Before ES6 (Traditional)ΒΆ
2. With Arrow FunctionΒΆ
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:
4. With ParametersΒΆ
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.