TypeScript Default Parameters

In TypeScript, default parameters allow you to assign default values to function parameters. If a value is not passed for that parameter, the default value is used. This feature is helpful for creating more flexible functions without overloading them or checking if parameters are provided.

 

Syntax for Default Parameters

function functionName(param1: type = defaultValue): returnType {
  // function body
}

In this syntax, if param1 is not provided when calling the function, the defaultValue will be used.

Example of Default Parameters

Here’s an example of using default parameters in TypeScript:

function greet(name: string, age: number = 30): string {
  return `Hello ${name}, age ${age}`;
}

console.log(greet("John"));        // Output: Hello John, age 30
console.log(greet("Alice", 25));   // Output: Hello Alice, age 25

In this example:

  • The age parameter has a default value of 30.
  • If no value is provided for age, it defaults to 30.

Default Parameters with Multiple Parameters

You can use default parameters with multiple parameters, where each parameter can have its own default value.

function createUser(name: string, age: number = 18, city: string = "New York"): string {
  return `Name: ${name}, Age: ${age}, City: ${city}`;
}

console.log(createUser("John"));                 // Output: Name: John, Age: 18, City: New York
console.log(createUser("Alice", 25));            // Output: Name: Alice, Age: 25, City: New York
console.log(createUser("Bob", 30, "Los Angeles")); // Output: Name: Bob, Age: 30, City: Los Angeles

Here:

  • age has a default value of 18.
  • city has a default value of "New York".
  • The function behaves differently based on which parameters are provided.

Default Parameters with Optional Parameters

You can combine default parameters with optional parameters. This allows the flexibility to either provide a value or omit it, with a default value being used when omitted.

function displayMessage(message: string, prefix: string = "Info"): string {
  return `${prefix}: ${message}`;
}

console.log(displayMessage("Server is running"));        // Output: Info: Server is running
console.log(displayMessage("Server is running", "Alert")); // Output: Alert: Server is running

In this case:

  • prefix is a default parameter.
  • If the caller doesn't pass a prefix, it defaults to "Info".

Combining Default Parameters with Rest Parameters

You can also combine default parameters with rest parameters. Rest parameters allow you to pass an arbitrary number of arguments, and default parameters will be applied as needed.

function logMessage(level: string = "INFO", ...messages: string[]): void {
  console.log(`[${level}]`, ...messages);
}

logMessage("ERROR", "Something went wrong!");    // Output: [ERROR] Something went wrong!
logMessage("DEBUG", "This is a debug message");  // Output: [DEBUG] This is a debug message
logMessage(undefined, "Default level log");      // Output: [INFO] Default level log

Here:

  • level has a default value of "INFO".
  • If level is not passed, it defaults to "INFO".
  • The messages parameter is a rest parameter that collects all following arguments.

 

Summary

Default parameters in TypeScript provide a way to define values for function parameters that are used when no argument is provided. This feature helps make functions more flexible and concise, reducing the need for additional checks or overloads. By using default values, you can create functions that handle missing data gracefully and enhance the readability of your code.