TypeScript – Passing Arrays to Functions

function

You can pass to the function a pointer to an array by specifying the array’s name without an index.

Example

var names:string[] = new Array("zafrul","subrat","Jay","ajit")  

function disp(arr_names:string[]) {
   for(var i = 0;i<arr_names.length;i++) { 
      console.log(names[i]) 
   }  
}  
disp(names)

On compiling, it will generate following JavaScript code −

//Generated by typescript 1.8.10
var names = new Array("zafrul", "subrat", "Jay", "ajit");
function disp(arr_names) {
   for (var i = 0; i < arr_names.length; i++) {
      console.log(names[i]);
   }
}
disp(names);

Its output is as follows −

zafrul
subrat
Jay 
ajit

Previous Page:-Click here

This Post Has One Comment

Leave a Reply