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

1 Answer

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

console.log(arr);

  
  
  
  
/*
run:
  
[]
  
*/

 



answered Jul 6, 2022 by avibootz

Related questions

...