...

/

Named and Anonymous Functions

Named and Anonymous Functions

In this lesson, you will see how to name a function, create an anonymous function as well as how to categorize them.

Expressions and declarations

JavaScript, and therefore TypeScript, have many ways to define functions. At a high level, functions can be divided into function declarations and function expressions. A function declaration is when a function is defined by not assigning the function to a variable. e.g.

function myFunction() ...

A function expression is a function assigned to a variable. e.g.

const f = function() ...

The function expression example leads to the creation of anonymous functions where a pointer to a function exists but the function is nameless.

Press + to interact
// Named Function
function functionName1(){}
functionName1(); // Invocation
// Anonymous Function 1
const pointerToFunction1 = function(){}
pointerToFunction1(); // Invocation
// Anonymous Function 2 + Invocation A.K.A. IIFE
(function(){})();

Function’s arguments

TypeScript types are used for function arguments. Types at the level of ...