TypeScript - Passing Arrays to Functions



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("Mary","Tom","Jack","Jill")  

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("Mary", "Tom", "Jack", "Jill");
function disp(arr_names) {
   for (var i = 0; i < arr_names.length; i++) {
      console.log(names[i]);
   }
}
disp(names);

Its output is as follows −

Mary 
Tom 
Jack 
Jill
typescript_arrays.htm
Advertisements