Function Expressions
Function expressions differ from function declarations in that they can be assigned to variables, used inline, or invoked immediately – an immediately invoked function expression or IIFE. Function expressions can be named or anonymous. Let's look at a few examples:
Example09.ts
1 const myFunction = function(name: string): string { 2 return `Hello ${name}!`; 3 }; 4 console.log(myFunction('function expression'));
Link to the preceding example: https://packt.link/2JeGQ
You will get the following output:
Hello function expression!
This looks quite a lot like a previous example we looked at, and it works almost exactly the same. Here is the function declaration for comparison:
function myFunction(name: string = 'world'): string { return `Hello ${name}!`; }
The one slight difference is that function declarations are hoisted, meaning they are loaded into memory (along with any declared variables) and...