Logo

Home

About

Blog

Contact

Guestbook

Merch/Store

Portfolio

Privacy

TOS

Click to navigate

  1. Home
  2. Joshua R. Lehman's Blog
  3. Optional, Default, and Rest Parameters

Table of contents

  • Share on X
  • Discuss on X

Related Articles

Typing Functions: Parameters and Returns
TypeScript
8m
Dec 18, 2025

Typing Functions: Parameters and Returns

Functions are the building blocks of any program. In TypeScript, typing function parameters and return values helps catch errors early and improves code readability. This guide covers function signatures, typing parameters and returns, void functions, and implicit return types.

#TypeScript#Functions+4
Function Overloading in TypeScript
TypeScript
9m
Dec 28, 2025

Function Overloading in TypeScript

Function overloading allows you to define multiple function signatures for a single implementation in TypeScript. This guide covers how to write overloaded functions, the implementation signature, and practical use cases to improve type safety and clarity.

#TypeScript#Functions+3
Arrow Functions and the 'this' Context
TypeScript
7m
Jan 2, 2026

Arrow Functions and the 'this' Context

Arrow functions in TypeScript and JavaScript have distinct behavior with the 'this' context compared to regular functions. This guide explains context binding, the differences between arrow and regular functions, and when to use each for optimal results.

#TypeScript#Functions+3

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2026 • All rights reserved

Contents

  • Introduction
  • Optional Parameters
  • Default Parameters
  • Rest Parameters
  • Combining Parameter Types
  • Best Practices
  • Common Pitfalls
  • Next Steps
TypeScript

Optional, Default, and Rest Parameters

December 23, 2025•2 min read
Joshua R. Lehman
Joshua R. Lehman
Author
Optional, Default, and Rest Parameters in TypeScript
Optional, Default, and Rest Parameters

Optional, Default, and Rest Parameters#

Functions often need to be flexible in how many arguments they accept. TypeScript provides three powerful features to handle this: optional parameters, default parameters, and rest parameters. These allow you to write versatile functions while keeping type safety intact.

Optional Parameters

Optional parameters let callers omit arguments. Mark a parameter optional by adding a ? after its name.

function greet(name?: string): string {
  return `Hello, ${name ?? "Guest"}!`;
}
 
console.log(greet()); // Hello, Guest!
console.log(greet("Alice")); // Hello, Alice!

Note: Optional parameters must come after all required parameters.

Default Parameters

Default parameters provide a fallback value if the argument is omitted or undefined.

function multiply(a: number, b: number = 1): number {
  return a * b;
}
 
console.log(multiply(5)); // 5
console.log(multiply(5, 2)); // 10

Default parameters are implicitly optional and can simplify your function logic.

Rest Parameters

Rest parameters collect all remaining arguments into an array.

function sum(...numbers: number[]): number {
  return numbers.reduce((total, n) => total + n, 0);
}
 
console.log(sum(1, 2, 3)); // 6
console.log(sum(4, 5)); // 9

Rest parameters must be the last parameter in the function signature.

Combining Parameter Types

You can combine required, optional, default, and rest parameters:

function buildName(
  firstName: string,
  lastName?: string,
  ...titles: string[]
): string {
  let fullName = firstName;
  if (lastName) fullName += ` ${lastName}`;
  if (titles.length) fullName += `, ${titles.join(" ")}`;
  return fullName;
}
 
console.log(buildName("John")); // John
console.log(buildName("John", "Doe")); // John Doe
console.log(buildName("John", "Doe", "PhD", "Esq.")); // John Doe, PhD Esq.

Best Practices

  • Place required parameters first, then optional/default, then rest parameters.
  • Use default parameters to avoid manual checks for undefined.
  • Avoid mixing optional and default parameters in confusing ways.
  • Document your function parameters clearly.

Tip: Use rest parameters to handle variable argument lists cleanly instead of arguments object.

Common Pitfalls

  • Placing optional parameters before required ones causes errors.
  • Forgetting that rest parameters must be last.
  • Using undefined explicitly can override default parameters unintentionally.

Warning: Calling a function with undefined for a default parameter will use the default value, but passing null will not.

Next Steps

Next, explore function overloading in TypeScript to define multiple function signatures for more complex behaviors.


Questions about optional, default, or rest parameters? Share your thoughts or examples in the comments!