• Regular Functions / Function Declarations -
  • Anonymous Functions / Function Expressions are just function declarations without a name. Every time you write the function keyword, anywhere in your code, giving it a name right after is optional. Omitting the name makes the function, well, anonymous.
  • Arrow Functions provide lexical binding, meaning you get the control back on the value of this inside the function’s body. By getting the control back, I mean that it’s fairly easy to know which object this refers to just by looking at the code where your arrow function is defined (and not called)

Code Examples

Function Type

Code Example

Regular Function
Function Declaration

function regular(a, b) { return a + b; }
regular(1, 2); // returns 3

Anonymous Function
Function Expression

var anon = function (a, b) { return a + b; };
anon(1, 2);    // outputs 3
 

Arrow Function

var arrow = (a, b) => { return a + b; };
arrow(1, 2);   // outputs 3
var arrow = (a, b) => a + b;
arrow(1, 2);   // outputs 3

Function Type

Code Example

Regular Function
Function Declaration

(function regular(a, b) {
	console.log(a + b);
})(1, 2);                     // outputs 3
regular(1, 2);                // ReferenceError: regular is not defined

Anonymous Function
Function Expression

(function (a, b) {
	console.log(a + b)
})(1, 2);                     // outputs 3

Arrow Function

((a, b) => { 
	console.log(a + b)
})(1, 2);                     // outputs 3

Function Type

Code Example

Regular Function
Function Declaration

var regular_response = (function regular(a, b) {
	return a + b;
})(1, 2);                     
console.log(regular_response);    // outputs 3

Anonymous Function
Function Expression

var anon_response = (function (a, b) {
	return a + b;
})(1, 2);
console.log(anon_response);       // outputs 3
 

Arrow Function

var arrow_response = ((a, b) => { 
	return a + b;
})(1, 2);
console.log(arrow_response);      // outputs 3

Differences Between Them

arrow function does not do ‘this’ binding, as shown below

var user = { 
    name: "Marcus Chiu", 
    regular() {    
        console.log(this.name); // 'this' binding works here
    },
	anon: function () {
		console.log(this.name); // 'this' binding works here
	},
	arrow: () => { 
        console.log(this.name); // 'this' binding FAILS here
    },
};
 
user.regular(); // outputs 'Marcus Chiu'
user.anon();    // outputs 'Marcus Chiu'
user.arrow();   // outputs nothing