- Function declaration
- Function expression
- Arrow function
- Immediately Invoked Function Expression (IIFE)
Function declaration
It starts with keyword function. () is the grouping operator.
function name(){}
Function expression
It's used in statements, like assigning a function to a variable.
const great = function () {...}
great()
Arrow function
It's introduced since ES6, and used to shorten the code
const great = () => {}
Immediately Invoked Function Expression (IIFE)
An IIEF is a JavaScript function that runs as soon as it is defined.
IIFE contains two major parts
- The first is the anonymous functions with lexical scope enclosed within the Grouping Operator. This can avoid polluting the global scope
- The second part creates the immediately invoked function expression (), the JavaScript engine will directly interpret the function
(function(){})()
(()=>{})()
Top comments (1)