Undefined array element becomes defined and null

T

Tim Streater

I have a JavaScript array, myArray. I happen to know that let's say
element 27 is undefined - it's never been created. It appears that I can
detect this with:

if (myArray[27]==null)
{
alert (myArray[27]);
}


The alert puts up 'undefined'. Is it valid to be able to detect the
undefined stater of element 27 in this way?

Initially I thought perhaps that the act of accessing the element
created it and set it to null, but apparently not if the alert is
anything to go by. Or is this just a quirk of Safari?
 
J

J.R.

I have a JavaScript array, myArray. I happen to know that let's say
element 27 is undefined - it's never been created. It appears that I can
detect this with:

if (myArray[27]==null)
{
alert (myArray[27]);
}


The alert puts up 'undefined'. Is it valid to be able to detect the
undefined stater of element 27 in this way?

Initially I thought perhaps that the act of accessing the element
created it and set it to null, but apparently not if the alert is
anything to go by. Or is this just a quirk of Safari?

In this particular case, we should use the strict Equals Operator (===)
than just the Equals Operator (==), because undefined == null, for
instance, produces true, whereas undefined === null yields false.

Note: Douglas Crockford's advice is to never use [what he calls] the
evil twins (== and !=). Instead, we should always use === and !==,
although Crockford's advice is a tad exaggerated when dealing with the
typeof operator which always returns a string value.

Another important thing: if we access a missing array element, we will
get the undefined value, not null. So, the OP's code might be rewritten to:

var missingElem = myArray[27];
if (typeof missingElem == 'undefined') {
alert('this is a missing element in myArray');
}
 
T

Tim Streater

Stefan Weiss said:
I have a JavaScript array, myArray. I happen to know that let's say
element 27 is undefined - it's never been created. It appears that I can
detect this with:

if (myArray[27]==null)
{
alert (myArray[27]);
}


The alert puts up 'undefined'. Is it valid to be able to detect the
undefined stater of element 27 in this way?

Not quite. First of all, when you're comparing something to null or
undefined, always use the strict comparison operator:

myArray[27] === null // false
myArray[27] == null // true
Righto.
Initially I thought perhaps that the act of accessing the element
created it and set it to null, but apparently not if the alert is
anything to go by. Or is this just a quirk of Safari?

There's no such thing as autovivification of array elements or object
properties in JS (thank god).

So no magic then - good!

Joao and Stefan - thanks both for a clear summary.
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 19 dec 2011 in comp.lang.javascript:
It should be noted that ECMA-262 Ed. 5 finally abolished the
overwritable `undefined' property of the global object. From ECMA-252
Ed. 5.1:

What is the sense in making THE global object undefined,
except to render the script useless?

What would happen to your

window.alert('nonsense');

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top