## DEV Community is a community of 699,510 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Regular vs Arrow Function

Suprabha
π₯ New JS, CSS, HTML articles every week π₯ Frontend engineer @gojekindonesia π©π»βπ» http://instagram.com/suprabhasupi

Define your functions in many ways.

One way is using function keyword:

// function declaration
function test(msg) {
return `Hey \${msg}`
}

// function expression
const test = function(msg) {
return `Hey \${msg}`
}

You can call both function declaration and expression as Normal/Regular Function

Arrow function is introduced in ES6 and also known as fat arrow function.

const arrowFunction = (msg) => {
return `Hey \${msg}`
}

As you see both functions work same by above example. Now the question comes why do we need regular or arrow function.

Let's discuss below π

### 1οΈβ£ Syntax

We can write normal and arrow function in this way π

// ES5
var add = function(x, y) {
return x + y
};

// ES6
let add = (x, y) =>  x + y

### Implicit Return

In regular function, you have to use return keyword to return any value. If you don't return anything then the function will return undefined.

function regFunc() {
return "Regular Function";
}
regFunc();
// Regular Function

function regFunc() {
console.log("Regular Function")
}
regFunc();
// Regular Function
// undefined

Arrow functions behave in the same way when returning values.

If the arrow function contains one expression, you can omit the curly braces, and then the expression will be implicitly returned.

#### {} not required if its only one line of statement

const addOne = (number) => number + 1;

#### () not required if you pass only one argument

let add = x => x + x;

#### If there is no arguments

let arrowFunc = _ => console.log("Arrow Function");

### 2οΈβ£ Arguments binding

In regular function, Arguments keywords can be used to access the arguments of which passed to function.

Example:

function regularFunction(a,b) {
console.log(arguments)
}

regularFunction(1,2)
// Arguments[1,2]

Arrow functions do not have an arguments binding.

const arrowFunction = (a,b) => {
console.log(arguments)
}

arrowFunction(1,2)
//ReferenceError: argumnets is not defined

However, if you want to access arguments in an arrow function, you can use the rest operator:

var arrowFunction = (...args) => {
console.log(...args)
}

arrowFunction(1,2)
// 1 2

### 3οΈβ£ this

In regular function, this changes according to the way that function is invoked.

• Simple Invocation:Β thisΒ equals the global object or maybe undefined if you are using strict mode.
• Method Invocation:Β thisΒ equals the object that owns the method.
• Indirect Invocation:Β thisΒ equals the first argument.
• Constructor Invocation:Β thisΒ equals the newly created instance.
// 1οΈβ£ Simple Invocation
function simpleInvocation() {
console.log(this);
}

simpleInvocation();
// Window Object

// 2οΈβ£ Method Invocation
const methodInvocation = {
method() {
console.log(this);
}
};

methodInvocation.method();
// logs methodInvocation object

// 3οΈβ£ Indirect Invocation
const context = { aVal: 'A', bVal: 'B' };
function indirectInvocation() {
console.log(this);
}

indirectInvocation.call(context);  // logs { aVal: 'A' }
indirectInvocation.apply(context); // logs { bVal: 'A' }

// 4οΈβ£ Constructor Invocation
function constructorInvocation() {
console.log(this);
}

new constructorInvocation();
// logs an instance of constructorInvocation

Arrow functions don't have their own this, and they donβt redefine the value of this within the function.

this inside an arrow function always refers to this from the outer context.

var name = "Suprabha"
let newObject = {
name : "supi",
arrowFunc: () => {
console.log(this.name);
},
regularFunc() {
console.log(this.name);
}
}

newObject.arrowFunc(); // Suprabha
newObject.regularFunc(); // supi

### 4οΈβ£ new

Regular functions are constructible, they can be called using the new keyword.

console.log(x + y)
}

// 5

However, arrow functions can never be used as constructor functions. Hence, they can never be invoked with the new keyword

let add = (x, y) => console.log(x + y);

// TypeError: add is not a constructor

### 5οΈβ£ No duplicate named parameters

In normal function, we can do this:

// β will work

// β will not work
'use strict';

// Uncaught SyntaxError: Duplicate parameter name not allowed in this context

Arrow functions can never have duplicate named parameters, whether in strict or non-strict mode.

const arrowFunc = (a,a) => {}

// Uncaught SyntaxError: Duplicate parameter name not allowed in this context

### 6οΈβ£ Function Hoisting

In regular function, function gets hoisting at top.

normalFunc()

function normalFunc() {
return "Normal Function"
}

// "Normal Function"

In arrow function, function get hoisted where you define. So, if you call the function before initialisation you will get referenceError.

arrowFunc()

const arrowFunc = () => {
return "Arrow Function"
}

// ReferenceError: Cannot access 'arrowFunc' before initialization

### 7οΈβ£ Methods

You can define methods in class using regular function.

class FullName {
constructor(name) {
this.name = name;
}

result() {
console.log(this.name)
}
}

let name = new FullName("Suprabha")

console.log(name)
// FullNameΒ {name: "Suprabha"}

You need to apply method as callback also.

setTimeout(name.result, 2000)
// after 1 second logs ""

But if you bind this

setTimeout(name.result.bind(name), 2000)
// Suprabha

By above example, you can see that you have to bind the this to there context.

In arrow function, you don't have to bind with context.

class FullName {
constructor(name) {
this.name = name;
}

result = () => {
console.log(this.name)
}
}

let name = new FullName("Suprabha")

setTimeout(name.result, 2000) // Suprabha

### When not to use Arrow function π©π»βπ»

Object Methods

let dog = {
count: 3,
jumps: () => {
this.count++
}
}

When you call dog.jumps, the number of count does not increase. It is because this is not bound to anything, and will inherit the value of this from its parent scope.

## Reference π§

### Summary

In regular function, this value is dynamic, In arrow function it equals to this of the outer function.

In regular function, arguments will give you list of parameter passed in function, In arrow function arguments is not defined.

In regular function, you always have to return any value, but in Arrow function you can skip return keyword and write in single line.

In arrow function parameters should be unique.

Hoisting matters in arrow function as function get not be invoked before initialisations.

Thanks for reading the article β€οΈ

 π Twitter π Ebooks π Instagram

## Discussion (4)

Lee Baldwin

As a newbie to front-end development, I am trying to understand why developers would want to use arrow functions. It seems to me that it would just be easier to type the word function and return than to type an arrow function, unless they create a arrow function key on keyboards.

Isaac Lyman

Note that => is written with an equal sign and a greater-than sign, both common programming symbols and pretty accessibly on a standard keyboard.

As for why, the top reasons I use arrow functions are:
1) Succinctness. If I'm trying to add 1 to each element in listOfNumbers, the old way to do it was:

var incrementedNumbers = listOfNumbers.map(function (num) { return num + 1; });

With an arrow function I can do

var incrementedNumbers = listOfNumbers.map(num => num + 1);

Which is quicker to write and quicker to read.

2) Lexical context. When you write function, it creates its own this binding (that is, inside of a classic function, the this keyword refers to the function itself). But for arrow functions, this refers to the same thing both inside and outside the function; arrow functions don't create a lexical context. This is occasionally handy when I have some data in this that I need to use throughout a function, including in various predicates (like in functions passed to Array.map or async callbacks).

Manu

Haha, I have an obsession with arrow functions
(I don't really care about IE, it's shutting down anyways someday)

peerreynders

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.

i.e. preferring arrow functions you may be called upon to demonstrate knowledge of when not to use them, e.g. as class methods.

Plain ol' function is still the most appropriate for the general use case (and function hoisting can be quite helpful - you can put functions at the end of the script, out of the way of the primary script). Arrow functions were largely introduced to save you from having to explicitly bind functions for use as event listeners.