Default Parameters

What

Default parameters provide fallback values for function arguments if they are omitted or passed as undefined during a function call.

How

You assign a value directly to the parameter in the function signature. When a default value is used, TypeScript can often infer the parameter type automatically:

function newCharacter(name: string, role: string = "warrior"): string {
  return `${name} is a ${role}`;
}

console.log(newCharacter("Gandalf")); // "Gandalf is a warrior"

Why

Default parameters simplify function calls by removing the need for the caller to provide every single value. They also eliminate the need for manual if (!param) { ... } checks inside the function body, leading to cleaner and more predictable code.

Summary

Unlike optional parameters (?), default parameters do not union undefined into the type, as the parameter will always have a value.