TypeScript Tutorial

TypeScript Arrow Functions

An arrow function is a function you write inline: (args) => body.

A function with no function keyword

An arrow function is a function you write at the point of use. The shape is a parameter list, then=>, then a body. In prose that looks like (args) => body. Store it inconst, or pass it straight into map or sort.

C++ lambdas look like [captures](args) { body }. TypeScript arrows do not use a capture list. They close over locals automatically. Annotate parameters. Annotate the return type when the body is more than one expression.

Store an arrow in a const

The type of a function from number to number is written(n: number) => number. That is a type, not a call. The value on the right is the arrow.

Example

const bump = (n: number): number => n + 10;
console.log(bump(5));
console.log(bump(20));

Output is 15 then 30. bump(5) is an ordinary call. A single-expression body after => is the return value. You do not write return on that short form.

Compile this at /typescript/try. Change 10 to 1 and run again. The name bump still points at one function.

A brace body needs return

When the work is more than one expression, use braces. Then you must return if the caller needs a value. Forgetting return makes the function return undefined.

Example

const label = (n: number): string => {
  if (n % 2 === 0) {
    return "even";
  }
  return "odd";
};
console.log(label(8));
console.log(label(7));

Output is even then odd. The return type : string sits before =>.

Pass an arrow into sort

sort takes a comparator. An arrow is the usual way to write that comparison at the call site. You already did this in the algorithms chapter. Here the focus is the syntax, not the array method.

Example

const scores: number[] = [6, 1, 9, 4];
scores.sort((a: number, b: number) => a > b ? 1 : a < b ? -1 : 0);
console.log(scores.join(" "));
scores.sort((a: number, b: number) => b - a);
console.log(scores.join(" "));

First line 1 4 6 9 (the ternary comparator is a long way to write ascending). Second line9 6 4 1 from b - a. Prefer the subtraction form for numbers.

map and forEach

Arrays expect a function of one item. An arrow fits on the same line as the call. Type the item. The return ofmap is a new array. forEach is for side effects such as printing.

Example

const nums: number[] = [1, 2, 3, 4];
const squares: number[] = nums.map((n: number) => n * n);
console.log(squares.join(" "));
nums.forEach((n: number) => {
  console.log(n * n);
});

First line 1 4 9 16. Then four more lines, one square each. Use map when you need the list. Use forEach (or a loop) when you only want to print.

What the pieces mean

PieceMeaning
(n: number)Parameters, with types
: number before =>Return type of the function value
=> n + 10Expression body; that value is returned
=> { return n + 10; }Block body; write return yourself

Zero parameters are () => 1. One untyped parameter may drop the parentheses in JavaScript; this tutorial always writes (n: number) so the type stays visible.

Keep the body small

An arrow that fills a screen belongs in a named function instead. Use an arrow when the work is a few lines and the name would be used once: a sort order, a small transform, a predicate forfind. Next: Date, for a timestamp you construct from a fixed ISO string.

FAQ: TypeScript Arrow Functions

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript arrow functions examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript arrow functions in this TypeScript TypeScript lesson (TypeScript Arrow Functions).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.