Contenido del Curso
Introduction to TypeScript
Introduction to TypeScript
Default Values
Another feature of functions in TypeScript is default parameter values. Default values are often used alongside or in place of optional parameters. You can set a default value for a parameter, and if you don't specify a value for that parameter when calling the function, it will be initialized with the default value.
Let's take a look at an example:
function createBankTransaction(amount: number, description: string = "No description provided") { console.log(`Transaction: ${description}, Amount: $${amount}`); } createBankTransaction(100); // No description provided: Transaction: No description provided, Amount: $100 createBankTransaction(200, "Deposit"); // With provided description: Transaction: Deposit, Amount: $200
Here's the simplest example of a function for conducting bank transactions. This function will provide us with a notification of the transaction amount and its description.
Users don't always like to provide descriptions for their transactions, so this field is optional. As you can see, to specify a default value that tells us there's no description for the transaction, we used the equal sign and the value we want to pass as the default. Therefore, if this parameter is not defined when calling the function, the default value will be used. Let's take a look at the syntax again:
Also, it's worth noting that you can use any number of different parameters in functions. You can use 2 optional parameters, 31 default parameters, or even 792 regular parameters. I wouldn't recommend using more than 792 parameters, or people might look at you with disdain. Let's take a look at a similar example:
function orderProduct(productName: string, quantity: number = 1, discount?: number, shippingMethod = "Standard", giftWrap = false, deliveryDate = "Tomorrow"): void { let totalCost = 0; // Calculate the product cost let basePrice = 100; // Cost per unit of the product if (discount !== undefined) { basePrice -= discount; } totalCost = basePrice * quantity; // Add shipping cost if (shippingMethod === "Express") { totalCost += 10; } // Add gift wrapping cost if (giftWrap) { totalCost += 5; } console.log(`Order Details: Product: ${productName} Quantity: ${quantity} Shipping Method: ${shippingMethod} Gift Wrap: ${giftWrap} Delivery Date: ${deliveryDate} Total Cost: $${totalCost}`); } // Function call examples: orderProduct("Laptop"); // Defaults to 1 unit, standard shipping, no discount, no gift wrapping orderProduct("Smartphone", 2, 20, "Express", true); // Order 2 smartphones with a 20% discount, express shipping, and gift wrapping orderProduct("Headphones", 3, undefined, "Standard", false, "Next week"); // Order 3 headphones with no discount, standard shipping, and delivery next week
In this example:
productName
is a required parameter;quantity
,discount
,shippingMethod
,giftWrap
, anddeliveryDate
all have default values and can be omitted when calling the function;- The
orderProduct
function takes parameters, calculates and prints the total cost of an order, and its details.
This example shows how optional and default parameters can be used to create flexible functions with various usage scenarios.
The code turned out to be quite extensive, but I hope it demonstrates how parameters work in functions. If you understood everything, you're doing great!
¡Gracias por tus comentarios!