TypeScript – Function Returning an Array

returning

In this topic, we will discuss returning an array function in Typescript

Allows a function to return an array.

Example

function disp():string[] { 
   return new Array("zafrul","subrat","Jay","ajit") 
} 
 
var nums:string[] = disp() 
for(var i in nums) { 
   console.log(nums[i]) 
}

On compiling, it will generate following JavaScript code −

//Generated by typescript 1.8.10 
function disp() { 
   return new Array("zafrul", "subrat", "Jay", "ajit"); 
} 
var nums = disp(); 
for (var i in nums) { 
   console.log(nums[i]); 
}

Its output is as follows −

zafrul
subrat
Jay
ajit

Previous Page:-Click Here

Leave a Reply