Loading

TypeScript

What is the Function type in TypeScript ?. The Complete TypeScript Developer Course 2022 [Videos].

TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance.

It uses type inference to provide powerful tools without the need for extra code. TypeScript may be executed everywhere JavaScript is supported as it can be converted to JavaScript code. 

TypeScript Functions: Functions are the most crucial aspect of JavaScript as it is a functional programming language. Functions are pieces of code that execute specified tasks. They are used to implement object-oriented programming principles like classes, objects, polymorphism, and abstraction. It is used to ensure the reusability and maintainability of the program. Although TypeScript has the idea of classes and modules, functions are still an important aspect of the language.

Function declaration: The name, parameters, and return type of a function are all specified in a function declaration. The function declaration has the following:

 Syntax:

function functionName(arg1, arg2, ... , argN);

Function definition: It includes the actual statements that will be executed. It outlines what should be done and how it should be done. The function definition has the following

 Syntax:

function functionName(arg1, arg2, ... , argN){
// Actual code for execution
}

Function call: A function can be called from anywhere in the application. In both function calling and function definition, the parameter/argument must be the same. We must pass the same number of parameters that the function definition specifies. The function call has the following 

Syntax:

functionName(arg1, arg2, ... , argM);

Types of Functions in TypeScript: There are two types of functions in TypeScript:

  • Named Function
  • Anonymous Function

1. Named function: A named function is defined as one that is declared and called by its given name. They may include parameters and have return types. 

Syntax:

functionName( [args] ) { }  

Example: 



// Named Function Definition  
function myFunction(x: number, y: number): number {
  return x + y;
}
  
// Function Call  
myFunction(7, 5);  

Output:

12

2. Anonymous Function: An anonymous function is a function without a name. At runtime, these kinds of functions are dynamically defined as an expression. We may save it in a variable and eliminate the requirement for function names. They accept inputs and return outputs in the same way as normal functions do. We may use the variable name to call it when we need it. The functions themselves are contained inside the variable.

Syntax: 

let result = function( [args] ) { }  

Example:

// Anonymous Function  
let myFunction = function (a: number, b: number) : number {  
    return a + b;  
};  
  
// Anonymous Function Call  
console.log(myFuction(7, 5));

Output:

12

Advantage of function: Benefits of functions may include but are not limited to the following:

  • Code Reusability: We can call a function several times without having to rewrite the same code block. The reusability of code saves time and decreases the size of the program.
  • Less coding: Our software is more concise because of the functions. As a result, we don’t need to write a large number of lines of code each time we execute a routine activity.
  • Easy to debug: It makes it simple for the programmer to discover and isolate incorrect data.

See All

Comments (166 Comments)

Submit Your Comment

See All Posts

Related Posts

TypeScript / Blog

Difference between TypeScript and JavaScript

When JavaScript was developed the JavaScript development team introduced JavaScript as a client-side programming language. But when people were using JavaScript then developers get to know that JavaScript can be used as a server-side programming language also. But When JavaScript was growing then the code of JavaScript became complex and heavy.
19-jan-2022 /33 /166

TypeScript / Blog

What is the difference between interface and type in TypeScript ?

Both the methods Type and the Interface are used to describe the structure of the objects in TypeScript. But holds some specific features that will be helpful according to the situation, choosing between them and totally depends on the developer.
/33 /166

TypeScript / Blog

What is the Function type in TypeScript ?

TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance.
19-jan-2022 /33 /166