What are Arrow/lambda functions in TypeScript ? (original) (raw)

Last Updated : 23 Jan, 2025

Arrow functions in TypeScript (also known as lambda functions) are concise and lightweight function expressions using the => syntax.

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

`

**Output:

8

Arrow Function with Array Methods

TypeScript `

const numbers = [1, 2, 3, 4, 5]; const squared = numbers.map(n => n * n); console.log(squared);

`

**Output:

[1, 4, 9, 16, 25]

Arrow Function as a Callback

TypeScript `

setTimeout(() => { console.log("This message is displayed after 1 second."); }, 1000);

`

**Output:

This message is displayed after 1 second.

Best Practices for Using Arrow Functions in TypeScript