Lesson 26 of 60 – Arrow Functions
43%

Arrow Functions in TypeScript

An arrow function is a shorter way to write a function in JavaScript and TypeScript. Arrow functions use the => symbol and are very commonly used in React and React Native applications.

Note: TypeScript allows you to add type annotations to arrow function parameters and return values.

1. What is an Arrow Function?

An arrow function is a shorter syntax for creating a function.

const greet = () => {
    console.log("Hello");
};

greet();

The => symbol is called the arrow operator.

2. Basic Arrow Function Syntax

The basic syntax of an arrow function is:

const functionName = () => {
    // code
};

The function is stored inside a variable.

3. Arrow Function Without Parameters

An arrow function can have no parameters.

const message = () => {
    console.log("Welcome to TypeScript");
};

message();

Empty parentheses () are used when there are no parameters.

4. Arrow Function with One Parameter

An arrow function can receive a parameter.

const greet = (name: string) => {
    console.log("Hello " + name);
};

greet("Amit");

The parameter is typed as string.

5. Arrow Function with Number Parameter

TypeScript allows us to specify the parameter type.

const square = (number: number) => {
    return number * number;
};

console.log(
    square(5)
);

Output:

25

6. Arrow Function with Multiple Parameters

Multiple parameters are separated by commas.

const add = (
    a: number,
    b: number
) => {

    return a + b;
};

console.log(
    add(10, 20)
);

7. Arrow Function Return Type

You can specify the return type after the parameter list.

const add = (
    a: number,
    b: number
): number => {

    return a + b;
};

Here, : number means the function returns a number.

8. Arrow Function Returning a String

An arrow function can return a string.

const getName = (): string => {

    return "Rahul";
};

console.log(
    getName()
);

9. Arrow Function Returning a Boolean

An arrow function can return a boolean value.

const isAdult = (
    age: number
): boolean => {

    return age >= 18;
};

console.log(
    isAdult(20)
);

10. Short Arrow Function

When an arrow function contains only one return statement, it can be written in a shorter form.

const square = (
    number: number
): number => number * number;

The braces and return keyword are not required.

11. Implicit Return

When the function contains a single expression, the result is automatically returned.

const double = (
    number: number
): number => number * 2;

console.log(
    double(10)
);

This is called an implicit return.

12. Explicit Return

With curly braces, use the return statement explicitly.

const double = (
    number: number
): number => {

    return number * 2;
};

Both explicit and implicit return styles are valid.

13. One Parameter Without Parentheses

When there is one parameter, parentheses can be omitted.

const square = number => number * number;

In TypeScript, however, adding the parameter type requires parentheses.

const square = (
    number: number
) => number * number;

14. Arrow Function with Two Parameters

With multiple parameters, parentheses are required.

const multiply = (
    a: number,
    b: number
): number => a * b;

console.log(
    multiply(5, 4)
);

Output:

20

15. Arrow Function with an Array

Arrow functions can receive arrays as parameters.

const getLength = (
    items: string[]
): number => {

    return items.length;
};

console.log(
    getLength([
        "HTML",
        "CSS",
        "TypeScript"
    ])
);

16. Arrow Functions with Array Methods

Arrow functions are commonly used with array methods such as map().

const numbers: number[] = [
    1, 2, 3, 4
];

const doubled = numbers.map(
    number => number * 2
);

console.log(doubled);

Output:

[2, 4, 6, 8]

17. Arrow Function with filter()

The filter() method can use an arrow function to select values.

const numbers: number[] = [
    10, 15, 20, 25
];

const result = numbers.filter(
    number => number > 15
);

console.log(result);

Output:

[20, 25]

18. Arrow Function with reduce()

Arrow functions are also commonly used with reduce().

const numbers: number[] = [
    10, 20, 30
];

const total = numbers.reduce(
    (sum, number) => sum + number,
    0
);

console.log(total);

Output:

60

19. Arrow Functions as Callback Functions

An arrow function can be passed as a callback to another function.

const numbers: number[] = [
    10, 20, 30
];

numbers.forEach(
    number => {
        console.log(number);
    }
);

This pattern is very common in modern TypeScript code.

20. Arrow Function with Object

An arrow function can return an object.

type Student = {
    name: string;
    age: number;
};

const createStudent = (
    name: string,
    age: number
): Student => {

    return {
        name: name,
        age: age
    };
};

console.log(
    createStudent("Amit", 20)
);

21. Arrow Function and Type Inference

TypeScript can often infer the return type automatically.

const add = (
    a: number,
    b: number
) => {

    return a + b;
};

TypeScript understands that the return type is number.

22. Arrow Function with Optional Parameter

Arrow functions can use optional parameters.

const greet = (
    name?: string
): string => {

    return name
        ? "Hello " + name
        : "Hello Guest";
};

console.log(
    greet()
);

console.log(
    greet("Amit")
);

23. Arrow Function with Default Parameter

A default value can be assigned to an arrow function parameter.

const greet = (
    name: string = "Guest"
): string => {

    return "Hello " + name;
};

console.log(
    greet()
);

console.log(
    greet("Rahul")
);

24. Arrow Functions in React

Arrow functions are frequently used for React event handlers and component functions.

const handleClick = (): void => {

    console.log("Button clicked");

};

This function can be used as an event handler.

25. Arrow Functions in React Native

React Native applications commonly use arrow functions for button handlers and other actions.

const handlePress = (): void => {

    console.log(
        "Button Pressed"
    );

};

This function can be connected to a React Native Button.

26. Arrow Function with Rest Parameters

Arrow functions can also use rest parameters.

const add = (
    ...numbers: number[]
): number => {

    return numbers.reduce(
        (sum, number) =>
            sum + number,
        0
    );
};

console.log(
    add(10, 20, 30)
);

27. Common Arrow Function Mistake

When using curly braces, you need return if you want to return a value.

const square = (
    number: number
) => {

    return number * number;
};

Without return, the function does not return the calculated value.

28. Traditional Function vs Arrow Function

Traditional function:

function add(
    a: number,
    b: number
): number {

    return a + b;
}

Arrow function:

const add = (
    a: number,
    b: number
): number => {

    return a + b;
};

The arrow function provides a shorter syntax.

29. Complete Practical Example

Here is a practical student result example using an arrow function.

const calculatePercentage = (
    total: number,
    marks: number
): number => {

    return (
        marks / total
    ) * 100;
};

const percentage =
    calculatePercentage(
        500,
        425
    );

console.log(
    "Percentage: " +
    percentage
);

Output:

Percentage: 85

30. Arrow Functions Summary

Arrow functions provide a concise way to create functions in TypeScript.

const add = (
    a: number,
    b: number
): number => a + b;

They are especially common with array methods, callbacks, React components, event handlers, and React Native applications.

📌 Key Points

  • Arrow functions use the => symbol.
  • They provide a shorter function syntax.
  • Parameters can have TypeScript type annotations.
  • Return types can be explicitly specified.
  • Single-expression arrow functions can use implicit return.
  • Curly braces usually require an explicit return.
  • Arrow functions work well with map(), filter(), reduce(), and forEach().
  • They can use optional, default, and rest parameters.
  • They are commonly used in React and React Native.
  • TypeScript can infer many arrow function types automatically.

🧠 Quick Quiz

Question: Which symbol is used to create an arrow function?