0Pricing
TypeScript Academy · Lesson

Functions: params, default/optional/rest; return types

Define functions with parameters, defaults, optionals, rest args, and proper return types.

Functions: params, default/optional/rest; return types is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

Goal: Learn to declare functions with parameters, optional and default values, rest arguments, and return types.

Basic function

Functions declare parameter and return types. Here, greet takes a string and returns a string.

function greet(name: string): string {
  return "Hello " + name;
}

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

Default params

Default params assign a fallback value when the argument is missing. Example: exponent defaults to 2.

function power(base: number, exp: number = 2): number {
  return base ** exp;
}

console.log(power(3));     // default exp=2 → 3**2 = 9
console.log(power(2, 3));  // exp=3 → 2**3 = 8

Optional params

Optional params use "?" to indicate an argument may be omitted. Handle undefined safely.

function lengthOf(text?: string): number {
  return text ? text.length : 0;
}

console.log(lengthOf("TS")); // 2
console.log(lengthOf());     // 0

Rest params

Rest params collect multiple args into an array. Useful for flexible calls.

function sum(...nums: number[]): number {
  return nums.reduce((a, b) => a + b, 0);
}

console.log(sum(1, 2, 3, 4)); // 10

Return types

Always specify return types when clarity matters. TS infers simple cases but explicitness helps APIs.

function isEven(n: number): boolean {
  return n % 2 === 0;
}

console.log(isEven(4)); // true

Optional param check

Quick check: How do you declare an optional parameter?

Recap

Recap: You learned function parameters, defaults, optional args, rest arrays, and explicit return types. These keep your code predictable.

Frequently asked questions

Is the “Functions: params, default/optional/rest; return types” lesson free?

Yes — the full text of “Functions: params, default/optional/rest; return types” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Functions: params, default/optional/rest; return types”?

Define functions with parameters, defaults, optionals, rest args, and proper return types. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Functions: params, default/optional/rest; return types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. If/else, switch, loops; truthiness & strict checks
  2. Functions: params, default/optional/rest; return types
  3. Arrow functions & contextual typing
← Back to TypeScript Academy