Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Undefined array element becomes defined and null

Reply
Thread Tools

Undefined array element becomes defined and null

 
 
Tim Streater
Guest
Posts: n/a
 
      12-18-2011
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?

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
 
 
 
J.R.
Guest
Posts: n/a
 
      12-19-2011
On 18/12/2011 21:37, Tim Streater wrote:
> 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');
}


--
Joao Rodrigues (J.R.)
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      12-19-2011
In article <WZOdnXmwnYiSAnPTnZ2dnUVZ8u->,
Stefan Weiss <> wrote:

> On 2011-12-19 00:37, Tim Streater wrote:
> > 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.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-19-2011
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');

?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
and becomes or and or becomes and Stef Mientki Python 9 05-28-2011 02:04 PM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
RAM - 2gb becomes 4gb becomes 2gb b Computer Support 10 04-27-2006 11:58 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
First "undefined" then null and now "null" Don Vaillancourt Javascript 13 12-11-2004 04:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57