Advantage of function
There are mainly two advantages of functions.
There are three aspects of a function.
In TypeScript, a function can be created in two ways. They are-
When we declared and called a function by its given name, then this type of function is known as a named function.
A function without a name is known as an anonymous function. This type of function is dynamically declared at runtime. It is defined as an expression. This expression is stored in a variable, so it does not need function names. They are always invoked by using the variable name that the function is stored in.
Parameters are the values or arguments that passed to a function. In TypeScript, the compiler accepts the same number and type of arguments as defined in the function signature. If the compiler does not match the same number and type of arguments as defined in the function signature, then it will give the compilation error.
Function parameter can be categories into the following:
ID: 101 Name: Virat Kohli ID: 105 Name: Sachin Email-Id: email@example.com
TypeScript provides an option to set a default value to a function parameter. If the user does not pass the value for that parameter, the default value will be initialized for that parameter. The behavior of the default parameter is the same as an optional parameter. For the default parameter, if a value is not passed in a function call, then the default parameter must follow the required parameters in the function signature.
Note: The parameter cannot be optional and default at the same time.
Hello JavaTpoint! Hi JavaTpoint! Hello Sachin!
The rest parameter is used to pass zero or more values to a function. It is declared by prefixing the three dot characters ('...') before the parameter. It allows the functions to have a variable number of arguments without using the arguments object. The TypeScript compiler will create an array of arguments with the rest parameter, so all array methods work with rest parameter. The rest parameter is very useful where we have an undetermined number of parameters.
Rules to follow in rest parameter: