Tuesday, November 6, 2018

TypeScript - ES6 - arrow functions

In ES6, arrow functions were introduced.

Consider the following function to multiply two numbers.
console.log("Arrow functions Demo");
console.log("+++++++++++++++++++++");
const multNumbers = function(number1:number, number2:number): number {
return number1 * number2;
}
this is the traditional method to define functions.

Now, the ES6 way of defining arrow functions are as follows:

console.log("Add Numbers:");
const addMyNumbers = (number1:number, number2:number) => (number1+number2);
console.log("addMyNumbers(4,5) == "+addMyNumbers(4,5));

This is called arrow functions because we use the arrow operator to point to the function body.

Reference:
https://www.typescriptlang.org/docs/handbook/functions.html

No comments:

Post a Comment