Functions

JavaScript that follows ES5 specificaitons does not support classes and modules. However, we tried to achieve the scoping of variables and modularity using functional programming in JavaScript. Functions are the building blocks of an application in JavaScript.

Though TypeScript supports classes and modules, functions play a key role in defining a specific logic. We can define both the function and Anonymous functions in JavaScript as shown:

//Named function 
function multiply(a, b) {
return a * b;
}
//Anonymous function
var result = function(a, b) { return a * b; };

In TypeScript, we define functions with the type of the parameters and the return type using function arrow notation, which is also supported in ES6; it's done like this:

var multiply(a: number, b: number) => number =  
function(a: number, b: number): number { return a * b; };
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset