Default and Initialized Parameters
Overview
TypeScript is a strongly typed programming language in which everything has a type whether it is a variable or a function.
Now in this article, we will learn about TypeScript Default Parameters. Default parameters are those that allow named parameters to be declared with default values if no value or undefined is passed.
When we declare parameters for a function, then all parameters are required and the user has to pass values to every parameter.
Introduction
According to Programming, a parameter also known as a formal argument is a kind of special variable used in a function to refer to a section of data provided as input to the subroutine. This section of data is the value of the argument by which the function has been called.
An argument is an actual expression that is passed onto a function in the calling statement, whereas the parameter is the variable inside the implementation of the function.
For example, if we define a sub function as sub(x, y): return x-y, then x-y will be the parameters and if we call the above function as sub(5, 4), then 5, 4 will be the arguments.
Default Parameters
The default parameters are used to assign some default value to a variable. In JavaScript, we are not required to pass the required argument to the called function because it internally manages the passing and declaration of default values. But in TypeScript, we can use default parameters to assign such argument some value. Also, we don't have to assign the type to the default parameter.
Syntax of Default Parameters
In the above syntax, we have defined a function and passed the default value to the function using the default parameter, so its value will not be null or undefined.
Advantages of Default Parameters
- They help in reducing the overall size of the TypeScript code.
- They help in improving the consistency of the program.
- These parameters are useful if we want to increase the ability of an existing function by adding another default parameter.
Rules & Regulations for Default Parameters
We should keep in mind some points when we are using default parameters in our code :
- We have to assign a default value to the default parameter.
- We are not required to provide any type for the default parameter.
- We can pass multiple default parameters inside the same function.
- There is no specific symbol or keyword which is used to define the default parameter, as they are just like another normal variable in TypeScript.
- We are required to pass all the default parameters at the end of other parameters, otherwise, it may give an error and wrong value in the output.
Examples of Default Parameters
Using Single Default Parameter in Function Signature :
Output :
Using Multiple Default Parameters in Function Signature :
Output :
Optional Parameters
When we initialize parameters for a function, then all the parameters are required and you have to pass values to every parameter. If you don't have any value for any parameter, then you can pass a null value. TypeScript provides an "optional parameters" feature. By using this feature, you can declare some parameters in the function optional, so that the user will not be required to pass the value to optional parameters.
Syntax
In the above-given syntax, there are two parameters : par1 is the default parameter, and par2 is an optional parameter that is appended with a ?. This parameter should come after all non-optional parameters.
Example
Output :
In the above example, we have defined a function display() with two parameters one is marked optional. When we call the function by passing only one value, then it will take the value of the optional parameter as undefined because in the function we are printing both the values of the parameter that is marked as optional also.
Conclusion
- By using the default parameter, we can assign a value to the variable. The value assigned value will be the same for all other variables, and after each function call, the value will remain the same.
- Use the default parameter syntax parameter:=defaultValue if we want to set the default initialized value for the parameter.
- Default parameters are optional.
- To use the default initialized value of a parameter, we have to exclude the argument when calling the function or pass the undefined into the function.
- We came to know about Optional Parameters and their working with a simple example.
- Hope this article has provided you with the relevant information regarding Default & Initialized Parameters and their importance in the TypeScript code.