Site Loader
Auckland, New Zealand
In our last post we discussed how to work with Variable, Enums and Arrays in Typescript. In this post we will discuss working with different types of functions

Functions

Functions are the fundamental building block of any applications in JavaScript. They’re how you build up layers of abstraction, mimicking classes, information hiding, and modules. In TypeScript, while there are classes, namespaces, and modules, functions still play the key role in describing how to do things. TypeScript also adds some new capabilities to the standard JavaScript functions to make them easier to work with

Function Parameters

  1. Parameter with types
  2. Rest parameters
  3. Default parameters

Parameters with Types

This function looks something like this
//Function declaration
function GetStudentsListGenerics(students: Array){
students.forEach(element => {
console.log("Age:" + element.Age + " with Name:" + element.Name + " has Phone:" +element.Phone + " knows language:"+ element.Language );
});
}

Rest Parameters

//Rest Parameters
function GetNumbers(...nums: number[]){
    nums.forEach(element => {
        console.log("Number: " + element);
    });
}

Default Parameters

//Default Parameters
function GetInfo(info:string = "Happy"){
    console.log(info);
}

Anonymous Functions

Functions which don’t have Name/identifiers are considered as Anonymous functions. These functions are dynamic and called during runtime
//Anonymous function
let StudentName = function (lName:string, fName:string){
    return fName + "..." + lName;
}

console.log(StudentName("KK", "Karthik"));

Arrow Functions

Lambda refers to anonymous functions in programming. Lambda functions are a concise mechanism to represent anonymous functions. These functions are also called as Arrow functions.
//Arrow Function
let StudentFullName = (lName:string, fName:string) => {return fName + ".." + lName};

console.log(StudentFullName("KK", "Prashanth"));
Here is the complete video of the above discussion

Anonymous and Arrow Functions

Thanks for reading the post and watching the video!!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

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