Basic Syntax: Standard way of writing functions.
function f(a,b){
const sum = a + b;
return sum;
}
console.log(f(1,5)); // Returns 6
You can use ...arg
Anonymous Syntax: Variable is name of function.
var f = function(a,b){
const sum = a + b;
return sum;
}
console.log(f(1,5)); // Returns 6
Immediately Invoked Function Expression (IIFE): This form of function syntax allows for the encapsulation of a variable within a new scope.
const result = (function(a,b){
const sum = a + b;
return sum;
})(1,5);
console.log(result); // Returns 6
Arrow Syntax: A way to declare functions. However, it is used in more unique circumstances.
const f = (a, b) => {
const sum = a + b;
return sum;
};
console.log(f(1,5)); // Returns 6
Arrow syntax is different from function syntax because:
this
, super
, and arguments can be used as a constructor.Omit Return: Shorter option for writing functions. You can omit the return
keyword.
const f = (a, b) => a + b;
console.log(f(1,5)); // Returns 6