Updated April 1, 2023
Introduction to TypeScript Arrow Function
Whenever there is a need to avoid typing functions, we make use of a function in TypeScript called arrow function which is a short hand syntax for defining the functions that are anonymous and there is no need to make use of the keyword called function when we are using arrow function and arrow function is represented by a fat arrow or “=>” also known as lambda function and there is lexical scoping of the keyword this while using arrow function and the meaning of the arguments are captured lexically while using arrow function and this arrow function is comprised of parameters followed by a fat arrow followed by the set of instructions.
Syntax to declare arrow function in TypeScript class:
(parameter1, parameter2, parameter3,.. parametern) => {
set of instructions;
}
Where,
parameter1, parameter2, parameter3, parameter are the parameters that are passed to the arrow function which executes the set of instructions enclosed between the curly brackets pointed to by a fat arrow.
Working of Arrow Function in TypeScript
- Whenever there is a need to avoid typing functions and to define anonymous functions, we make use of a function called arrow function.
- We do not have to make use of the keyword function when we are using arrow function.
- The arrow function is represented by a fat arrow or “=>”.
- The arrow function takes a set of parameters enclosed in small brackets followed by a fat arrow which is then followed by the set of instructions to be executed enclosed inside the curly brackets.
- The meaning of this keyword and the meaning of arguments to the arrow function is captured lexically using arrow function.
Examples of TypeScript Arrow Function
Given below are the examples mentioned:
Example #1
TypeScript program to demonstrate the usage of arrow function using which we compute the power of a given number and display the output on the screen.
Code:
//defining an anonymous function using arrow function to compute the power of a number and display the result as the output on the screen
let power = (firstnum:number, secondnum:number) : number => {
return Math.pow(firstnum, secondnum);
}
console.log('The result when the given number is raised to the power of 2 is: ');
console.log(power(10,2));
Output:
In the above program, we are defining an anonymous function using arrow function to compute the power of a given number and display the result as the output on the screen.
Example #2
TypeScript program to demonstrate the usage of arrow function using which we find the square root of a given number and display the output on the screen.
Code:
//defining an anonymous function using arrow function to compute the square root of a number and display the result as the output on the screen
let squareroot = (firstnum:number) : number => {
return Math.sqrt(firstnum);
}
console.log('The square root of the given number is: ');
console.log(squareroot(4));
Output:
In the above program, we are defining an anonymous function using arrow function to find the square root of a given number and display the result as the output on the screen.
Example #3
Program to demonstrate the usage of the arrow function using which we add the given two numbers and display the output on the screen.
Code:
//defining an anonymous function using arrow function to add the given two numbers and display the result as the output on the screen
let addit = (firstnum:number, secondnum:number) : number => {
return firstnum + secondnum;
}
console.log('The result of adding the given two numbers is:');
console.log(addit(10,2));
Output:
In the above program, we are defining an anonymous function using arrow function to add the given two numbers and display the result as the output on the screen.
Example #4
Program to demonstrate the usage of arrow function using which we subtract the given two numbers and display the output on the screen.
Code:
//defining an anonymous function using arrow function to subtract the given two numbers and display the result as the output on the screen
let subit = (firstnum:number, secondnum:number) : number => {
return firstnum - secondnum;
}
console.log('The result of subtracting the given two numbers is: ');
console.log(subit(10,2));
Output:
In the above program, we are defining an anonymous function using arrow function to subtract the given two numbers and display the result as the output on the screen.
Example #5
Program to demonstrate the usage of arrow function using which we multiply the given two numbers and display the output on the screen.
Code:
//defining an anonymous function using arrow function to multiply the given two numbers and display the result as the output on the screen
let mulit = (firstnum:number, secondnum:number) : number => {
return firstnum * secondnum;
}
console.log('The result of multiplying the given two numbers is:');
console.log(mulit(10,2));
Output:
In the above program, we are defining an anonymous function using arrow function to multiply the given two numbers and display the result as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Arrow Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.