Javatpoint Logo
Javatpoint Logo

TypeScript Arrow function

ES6 version of TypeScript provides shorthand syntax for defining the anonymous function, i.e., for function expressions. These arrow functions are also called Lambda functions. Arrow function omits the function keyword. It has lexical scoping of this keyword. TypeScript extends arrow function with types parameters.

Syntax

An Arrow function can be categorized into three parts-

  • Parameters: A function may or may not have parameters.
  • The arrow notation/lambda notation (=>)
  • Statements: It represents the function's instruction set.

By using fat arrow (=>) notation, there is no need to use the 'function' keyword. Parameters are passed in the brackets (), and the function expression is enclosed within the curly brackets {}.

Example of Arrow function with parameter:

Program written in TypeScript-

After compiling the above TypeScript code, the corresponding JavaScript code is-

Output:

In the above example, the sum is an arrow function, a:number, b:number is a parameter type, :number is the return type, the arrow notation => separates the function parameter and the function body.

50

Example of Arrow function without a parameter:

Output:

Hello JavaTpoint!

In the arrow function, if the function body consists of only one statement, then there is no need of the curly brackets and the return keyword. It can be shown in the below example:

Output:

SUM: 20

Example of Arrow function in a class:

An arrow function can be included in a class as a property. It can be shown in the below example.

Output:

Student Code: 101
Student Name: Abhishek Mishra




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA