I have a javascript query like this that is meant to check both undefined arrays and empty arrays:
if(array){
if(array.length > 0){
function1();
}else{
function2();
}
}else{
function2();
}
I've tried something like
if(array && ( array.length > 0)){
function1();
}else{
function2();
}
but no dice, get a variable is undefined error. According to this answer doing if(variable) works just fine for this use-case, but this only works on strings rather than arrays. Is there any way to do two-in-one in javascript in a case like this?
if(array && ( array.length > 0)){should work fine. If you get an error, it's unrelated orarrayis not declared (maybe you get aReferenceError?)arrayis supposed to come from? As long as you are not integrating two independent scripts somehow, any variable should be at least declared. Please provide more context.