/    Sign up×
Community /Pin to ProfileBookmark

Test if array exists

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”);
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@dabushMar 24.2003 — me wondering same thing
Copy linkTweet thisAlerts:
@AdamBrillMar 24.2003 — 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... ?
Copy linkTweet thisAlerts:
@dabushMar 24.2003 — TY
Copy linkTweet thisAlerts:
@NedalsauthorMar 24.2003 — 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.
Copy linkTweet thisAlerts:
@AdamBrillMar 24.2003 — 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... ?
Copy linkTweet thisAlerts:
@NedalsauthorMar 24.2003 — TY.

I guess it saves typing. So little time ?

Anyway, I think I'll go with the array-of-arrays
Copy linkTweet thisAlerts:
@AdamBrillMar 24.2003 — [i]Originally posted by nedals [/i]

[B]TY.

I guess it saves typing. So little time ?[/B]
[/QUOTE]
LOL ?
[i]Originally posted by nedals [/i]

[B]Anyway, I think I'll go with the array-of-arrays [/B][/QUOTE]
Probably a good idea... Normally it is a bad idea to catch all of the errors... ?
×

Success!

Help @Nedals spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...