What are Arrow/lambda functions in TypeScript ? Last Updated : 23 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Arrow functions in TypeScript (also known as lambda functions) are concise and lightweight function expressions using the => syntax.Provide a shorter syntax for defining functions.Automatically bind the context of their surrounding scope.It is commonly used for callbacks, array methods, and simple one-liner functions.Syntax:(param1, param2, ..., paramN) => expression; //Arrow function having multiple parameters() => expressions; //Arrow function with no parameters JavaScript const greet = (name: string): string => `Hello, ${name}!`; console.log(greet("Alice")); Output:Hello, Alice!More Example of Arrow function in TypeScript Arrow Function in a Class JavaScript class Calculator { add = (a: number, b: number): number => a + b; } const calc = new Calculator(); console.log(calc.add(5, 3)); The Calculator class includes an add method defined as an arrow function, which takes two numbers and returns their sum.Using an arrow function ensures that the method inherits the this context from its enclosing class.Output:8Arrow Function with Array Methods TypeScript const numbers = [1, 2, 3, 4, 5]; const squared = numbers.map(n => n * n); console.log(squared); An arrow function is used within the map method to square each number in the numbers array.This concise syntax enhances readability when performing operations on array elements.Output: [1, 4, 9, 16, 25]Arrow Function as a Callback TypeScript setTimeout(() => { console.log("This message is displayed after 1 second."); }, 1000); An arrow function is passed as a callback to setTimeout, which logs a message after a 1-second delay.Arrow functions are ideal for inline callbacks due to their concise syntax and lexical this binding.Output:This message is displayed after 1 second.Best Practices for Using Arrow Functions in TypeScriptUse Arrow Functions for Short Callbacks: Arrow functions are ideal for short and concise callback functions in methods like map, filter, and reduce.Avoid Arrow Functions for Methods in Classes: Use regular functions for class methods that require dynamic this binding, as arrow functions inherit the this context.Return Object Literals Correctly: Wrap object literals in parentheses when returning them in single-line arrow functions to avoid syntax errors.Use Arrow Functions to Maintain this Context: Arrow functions are useful in scenarios where you want to preserve the this context in event handlers or asynchronous code. Comment More info I isitapol2002 Follow Improve Article Tags : TypeScript Geeks-Premier-League-2022 Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like