TypeScript Functions

A function is a set of statements execute to achieve a specific task.

we will discuss on:

  1. basic function
  2. function with optional param
  3. function with rest param
  4. Anonymous function

1. Basic function:


code example:

output: 9

2. Function with optional param:

  In the below example, email is the optional param as it it marked with ‘?’.

    An optional param does not need argument to pass. in below example e-mail as arg not been passed to method and still execute successfully.

Output: details are shri 123

3. Function with rest param:
  • This function is same as variable arg in java.you
  • It does not defined the number of param.
  • It uses syntax  ” ...variable:dataypeas param.
  • The rest param work on only one type of data type. eg. all param can be considered for rest param if they doing for all number or string or array.

example code:

Output: 9

4. Anonymous function :

A function with no name and just body definition called Anonymous function.

for example:

Output: hello world

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top