Arrow functions
Short syntax
Arrow functions drop the function keyword and use a fat arrow between parameters and body. One expression body returns its value with no braces and no return word. Braces return for multi step bodies.
const add = (a: number, b: number): number => a + b; const double = (n: number): number => { const two: number = 2; return n * two; }; console.log(add(1, 2)); console.log(double(3));
3
6
Lexical this
A classic function gets its own this value, which often surprises inside callbacks. An arrow keeps the this value from the surrounding scope instead. Methods that schedule work or handle events stay bound with no extra code.
class Timer { seconds: number = 0; tick(): void { setTimeout(() => { this.seconds += 1; console.log(this.seconds); }, 10); } } new Timer().tick();
1
Inline callbacks
Arrows shine as short callbacks passed directly into the call. The logic sits next to the operation it serves. Names exist only when the same callback is reused in several places.
const names: string[] = ["ada", "bob", "cy"]; const upper: string[] = names.map((n) => n.toUpperCase()); console.log(upper.join(", "));
ADA, BOB, CY
When arrows win
Prefer arrows for callbacks, array helpers, and promise chains. Prefer named function declarations for top level units and recursive logic. Match the shape to the job and every reader will follow.
function fetchIds(): number[] { return [1, 2, 3]; } const labels: string[] = fetchIds().map((id) => "id-" + id); console.log(labels.join(", "));
id-1, id-2, id-3
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does an arrow function with an expression body return?
Answer
The value of the expression, with no
returnwordA body without braces is one expression, and its value is the result of the call.
-
Which
thisdoes an arrow function use?Answer
The
thisof the scope around itThe arrow has no
thisof its own, so a method that schedules work keeps its instance. -
Which job fits a named function declaration better than an arrow?
Answer
A top level unit that calls itself recursively
Named declarations read well as top level units, while arrows shine as inline callbacks.
-
What does this program print?
const add = (a: number, b: number): number => a + b; console.log(add(1, 2));Answer
3
The expression body returns the sum of the two arguments.
-
What does this program print?
const double = (n: number): number => { const two: number = 2; return n * two; }; console.log(double(3));Answer
6
The block body returns the product, so the call prints 6.
-
What does this program print?
const names: string[] = ["ada", "bob", "cy"]; const upper: string[] = names.map((n) => n.toUpperCase()); console.log(upper.join(", "));Answer
ADA, BOB, CY
Each name is upper cased by the inline arrow, and
joinputs the separator between them. -
What does this program print?
const nums = [1, 2, 3]; const squares = nums.map((n) => n * n); console.log(squares.join(","));Answer
1,4,9
The arrow squares each item and
joinuses the comma without a space. -
What does the compiler say about this program?
class Counter { count = 0; start(): void { setTimeout(function () { this.count += 1; console.log(this.count); }, 10); } } new Counter().start();Answer
error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
A classic function gets its own
this, so understrictthe compiler refuses the barethisinside it. -
What does the compiler say about this program?
const double = (n: number): number => { n * 2; }; console.log(double(3));Answer
error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
The braces make a block body, so the expression is not returned and the annotated type has no value.
-
What does this program print?
const names = ["ada", "bob", "cy"]; const short = names.filter((n) => n.length > 2); console.log(short.join(","));Answer
ada,bob
The arrow keeps the names longer than two characters, and
joinprints them.
Article author: Arthur Isaev