ProgramGuru

Arrow Functions

Understand Arrow Functions

Arrow functions offer a concise way to write function expressions using =>. They are common for small transformations and callbacks.

What you will learn: the purpose of arrow functions, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how arrow functions works and where its type rules apply.
  • Predict the exact output of the arrow functions example.
  • Diagnose a common issue: For a block-bodied arrow, forgetting return produces undefined at runtime and can be rejected by the declared return type.

Build the mental model

The expression form returns its expression without the return keyword. An arrow function with braces must return explicitly to produce a value. Arrow functions also capture this lexically rather than creating their own dynamic this binding.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Arrow Functions: explained visuallyArguments → Arrow expression → Returned valueArgumentsArrow expressionReturned valueFollow the values through the operation
Arrow Functions: Arguments → Arrow expression → Returned value.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

const square = (value: number): number => value * value;
const add = (a: number, b: number): number => {
  return a + b;
};
console.log(square(5));
console.log(add(3, 4));

Verified output

25
7

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

square(5) evaluates the single expression 5 × 5. add(3, 4) enters the block and explicitly returns 7. Both functions produce values for console.log.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

For a block-bodied arrow, forgetting return produces undefined at runtime and can be rejected by the declared return type.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Rewrite square with braces and return; then create an isEven arrow function with a boolean return type.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

How does an expression-bodied arrow return its result?
Choose one answer. You can try again.

Answer and explanation

The expression is returned implicitly.

Arrow functions offer a concise way to write function expressions using =>. They are common for small transformations and callbacks.

Review the key idea

Arrow Functions: Arrow functions offer a concise way to write function expressions using =>.

square(5) evaluates the single expression 5 × 5. add(3, 4) enters the block and explicitly returns 7. Both functions produce values for console.log.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Function Parameters →