DEV Community

Mavis
Mavis

Posted on

JavaScript Function Types

  • Function declaration
  • Function expression
  • Arrow function
  • Immediately Invoked Function Expression (IIFE)

Function declaration

It starts with keyword function. () is the grouping operator.

function name(){}
Enter fullscreen mode Exit fullscreen mode

Function expression

It's used in statements, like assigning a function to a variable.

const great = function () {...}
great()
Enter fullscreen mode Exit fullscreen mode

Arrow function

It's introduced since ES6, and used to shorten the code

const great = () => {}
Enter fullscreen mode Exit fullscreen mode

Immediately Invoked Function Expression (IIFE)

An IIEF is a JavaScript function that runs as soon as it is defined.
IIFE contains two major parts

  1. The first is the anonymous functions with lexical scope enclosed within the Grouping Operator. This can avoid polluting the global scope
  2. The second part creates the immediately invoked function expression (), the JavaScript engine will directly interpret the function
(function(){})()
(()=>{})()
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
Sloan, the sloth mascot
Comment deleted