ReviseAlgo Logo

Functions in TypeScript

Optional & Default Parameters

Master optional parameters (?), default parameter values, ordering rules, and handling undefined parameters in TypeScript functions.

Last Updated: July 29, 2026 10 min read

JavaScript allows calling functions with fewer arguments than parameters defined. TypeScript supports this flexibility using Optional Parameters (?) and Default Parameters (param = defaultValue).

1. Optional Parameters (?)

An optional parameter is indicated by placing a ? after the parameter name. At runtime, optional parameters evaluate to undefined if omitted by the caller.

Type Union Behavior

Behind the scenes, lastName?: string expands the parameter type to string | undefined.

2. Parameter Order Rule for Optional Parameters

Optional parameters must come after all required parameters in a function parameter list.

3. Default Parameters (param = defaultValue)

Default parameters allow you to provide a fallback value if the caller passes undefined or omits the argument.

Default Parameters Located Before Required Parameters

Unlike optional parameters, default parameters can appear before required parameters, but callers must explicitly pass undefined to trigger the default value.

4. Destructured Object Parameters with Defaults

When writing functions that take options objects, combine destructuring with default parameter values for clean type safety:

5. Interactive Code Playground

Test optional and default parameters below:

6. Common Pitfalls & Edge Cases

  • Passing null Does NOT Trigger Default Values: Default parameter values are only triggered when passing undefined, NOT null.
  • 7. Comparison Table

    FeatureOptional Parameter (param?: T)Default Parameter (param = defaultVal)
    Expanded TypeT \undefinedInferred as T
    Fallback ValueundefinedUser-defined expression
    Position RestrictionsMust be at the endCan be anywhere (undefined triggers default)
    | Runtime Value if Omitted | undefined | Computed defaultVal |