In TypeScript, just like in JavaScript, you can set default values for function parameters. This feature allows functions to be called with fewer arguments than they are defined to accept. If a parameter is not provided when the function is invoked, its default value will be used instead.
Default parameters are particularly useful for making functions more flexible and reducing the need for conditional checks within the function body.
When you define a function in TypeScript (or JavaScript), you can specify default values for some or all of its parameters. These defaults come into play when the function is called without providing arguments for those specific parameters.
Here's the basic syntax for defining default parameters:
function functionName(param1 = defaultValue1, param2 = defaultValue2) {
// Function body
}
In this example, `param1` will use `defaultValue1` if no argument is passed for it when the function is called. Similarly, `param2` will use `defaultValue2`.
## Examples
Let's explore some practical examples to understand how default parameters work in TypeScript.
### Example 1: Basic Usage
```typescript
function greet(name = "Guest") {
return `Hello, \${name}!`;
}
console.log(greet()); // Output: Hello, Guest!
console.log(greet("Alice")); // Output: Hello, Alice!
In this example, the greet function has a default parameter name set to "Guest". When greet() is called without any arguments, it uses the default value. However, when greet("Alice") is called, it overrides the default with the provided argument.
function createPerson(firstName = "John", lastName = "Doe") {
return { firstName, lastName };
}
console.log(createPerson()); // Output: { firstName: 'John', lastName: 'Doe' }
console.log(createPerson("Alice")); // Output: { firstName: 'Alice', lastName: 'Doe' }
console.log(createPerson("Bob", "Smith")); // Output: { firstName: 'Bob', lastName: 'Smith' }
Here, both `firstName` and `lastName` have default values. You can provide one or both arguments when calling the function.
### Example 3: Default Parameters with Expressions
Default parameters can also be expressions:
```typescript
function calculateDiscount(price, discount = price * 0.1) {
return price - discount;
}
console.log(calculateDiscount(100)); // Output: 90 (default 10% discount)
console.log(calculateDiscount(100, 20)); // Output: 80 (custom 20% discount)
In this example, the discount parameter defaults to 10% of the price. However, you can override it by providing a custom discount value.
You can even use function calls as default values:
function getGreeting() {
return "Hello";
}
function greetWithCustomMessage(message = getGreeting()) {
console.log(`\${message}, world!`);
}
greetWithCustomMessage(); // Output: Hello, world!
greetWithCustomMessage("Hi"); // Output: Hi, world!
Here, the getGreeting function is called to set the default value for message.
Now that you've learned about default parameters, you might want to explore another powerful feature of TypeScript and JavaScript: Rest Parameters. Rest parameters allow a function to accept any number of arguments as an array, providing even more flexibility in handling input.
Stay tuned for the next section where we dive into rest parameters!