![]() |
VOOZH | about |
Arrow functions in TypeScript are similar to JavaScript (ES6) but allow you to add type annotations for parameters and return values. They provide a concise and type-safe way to define functions.
this, avoiding common context issues.Syntax
let function_name = (
parameter_1 : data_type,
...
) : return_type => {
...
}
In the above syntax:
let:sed to declare the arrow function and assign it to a variable.function_name: The name of the variable holding the arrow function.parameter_1: data_type: Function parameter with its type annotation. Multiple parameters can be separated by commas.... : Indicates additional parameters may be defined.: return_type: Specifies the type of value the function will return.In this example, we create a function that returns a string. The function takes a name as a parameter and returns it.
Output:
GeeksforGeeksIn this example, we create a function to add two integers. The function takes two parameters of type number and returns their sum, also of type number
Output:
140
180
190
In this example, we create a function that returns a string composed of three different data types. The function takes a number, a string, and an array of numbers as parameters, and returns a formatted string.
Output:
1- ABC - 10,20,30
2- APPLE - 50,20,30
3- MANGO - 70,90,80