EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Arrow Function
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Arrow Function

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.

Syntax to declare arrow function in TypeScript class:

(parameter1, parameter2, parameter3,.. parametern) => {
set of instructions;
}

Where,

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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));

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)

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

This is a guide to TypeScript Arrow Function. Here we discuss the introduction, working of arrow function in TypeScript, and examples. You may also have a look at the following articles to learn more –

  1. TypeScript Dictionary
  2. TypeScript Generic
  3. TypeScript Functions
  4. TypeScript Operators
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

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

EDUCBA Login

Forgot Password?

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

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

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

EDUCBA

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

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

Let’s Get Started

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

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