Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts

    var shoe, attr1 = attr2 = attr3 = null; <-- what is this operation?

    var shoe, attr1 = attr2 = attr3 = null;

    I have some old legacy code that I am going thru, and I don't understand that line above....when would you use something like this?

    Is the var attr1 assigning itself to attr2 etc.....

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Those statements are overkill, because when a variable is declared, its value is automatically set to null, but what is being done is the shoe variable is being declared, and attr1 is being defined as the value of attr2, whose value is attr3, whose value is null (so attr1, attr2, and attr3 all equal null).

    Hope that helps!

    Happy coding!

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by nolachrymose
    when a variable is declared, its value is automatically set to null
    No, its value is undefined.
    Verify it quickly by typing this in the address bar:

    javascript:var blah;alert(blah);

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's a bit of semantics, Glenn, but you're right. Although, if you try this:

    Code:
    javascript:var blah;alert(blah==null);
    You get true.

    Happy coding!

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    They are only the same when used in equality operator.
    Null and Undefined

    The difference between null and undefined is a little subtle. The null keyword represents "no value," meaning "nothing," not even an empty string or zero. It is a type of JavaScript object (see Chapter 8, "Objects"). It can be used to initialize a variable so that it does not produce errors or to clear the value of a variable, so that there is no longer any data associated with that variable, and the memory used by it is freed. When a variable is assigned null, it does not contain any valid data type.

    A variable that has been declared, but given no initial value, contains the value undefined and will produce a runtime error if you try to use it. (If you declare the variable and assign null to it, null will act as a placeholder and you will not get an error.) The word undefined is not a keyword in JavaScript. If compared with the == equality operators, null and undefined are equal, but if compared with the identity operator, they are not identical.

    ---http://www.phptr.com/articles/article.asp?p=169501

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    (this is all assuming the variable undefined has not been defined elsewhere)

    null === undefined; // false

    == typecasts arguments before comparing... I wouldn't call:
    "1" == 1; // true
    just semantics, which is analagous to what you said earlier about null == undefined.

    typeof null; // object
    typeof undefined; // undefined
    Last edited by jkd; 06-17-2004 at 06:50 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •