ES6: Arrow Function

Introduction

ES6 provides various features to make the developer's life easier. Arrow function is one of the extensively used features provided in ES6 or ECMAScript 2015.

Arrow function is the new ES6 syntax for defining a JavaScript function. It saves few keystrokes and also makes code look cleaner compared to ES5 syntax. It is often referred as ‘Fat Arrow Function’, and syntax is similar to C# anonymous function.

It uses => token referred as ‘maps to or goes to’ symbol to define the function.

The keywords function and return keywords are avoided when using arrow function. When the definition contains multi-line, then { } curly braces are mandatory.

Function with Parameter with Multiple Parameters

Object Literals

Arrow function can be used to return object literals containing variables and functions. But there are some differences in which this keyboard is processed. Arrow function uses lexical scoping(inner functions contain the scope of parent functions even if the parent function has returned) to determine the value of ‘this’ keyword.

When using ES5, syntax, this keyword returns the current context but in case of arrow function, it returns the global context.