Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • TypeScript - Get Started
  • TypeScript - Overview
  • TypeScript - Installation
  • TypeScript - First Program
  • TypeScript - Type Annotation
  • TypeScript - Variable
  • Data Types - Number
  • String
  • Boolean
  • Array
  • Tuple
  • Enum
  • Union
  • Any
  • Void
  • Never
  • Type Inference
  • Type Assertion
  • if Statement
  • switch Statement
  • for Loop
  • while Loop
  • Function
  • Arrow Function
  • Function Overloading
  • Rest Parameters
  • Interface
  • Class
  • Abstract Class
  • Data Modifiers
  • ReadOnly
  • Static
  • Module
  • Compiling a Module
  • Namespace
  • Generic
  • Generic Interface
  • Generic Class
  • Compiling Project
  • Build Tools
  • Convert JavaScript to TypeScript
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

TypeScript - Arrow Functions

Fat arrow notations are used for anonymous functions i.e for function expressions. They are also called lambda functions in other languages.

Syntax:
(param1, param2, ..., paramN) => expression

Using fat arrow =>, we dropped the need to use the function keyword. Parameters are passed in the parenthesis (), and the function expression is enclosed within the curly brackets .

Example: Fat Arrow Function
let sum = (x: number, y: number): number => {
    return x + y;
}

sum(10, 20); //returns 30

In the above example, sum is an arrow function. (x:number, y:number) denotes the parameter types, :number specifies the return type. The fat arrow => separates the function parameters and the function body. The right side of => can contain one or more code statements.

The above arrow function sum will be converted into the following JavaScript code.

var sum = function (x, y) {
    return x + y;
}

The following is an arrow function without parameters.

Example: Parameterless Arrow Function
let Print = () => console.log("Hello TypeScript");

Print(); //Output: Hello TypeScript

Furthermore, if the function body consists of only one statement then no need for the curly brackets and the return keyword, as shown below.

let sum = (x: number, y: number) => x + y;

sum(3, 4); //returns 7

A class can include an arrow function as a property, as shown below.

Example: Arrow Function in Class
class Employee {
    empCode: number;
    empName: string;

    constructor(code: number, name: string) {
        this.empName = name;
        this.empCode = code;
    }

    display = () => console.log(this.empCode +' ' + this.empName)
}
let emp = new Employee(1, 'Ram');
emp.display();
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.