Undefined array element becomes defined and null

Discussion in 'Javascript' started by Tim Streater, Dec 18, 2011.

  1. Tim Streater

    Tim Streater Guest

    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
    Tim Streater, Dec 18, 2011
    #1
    1. Advertising

  2. Tim Streater

    J.R. Guest

    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.)
    J.R., Dec 19, 2011
    #2
    1. Advertising

  3. Tim Streater

    Tim Streater Guest

    In article <>,
    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
    Tim Streater, Dec 19, 2011
    #3
  4. Tim Streater

    Evertjan. Guest

    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)
    Evertjan., Dec 19, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    938
    Juha Nieminen
    Aug 22, 2007
  2. AAaron123
    Replies:
    9
    Views:
    618
    AAaron123
    Mar 18, 2009
  3. Stef Mientki

    and becomes or and or becomes and

    Stef Mientki, May 22, 2011, in forum: Python
    Replies:
    9
    Views:
    300
    Chris Angelico
    May 28, 2011
  4. Replies:
    15
    Views:
    171
    Brian McCauley
    Sep 23, 2007
  5. Taylor Venable

    variable bewilderingly becomes undefined

    Taylor Venable, Oct 17, 2007, in forum: Perl Misc
    Replies:
    14
    Views:
    160
    Taylor Venable
    Oct 18, 2007
Loading...

Share This Page