Зміст курсу
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:
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:
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!
Все було зрозуміло?
Зміст курсу
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:
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:
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!
Все було зрозуміло?