By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

undefined array

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.