1. Function Declaration
A function created with a function declaration is aFunction
object and has all the properties, methods and behavior ofFunction
objects.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function#Description
function add(a, b) {
return a + b;
}
2. Function Expression
Its like declaring a variable but using the function
keyword.
const add = function(a, b) {
return a + b;
}
3. Arrow Function Expression
An arrow function expression has a shorter syntax than a function expression and does not have its ownthis
,arguments
,super
, ornew.target
. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
const add = (a, b) => {
return a + b;
}
4. Concise Body Arrow Function Expression
An arrow function expression has a shorter syntax than a function expression and does not have its ownthis
,arguments
,super
, ornew.target
. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
const add = (a, b) => a + b;
5. IIFE "iffy" or the Immediately-Invoked Function Expression
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.https://developer.mozilla.org/en-US/docs/Glossary/IIFE
(function(a, b) {
alert(a + b);
})(a, b);