First "undefined" then null and now "null"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Don Vaillancourt

    #1

    First "undefined" then null and now "null"

    What's going on with Javascript.

    At the beginning there was the "undefined" value which represented an
    object which really didn't exist then came the null keyword. But
    yesterday I stumbled across "null" string.

    I know that I will get an "undefined" when I try to retrieve something
    from the DOM which doesn't exist.

    I have used null myself to initialize or reset variables. But in which
    case would Javascript return a null or "null".

    Are there any other return values out there that I may not know about?

    Thanks


    --
    * Don Vaillancourt
    Director of Software Development
    *
    *WEB IMPACT INC.*
    phone: 416-815-2000 ext. 245
    fax: 416-815-2001
    email: donv@web-impact.com <mailto:donv@we bimpact.com>
    web: http://www.web-impact.com



    / This email message is intended only for the addressee(s)
    and contains information that may be confidential and/or
    copyright. If you are not the intended recipient please
    notify the sender by reply email and immediately delete
    this email. Use, disclosure or reproduction of this email
    by anyone other than the intended recipient(s) is strictly
    prohibited. No representation is made that this email or
    any attachments are free of viruses. Virus scanning is
    recommended and is the responsibility of the recipient.
    /
  • Michael Winter

    #2
    Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

    On Wed, 27 Oct 2004 09:52:53 -0400, Don Vaillancourt <donv@webimpact .com>
    wrote:
    [color=blue]
    > What's going on with Javascript.[/color]

    A rather vague question.
    [color=blue]
    > At the beginning[/color]

    At the beginning of what?
    [color=blue]
    > there was the "undefined" value which represented an object which really
    > didn't exist then came the null keyword.[/color]

    What are you actually describing here?

    The value, undefined, is encountered when you would expect it - when
    something hasn't been defined.

    For example,

    alert(window.aN onExistantVaria ble);

    would yield undefined, as would

    function myFunction() {
    return;
    }

    alert(myFunctio n());

    because no return value was specified.

    The null value is, in my mind, used to signify that there is no object,
    when an object reference is expected. For example, document.getEle mentById
    returns a reference to an element within the document. However, if no
    element with the specified id can be found, you'll get null instead.
    [color=blue]
    > But yesterday I stumbled across "null" string.[/color]

    [snip]
    [color=blue]
    > I have used null myself to initialize or reset variables. But in which
    > case would Javascript return a null or "null".[/color]

    There is no built-in or host method that would return the string, 'null',
    to signify the result of an operation.
    [color=blue]
    > Are there any other return values out there that I may not know about?[/color]

    Again, a very strange question. There is no "standard set" of return
    values. An API function may return anything it says it will in its
    specification, and a user-defined function can return anything at all. A
    string, a number, an object (user-defined, regular expression, date, or
    function), an array, a boolean, undefined, or null.

    I doubt that helped, but as I'm not sure what you're asking, such a result
    is not too surprising.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • VK

      #3
      Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

      The rules are nearly the same as in other languages.

      Undefined value means that the requested object/property was never
      initialized (doesn't exist).
      window.alert(do cument.qwertyui op) > 'undefined'
      Thus you cannot assign undefined value - any assignement (even null) makes
      an initialization, so it is not undefined anymore.

      Null value can be assigned to an object/property. It means what it means -
      null, nothing.
      var nothing = null;
      window.alert(no thing) > 'null';

      By their nature undefined and null are very different.
      In higher level languages you usually have means to check if a value is
      undefined (never was init) or null (explisetly set to nothing). Respectively
      you have to use the corresponding comparison methods.
      In JavaScript they just simplified the comparison rules, so you can check
      the existence of an object like this:
      if (obj1.obj2 == null)
      which is really should be read something like
      if undefined(obj1. obj2)

      As a consequence there is no direct way in JavaScript to check against
      "never initialized"/"set equal to null".



      Comment

      • Don Vaillancourt

        #4
        Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

        Well I am talking about the standard implemented methods in Javascript
        for both Mozilla and MSIE.

        Whenever you try to access a property that doesn't exist Javascript will
        return the "undefined" string. That I understand.

        I have run into some Javascript methods returning null as well which I'm
        okay with and always testing for.

        But yesterday I ran into a "null" string which I found odd. I can't
        recall which method I was calling to get this result. But I'll look for
        it today as I have written a method to test against all three.

        Michael Winter wrote:[color=blue]
        > On Wed, 27 Oct 2004 09:52:53 -0400, Don Vaillancourt
        > <donv@webimpact .com> wrote:
        >[color=green]
        >> What's going on with Javascript.[/color]
        >
        >
        > A rather vague question.
        >[color=green]
        >> At the beginning[/color]
        >
        >
        > At the beginning of what?
        >[color=green]
        >> there was the "undefined" value which represented an object which
        >> really didn't exist then came the null keyword.[/color]
        >
        >
        > What are you actually describing here?
        >
        > The value, undefined, is encountered when you would expect it - when
        > something hasn't been defined.
        >
        > For example,
        >
        > alert(window.aN onExistantVaria ble);
        >
        > would yield undefined, as would
        >
        > function myFunction() {
        > return;
        > }
        >
        > alert(myFunctio n());
        >
        > because no return value was specified.
        >
        > The null value is, in my mind, used to signify that there is no object,
        > when an object reference is expected. For example,
        > document.getEle mentById returns a reference to an element within the
        > document. However, if no element with the specified id can be found,
        > you'll get null instead.
        >[color=green]
        >> But yesterday I stumbled across "null" string.[/color]
        >
        >
        > [snip]
        >[color=green]
        >> I have used null myself to initialize or reset variables. But in
        >> which case would Javascript return a null or "null".[/color]
        >
        >
        > There is no built-in or host method that would return the string,
        > 'null', to signify the result of an operation.
        >[color=green]
        >> Are there any other return values out there that I may not know about?[/color]
        >
        >
        > Again, a very strange question. There is no "standard set" of return
        > values. An API function may return anything it says it will in its
        > specification, and a user-defined function can return anything at all.
        > A string, a number, an object (user-defined, regular expression, date,
        > or function), an array, a boolean, undefined, or null.
        >
        > I doubt that helped, but as I'm not sure what you're asking, such a
        > result is not too surprising.
        >
        > Mike
        >[/color]


        --
        * Don Vaillancourt
        Director of Software Development
        *
        *WEB IMPACT INC.*
        phone: 416-815-2000 ext. 245
        fax: 416-815-2001
        email: donv@web-impact.com <mailto:donv@we bimpact.com>
        web: http://www.web-impact.com



        / This email message is intended only for the addressee(s)
        and contains information that may be confidential and/or
        copyright. If you are not the intended recipient please
        notify the sender by reply email and immediately delete
        this email. Use, disclosure or reproduction of this email
        by anyone other than the intended recipient(s) is strictly
        prohibited. No representation is made that this email or
        any attachments are free of viruses. Virus scanning is
        recommended and is the responsibility of the recipient.
        /

        Comment

        • Martin Honnen

          #5
          Re: First &quot;undefined &quot; then null and now &quot;null&quot ;



          VK wrote:

          [color=blue]
          > Undefined value means that the requested object/property was never
          > initialized (doesn't exist).
          > window.alert(do cument.qwertyui op) > 'undefined'
          > Thus you cannot assign undefined value - any assignement (even null) makes
          > an initialization, so it is not undefined anymore.[/color]

          That is not true, you can certainly do
          var x = undefined;
          if you want to (unless you happen to have some old JScript
          implementation where "undefined is undefined" but with JScript 5.5 or
          later that shouldn't happen).


          --

          Martin Honnen
          http://JavaScript.FAQTs.com/

          Comment

          • Michael Winter

            #6
            Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

            On Wed, 27 Oct 2004 16:46:00 +0200, VK <schools_ring@y ahoo.com> wrote:

            [snip]
            [color=blue]
            > As a consequence there is no direct way in JavaScript to check against
            > "never initialized"/"set equal to null".[/color]

            Yes, there is:

            1) A strict comparison:

            null === ref

            2) Using typeof:

            'undefined' == typeof ref

            The former will only evaluate to true if ref is null. The latter will only
            evaluate to true if ref is undefined. If you're concerned about early NN4
            support, the latter is preferred.

            An alternative to 1) is:

            !ref && ('object' == typeof ref)

            If ref is undefined or null, the first part of the expression will
            evaluate to true, but only null is of type, object.

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Don Vaillancourt

              #7
              Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

              I do remember in IE I used to have to define:

              undefined = "undefined"

              because it didn't exist at the time but in did in Netscape 4.7.


              Martin Honnen wrote:
              [color=blue]
              >
              >
              > VK wrote:
              >
              >[color=green]
              >> Undefined value means that the requested object/property was never
              >> initialized (doesn't exist).
              >> window.alert(do cument.qwertyui op) > 'undefined'
              >> Thus you cannot assign undefined value - any assignement (even null)
              >> makes
              >> an initialization, so it is not undefined anymore.[/color]
              >
              >
              > That is not true, you can certainly do
              > var x = undefined;
              > if you want to (unless you happen to have some old JScript
              > implementation where "undefined is undefined" but with JScript 5.5 or
              > later that shouldn't happen).
              >
              >[/color]


              --
              * Don Vaillancourt
              Director of Software Development
              *
              *WEB IMPACT INC.*
              phone: 416-815-2000 ext. 245
              fax: 416-815-2001
              email: donv@web-impact.com <mailto:donv@we bimpact.com>
              web: http://www.web-impact.com



              / This email message is intended only for the addressee(s)
              and contains information that may be confidential and/or
              copyright. If you are not the intended recipient please
              notify the sender by reply email and immediately delete
              this email. Use, disclosure or reproduction of this email
              by anyone other than the intended recipient(s) is strictly
              prohibited. No representation is made that this email or
              any attachments are free of viruses. Virus scanning is
              recommended and is the responsibility of the recipient.
              /

              Comment

              • Martin Honnen

                #8
                Re: First &quot;undefined &quot; then null and now &quot;null&quot ;



                Don Vaillancourt wrote:
                [color=blue]
                > I do remember in IE I used to have to define:
                >
                > undefined = "undefined"[/color]

                You should do
                var undefined = void 0
                but as said that was necessary with older JScript versions and is no
                longer in JScript 5.5 or 5.6 (as installed with IE 5.5 or IE 6).

                --

                Martin Honnen
                http://JavaScript.FAQTs.com/

                Comment

                • Don Vaillancourt

                  #9
                  Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                  Now that I remember, I used to test against undefined in Netscape, but
                  MSIE returned "undefined" . Now it seems "undefined" is returned in all
                  cases.

                  Don Vaillancourt wrote:
                  [color=blue]
                  > I do remember in IE I used to have to define:
                  >
                  > undefined = "undefined"
                  >
                  > because it didn't exist at the time but in did in Netscape 4.7.
                  >
                  >
                  > Martin Honnen wrote:
                  >[color=green]
                  >>
                  >>
                  >> VK wrote:
                  >>
                  >>[color=darkred]
                  >>> Undefined value means that the requested object/property was never
                  >>> initialized (doesn't exist).
                  >>> window.alert(do cument.qwertyui op) > 'undefined'
                  >>> Thus you cannot assign undefined value - any assignement (even null)
                  >>> makes
                  >>> an initialization, so it is not undefined anymore.[/color]
                  >>
                  >>
                  >>
                  >> That is not true, you can certainly do
                  >> var x = undefined;
                  >> if you want to (unless you happen to have some old JScript
                  >> implementation where "undefined is undefined" but with JScript 5.5 or
                  >> later that shouldn't happen).
                  >>
                  >>[/color]
                  >
                  >[/color]


                  --
                  * Don Vaillancourt
                  Director of Software Development
                  *
                  *WEB IMPACT INC.*
                  phone: 416-815-2000 ext. 245
                  fax: 416-815-2001
                  email: donv@web-impact.com <mailto:donv@we bimpact.com>
                  web: http://www.web-impact.com



                  / This email message is intended only for the addressee(s)
                  and contains information that may be confidential and/or
                  copyright. If you are not the intended recipient please
                  notify the sender by reply email and immediately delete
                  this email. Use, disclosure or reproduction of this email
                  by anyone other than the intended recipient(s) is strictly
                  prohibited. No representation is made that this email or
                  any attachments are free of viruses. Virus scanning is
                  recommended and is the responsibility of the recipient.
                  /

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                    Don Vaillancourt <donv@webimpact .com> writes:
                    [color=blue]
                    > Well I am talking about the standard implemented methods in Javascript
                    > for both Mozilla and MSIE.[/color]

                    There are about a gazillion of those, with all kinds of return values.
                    Do you have a specific example?
                    [color=blue]
                    > Whenever you try to access a property that doesn't exist Javascript
                    > will return the "undefined" string. That I understand.[/color]

                    No, it will return the undefined value. It is a specific value that
                    satisfies:
                    typeof <that value> == "undefined"
                    It is also the only value that satisfies this (it is the only value
                    of that type).

                    In modern ECMAScript implementations , there is a global variable called
                    "undefined" that holds the undefined value.
                    [color=blue]
                    > I have run into some Javascript methods returning null as well which
                    > I'm okay with and always testing for.[/color]

                    Javascript DOM methods are usually specified as returning null when
                    there is no reasonable return value. Remember that DOM is also defined
                    for Java where there is no undefined value, and static typing means
                    that the function must return a value matching some class.
                    [color=blue]
                    > But yesterday I ran into a "null" string which I found odd.[/color]

                    I assume that is a string value (typeof value == "string") with
                    length four.
                    [color=blue]
                    > I can't recall which method I was calling to get this result. But
                    > I'll look for it today as I have written a method to test against
                    > all three.[/color]

                    If you find it, do tell.

                    /L 'and please don't top post'
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • VK

                      #11
                      Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                      > !ref && ('object' == typeof ref)


                      \ | /
                      ( )
                      | |

                      Tried to draw a light bulb sign above :-)
                      Mais oui, of course! I should think about it earlier.


                      Comment

                      • Grant Wagner

                        #12
                        Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                        Don Vaillancourt wrote:
                        [color=blue]
                        > But yesterday I ran into a "null" string which I found odd. I can't
                        > recall which method I was calling to get this result. But I'll look for
                        > it today as I have written a method to test against all three.[/color]

                        null has typeof "object":

                        var a = null;
                        alert(typeof a); // "object"

                        This probably isn't technically correct, but it's how I view it:

                        JavaScript does object type-conversion when necessary. The -null- "object"
                        should be no different. If you use a variable containing -null- in a way that
                        requires a string, it is type-converted to a string (I just like to think
                        that toString() will be called implicitly - it can't be called explicitly by
                        you however) and you get the string consisting of the letters n, u, l and l.

                        var a = null;
                        if ("" + a == "null") alert('yes');
                        if (a == null) alert('yes');

                        Both alert "yes" because in the first example, -a- was concatenated to an
                        empty string. JavaScript type-converts the -object- null to the -string- (or
                        calls toString() on it, or however it achieves the conversion) containing the
                        character sequence n, u, l, l, which, when compared to another string
                        containing that character sequence, results in true.

                        So my suspicion is that you used a variable containing a null value in a way
                        that required it to be type-converted into a string.

                        --
                        Grant Wagner <gwagner@agrico reunited.com>
                        comp.lang.javas cript FAQ - http://jibbering.com/faq

                        Comment

                        • Steve van Dongen

                          #13
                          Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                          Don Vaillancourt <donv@webimpact .com> wrote:
                          [color=blue]
                          >Now that I remember, I used to test against undefined in Netscape, but
                          >MSIE returned "undefined" . Now it seems "undefined" is returned in all
                          >cases.[/color]

                          Nothing returns the string "undefined" to indicate an undefined value.

                          Regards,
                          Steve

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: First &quot;undefined &quot; then null and now &quot;null&quot ;

                            Steve van Dongen wrote:
                            [color=blue]
                            > Don Vaillancourt <donv@webimpact .com> wrote:[color=green]
                            >> Now that I remember, I used to test against undefined in Netscape, but
                            >> MSIE returned "undefined" . Now it seems "undefined" is returned in all
                            >> cases.[/color]
                            >
                            > Nothing returns the string "undefined" to indicate an undefined value.[/color]

                            That's not true. The "typeof" operator returns "undefined" if its operand
                            is an undefined value in both JavaScript 1.1+ and JScript, as specified in
                            ECMAScript.


                            PointedEars
                            --
                            how many <alt><e>s does it take to end that task?

                            Comment

                            Working...