TypeScript - Returning a Function

Functions may also return value along with control, back to the caller. Such functions are called as returning functions.


function function_name():return_type { 
   return value; 
  • The return_type can be any valid data type.

  • A returning function must end with a return statement.

  • A function can return at the most one value. In other words, there can be only one return statement per function.

  • The data type of the value returned must match the return type of the function.


//function defined 
function greet():string { //the function returns a string 
   return "Hello World" 

function caller() { 
   var msg = greet() //function greet() invoked 

//invoke function 
  • The example declares a function greet(). The function’s return type is string.

  • Line function returns a string value to the caller. This is achieved by the return statement.

  • The function greet() returns a string, which is stored in the variable msg. This is later displayed as output.

On compiling, it will generate following JavaScript code −

//Generated by typescript 1.8.10
//function defined
function greet() {
   return "Hello World";
function caller() {
   var msg = greet(); //function greet() invoked
//invoke function

The output of the above code is as follows −

Hello World
Kickstart Your Career

Get certified by completing the course

Get Started