EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Arrow Function

TypeScript Arrow Function

Updated April 1, 2023

TypeScript Arrow Function

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

TypeScript Arrow Function 1

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:

TypeScript Arrow Function 2

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:

which we add the given two numbers

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:

which we subtract the given two numbers

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:

multiply the given two numbers

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.

  1. TypeScript Dictionary
  2. TypeScript Generic
  3. TypeScript Functions
  4. TypeScript Operators
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW