Clear tutorial on Functions in TypeScript in under 2 mins

Learn to write the functions in TypeScript and print it in TypeScript

In this tutorial, we will learn how to write our first function using TypeScript. In the earlier tutorials, we have discussed how to get started with TypeScript, variables, arrays in TypeScript. You can access our previous tutorials from the menu. Now let’s get started with the first function in TypeScript.

Functions in TypeScript has similar usage as that of functions in other programming languages like Java, Python, R and so on. In this tutorial, we will demonstrate how to create a simple function that helps in printing a “Hello” followed by the “Name”  message.

Functions in TypeScript
Functions in TypeScript

Following coding is the basic function syntax. The function starts with the keyword function followed by the function name and after which we have curly braces. Inside the curly braces, we will write the statements.

function name(){}

Now let us create the function which acts as discussed above. 

function greetings(person){
	return "Hello, " + person;
}

Now the function is created. The next step is to input the person name dynamically and print it. The following code will do the task at hand. 

let user = "John Doe";
console.log(greetings(user));

The Output will be as follows.

Hello, John Doe

Now that you have learnt about Getting started with TypeScript. You can also check our tutorial on Interfaces In TypeScript. Check our Angular with Angular Material Training from Ampersand Academy


Comments

Leave a Reply

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