470,599 Members | 1,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

undefined array

in javascript, is there a way to test if an array is undefined????
eg...
for (var x=0; x<10; x++){
if test_array[x] is undefined then x++
document.write(test_array[x])

if anyone can help, thanks... oh and if you can get the syntex right for me
thanks..

Jul 23 '05 #1
7 2203
OzThor wrote:
in javascript, is there a way to test if an array is undefined????

eg...
for (var x=0; x<10; x++){
if test_array[x] is undefined then x++
document.write(test_array[x])

if anyone can help, thanks... oh and if you can get the syntex right for me
thanks..


for (var x = 0; x < 10; x++) {
if (typeof test_array[x] == 'undefined') {
continue;
}

document.write(test_array[x]);
}

or

for (var x = 0; x < 10; x++) {
if (typeof test_array[x] != 'undefined') {
document.write(test_array[x]);
}
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #2
"OzThor" <ro*****@ncable.net.au> writes:
in javascript, is there a way to test if an array is undefined????
If it's undefined, then it isn't an array :)
eg...
for (var x=0; x<10; x++){
if test_array[x] is undefined then x++
document.write(test_array[x])


Ah, you want to know whether the array element is undefined.

for (var i=0;i<10;i++) {
if (typeof test_array[i] != "undefined") {
document.write(test_array[i]);
}
}

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
Vax
[...]
for (var x = 0; x < 10; x++) {
if (typeof test_array[x] != 'undefined') {
document.write(test_array[x]);
}
}


or

for(var x,y;x<10;x++)if(test_array[x]!=y)document.write(test_array[x])

(y is declared, but undefined)

v.
Jul 23 '05 #4
Vax
for(var y,x=0;x<10;x++)if(test_array[x]!=y)document.write(test_array[x])

(y is declared, but undefined)

v.

Jul 23 '05 #5
Vax wrote:
for(var
y,x=0;x<10;x++)if(test_array[x]!=y)document.write(test_array[x])

(y is declared, but undefined)


Using the type-converting comparison - != - means that - null - values
will also be true in - if(test_array[x] != y) - and - null - values are
not undefined. Either a typeof test or the strict (non-type
converting) - !== - comparison are probably the only way to truly
identify undefined array members.

Richard.
Jul 23 '05 #6
Vax

Użytkownik "Richard Cornford" <Ri*****@litotes.demon.co.uk> napisał w
wiadomo¶ci news:c8*******************@news.demon.co.uk...
Vax wrote:
for(var
y,x=0;x<10;x++)if(test_array[x]!=y)document.write(test_array[x])

(y is declared, but undefined)


Using the type-converting comparison - != - means that - null - values
will also be true in - if(test_array[x] != y) - and - null - values are
not undefined.


yes, but try:
javascript:x=null;alert(x==undefined)
javascript:var x=0;alert(x==undefined)
javascript:var x=false;alert(x==undefined)

problem is only(?) with "null"...

v.
Jul 23 '05 #7
Vax wrote:
"Richard Cornford" wrote:
Vax wrote:
for(var
y,x=0;x<10;x++)if(test_array[x]!=y)document.write(test_array[x])

(y is declared, but undefined)


Using the type-converting comparison - != - means that - null -
values will also be true in - if(test_array[x] != y) - and - null -
values are not undefined.


yes, but try:
javascript:x=null;alert(x==undefined)
javascript:var x=0;alert(x==undefined)
javascript:var x=false;alert(x==undefined)

problem is only(?) with "null"...


That is what I said, but - null - and - undefined - are still distinct.

Richard.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by lawrence | last post: by
8 posts views Thread by Scott J. McCaughrin | last post: by
19 posts views Thread by Sharath A.V | last post: by
7 posts views Thread by deepak | last post: by
5 posts views Thread by Pseudonyme | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.