How to create an array if it doesn't exist in TypeScript

1 Answer

0 votes
if (typeof arr === 'undefined' || !Array.isArray(arr)) {
  	var arr : any = []; // var = arr will be accessible outside the if block
}

console.log(arr);

  
  
  
  
/*
run:
  
[]
  
*/

 



answered Jul 6, 2022 by avibootz
...