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

06-17-2004, 02:33 AM
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.....

06-17-2004, 04:03 AM
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! :)

06-17-2004, 04:40 AM
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);

06-17-2004, 05:35 AM
That's a bit of semantics, Glenn, but you're right. Although, if you try this:

javascript:var blah;alert(blah==null);

You get true. :rolleyes:

Happy coding! :)

06-17-2004, 06:25 AM
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.


06-17-2004, 07:45 PM
(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