Click to See Complete Forum and Search --> : Test if array exists


Nedals
03-23-2003, 08:03 PM
I thought this was simple. I must be missing something. Here's what I want to do, but it doesn't work.

if (someAry == undefined) {
alert("There's no array");
} else {
alert("There is an array");
}

dabush
03-23-2003, 08:55 PM
me wondering same thing

AdamBrill
03-23-2003, 09:46 PM
Well, this is how I do it. ;) Put this at the top of your script:

ArrayName=null;

Then, you can check if it is an array like this:

if(ArrayName){
//do something...
}else{
//do something else...
}

If the ArrayName is declared as an array after the top line, it will return true. Otherwise it will return false. I hope that helps... :)

dabush
03-23-2003, 09:55 PM
TY

Nedals
03-23-2003, 10:29 PM
Adam,
Thanks for the response but, alas, it doesn't solve the problem. I'm using an external script that may or may not contain arrays. So the array cannot be pre-declared. In Perl this is done using 'exists' and I was hoping that there was something similar in Javascript. I read though the documentation but found nothing. I do have another way to solve the problem (by creating an array of arrays) but I was trying to do it this way.

dabush: What's TY. I noticed another recent post that was titled TY. I think it was a response to another thread that I had responded to.

AdamBrill
03-23-2003, 10:42 PM
Something made me think that is what you meant... :) Anyway, this is the only way that I know of doing that:window.onerror=go;
if(test){
alert("test2");
}
function go(){
return true;
}
It just captures the errors... The problem with that is it captures all errors, not just that one, so I don't know if it will work or not...

TY stands for Thank You... ;)

Nedals
03-23-2003, 10:53 PM
TY.
I guess it saves typing. So little time :)

Anyway, I think I'll go with the array-of-arrays

AdamBrill
03-23-2003, 11:00 PM
Originally posted by nedals
TY.
I guess it saves typing. So little time :) LOL :D
Originally posted by nedals
Anyway, I think I'll go with the array-of-arrays Probably a good idea... Normally it is a bad idea to catch all of the errors... ;)