Javatpoint Logo
Javatpoint Logo

Function Parameter

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:

  • Optional Parameter
  • Default Parameter
  • Rest Parameter

Optional Parameter

In JavaScript, we can call a function without passing any arguments. Hence, in a JavaScript function, the parameter is optional, and when we do this, each parameter value is undefined.

Unlike JavaScript, the TypeScript compiler will throw an error if we try to invoke a function without providing the exact number and types of parameters as declared in its function signature. To overcome from this problem, we can use optional parameters by using question mark sign ('?'). It means that the parameters which may or may not receive a value can be appended with a '?' to mark them as optional. In below example, e_mail_id is marked as an optional parameter.




ID: 101  Name: Virat Kohli
ID: 105  Name: Sachin

Default Parameter

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!

Rest Parameter

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:

  • Only one rest parameter is allowed in a function.
  • It must be an array type.
  • It must be a last parameter in the parameter list.




Next TopicTypeScript Classes

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA