codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

14 / 60 topics
8Functions in TypeScript9Function Parameters10Return Types11Optional Parameters12Default Parameters13Rest Parameters14Arrow Functions
Tutorials/TypeScript/Arrow Functions
🔷TypeScript

Arrow Functions

Updated 2026-05-15
10 min read

Arrow Functions

Introduction

In TypeScript, as in JavaScript, functions are first-class citizens. This means that they can be assigned to variables, passed as arguments to other functions, and returned from other functions. One of the modern features introduced in ES6 (ECMAScript 2015) is the arrow function syntax, which provides a more concise way to write functions compared to traditional function expressions.

Arrow functions are particularly useful for writing shorter and cleaner code, especially when dealing with anonymous functions or callbacks. They also have some differences in behavior compared to traditional functions, such as how they handle this.

Concept

Syntax

The basic syntax of an arrow function is:

(param1, param2, ..., paramN) => { statements }

For a single parameter, you can omit the parentheses:

```typescript
param => { statements }

If the function body consists of a single expression, you can omit the curly braces and the `return` keyword:

```typescript
(param1, param2, ..., paramN) => expression

Examples

Let's start with some basic examples to illustrate how arrow functions work.

Example 1: Basic Arrow Function

Here is a simple example of an arrow function that takes two parameters and returns their sum:

TypeScript
1const add = (a: number, b: number) => a + b;
2console.log(add(2, 3)); // Output: 5

Example 2: Arrow Function with Single Parameter

If the function has only one parameter, you can omit the parentheses:

TypeScript
1const greet = name => `Hello, ${name}!`;
2console.log(greet('Alice')); // Output: Hello, Alice!

Example 3: Arrow Function with Expression Body

If the function body is a single expression, you can omit the curly braces and the return keyword:

TypeScript
1const square = (num: number) => num * num;
2console.log(square(4)); // Output: 16

Example 4: Arrow Function with No Parameters

If the function takes no parameters, you need to use empty parentheses:

TypeScript
1const sayHello = () => 'Hello!';
2console.log(sayHello()); // Output: Hello!

Differences from Traditional Functions

One of the key differences between arrow functions and traditional functions is how they handle this. Arrow functions do not have their own this context; instead, they inherit this from the parent scope.

Example 5: this in Traditional Function vs. Arrow Function

Consider the following example to understand the difference:

TypeScript
1class Counter {
2count = 0;
3
4increment() {
5 setTimeout(function() {
6 console.log(this.count); // Output: undefined
7 }, 1000);
8}
9
10incrementArrow() {
11 setTimeout(() => {
12 console.log(this.count); // Output: 1
13 }, 1000);
14}
15}
16
17const counter = new Counter();
18counter.increment();
19counter.incrementArrow();

In the increment method, the traditional function inside setTimeout does not have access to the count property of the Counter class because it has its own this context. In contrast, the arrow function in incrementArrow correctly captures the this from the parent scope (Counter instance).

What's Next?

In this tutorial, we covered the basics of arrow functions in TypeScript, including their syntax and some practical examples. In the next section, we will explore interfaces, which are a powerful feature for defining object types in TypeScript.

Stay tuned!


PreviousRest ParametersNext Interfaces in TypeScript

Recommended Gear

Rest ParametersInterfaces in TypeScript