How to return array from function in TypeScript

2 Answers

0 votes
function f() : number[] {
  let arr = [10, 8, 5, 4, 3, 6, 2, 7, 1, 9];
 
  return arr
}
 

 
console.log(f());
 
 
   
     
     
/*
run:
     
[10, 8, 5, 4, 3, 6, 2, 7, 1, 9] 
     
*/

 



answered Jan 17, 2022 by avibootz
0 votes
function f():string[] { 
   return new Array("c", "c++", "typescript", "python") 
} 
 
var arr:string[] = f() 

for (var i in arr) { 
   console.log(arr[i]) 
}




/*
run:

"c" 
"c++" 
"typescript" 
"python" 

*/

 



answered Jan 17, 2022 by avibootz

Related questions

1 answer 147 views
1 answer 151 views
1 answer 223 views
1 answer 133 views
1 answer 163 views
...