Updated March 28, 2023
Introduction to TypeScript Functions
Any programming language has its own set of predefined functions. And some functions are going to be defined by us. Functions are the main building blocks of any programming paradigm. If you know JavaScript functions then it is a lot easier to know about typescript functions. Typescript is a superset of JavaScript and for typescript, you need to be familiar with its ES6 syntax.
How to Define TypeScript Functions?
There are two types of functions named and anonymous which do not have any name.
Syntax:
function add(a,b) {
return a*b;
}
We will also see the same example for anonymous functions.
Const add = (a,b)=> a*b
But now, you may say that, what are the advantages we have in typescript over JavaScript then? Well, one of the core features is that typescript as the name suggests is the type. Yes, you can give type to the function also.
Example:
function add(a: number ,b: number): number {
return a*b;
}
How to Call TypeScript Functions?
If you are writing any function, then it has to be called to execute it. Writing function is known as a function definition in programming. We are calling the function with the function name and a pair of parenthesis.
For example, let’s take the above add function. We will call that function as below.
add(5,10); // we are providing values for the given parameters.
How to Return TypeScript Functions?
In this example you can see there are many situations where we have used return keyword is just to return value from it. We can return any type of value. If you want to return something from a function at that time you must use return statement with the semicolon. We can only return one and only one value from a single function. While returning the value we must check that returning value and type function should be the same so that the caller get the return value.
Example:
function cake():string {
//function defination
//the function returns a string
return "Welcome to the world of cakes"
}
function bake() {
var bakery = cake();
console.log(bakery);
}
bake()
Look at the above example carefully, here we have two functions one is bake and the other is cake. We are calling function cake inside the function bake. And function cake returning the string value. In the bake function, we are assigning our functional call to the variable bakery. Then we log that variable to the console. Upon executing this we will get “Welcome to the world of cakes ” as an output. It is easy to understand with little practice.
What is Parameterized TypeScript Functions?
The parameter is the case where a user has to give some value to work on. The parameter has to be considered important if mentioned. We must have to give respective values to each parameter in typescript. In programming, while working with parameterized functions no arguments should be matched with no of arguments.
Example:
function student(fName: string, lName: string) {
return fName + " " + lName;
}
let student1= student ("John", "roy");
Here, one important note to understand is that what if we do not give all arguments. or the case where we are not having data to give value for that parameter, in this situation, we have the concept of the optional parameter in typescript. These optional parameters use to show as this is optional.
We will look at the example. Take the same function student and we will try to make some parameters optional.
Example:
function student(fName: string, lName?: string) {
return fName + " " + lName;
}
let student1= student ("John");
In above example we just gave value for first name parameter. And it will work without the second parameter. Now it will not show any error.
Now, other than this if we wanted to give some default value to the parameter then we can do that.
Example:
function student(fName: string, lName: string = "rey") {
return fName + " " + lName;
}
let student1= student ("John");
Now, the above program will work correctly without any errors. It is fine to give a default value to the parameter.
Examples of TypeScript Functions
Following are the examples as given below:
Example #1
fat arrow function:
const msg = ()=>"Welcome to the typescript demo";
Here in the above example, we used an arrow function. Note that here we are not using the return statement because as per the convention if we have only one statement and i.e return statement then we don’t need to write it explicitly. This is shorthand syntax and is most commonly used in typescript.
Example #2
We can call one function as many times as we want, look at the below example to know more.
function student(fName: string, lName?:string) {
return fName + " " + lName;
}
student ("John");
student ("sam");
student ("ken");
student ("bren");
And simultaneously we made last name parameter optional. So that we don’t need to give the second parameter.
Example #3
Another example we are going to look for rest parameters. Till now we have seen parameters like optional, default and required. But there is one more case to this. What if we don’t know how much parameters the function will exactly take.
We will make use of the ellipsis (…) operator here. Again we will take the above example of a student.
function student(fName: string, …remainigStudents: string [] ) {
return fName + " " + remainigStudents.join(" ");
}
let allStudents = student("john", "sam", "reema", "kerry", "jem");
If you look closely we have given n no of arguments. But in a function definition, we just gave two parameters. But look at that three dots(…0) before the second parameter. It is called ellipsis. Which helps us do so.
Writing functions and work on them needs some debugging skills. For working with functions you need to understand the ES6 features of JavaScript.
Conclusion
Working with functions is the most important thing because by combining these functions we are designing the functionality. Functions in typescript are referred to as ES6 syntax. This is mostly seen with the arrow functions. ES6 is an ECMASript standard for JavaScript. We also have ES7 now and more releases will come in the future with some new features.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Functions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.