Javatpoint Logo
Javatpoint Logo

TypeScript Function

Functions are the fundamental building block of any applications in JavaScript. It makes the code readable, maintainable, and reusable. With functions, we can build up layers of abstraction, mimicking classes, information hiding, and modules. In TypeScript, while we have the classes, namespaces, and modules, functions still are an integral part in describing how to do things. TypeScript also allows adding new capabilities to the standard JavaScript functions to make the code easier to work.

Advantage of function

There are mainly two advantages of functions.

  • Code reusability: We can call a function several times, so it saves coding.
  • Less coding: Functions makes our program compact. So, we don't need to write many lines of code each time to perform a common task.

Function Aspects

There are three aspects of a function.

  • Function declaration: A function declaration tells the compiler about the function name, function parameters, and return type. The syntax of the function declaration is given below.
  • Function definition: It contains the actual statements which are to be executed. It specifies what and how a specific task would be done. The syntax of the function definition is given below.
  • Function call: Function can be called from anywhere in the program. The parameter/argument cannot differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration. The syntax of the function call is given below.

Function Creation

In TypeScript, a function can be created in two ways. They are-

  • Named function
  • Anonymous function

Named function

When we declared and called a function by its given name, then this type of function is known as a named function.

Syntax:

Example

Output:

Hello JavaTpoint!

Anonymous 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.

Syntax:

Example:

Output:

8

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.

Syntax

Example

Output:

ID: 101  Name: Virat Kohli
ID: 105  Name: Sachin
Email-Id: sachin@javatpoint.com

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.

Syntax

Example

Output:

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.

Syntax

Example

Output:

8
24




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA