D
Dave Anderson
Trevor said:I understood that, but the other repsonses have confused me
The rest of the discussion has been about the nuances of coercion
(http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion) in
JScript.
JScript will coerce several values to false. According to the MS description
of the Boolean Object, these are: 0 (numeric zero), null, NaN or "" (empty
string):
boolObj = new Boolean([boolValue])
boolValue
Optional. The initial Boolean value for the new object.
If Boolvalue is omitted, or is false, 0, null, NaN, or
an empty string, the initial value of the Boolean object
is false. Otherwise, the initial value is true.
http://msdn.microsoft.com/library/en-us/script56/html/d67748f2-7bf5-4889-8269-e777616cc5f0.asp
What this does not explicitly state is the fact that the special value
[undefined] also coerces to false (it is actually implicit in omission of
boolValue). You can illustrate this point for yourself:
var x
alert(x) // undefined
alert(new Boolean(x)) // false
It is worth noting what else this definition lacks. Consider:
alert(new Boolean(false)) // false
alert(new Boolean(new Boolean(false))) // true
Why is the second one true? HINT:
typeof false == "boolean"
typeof new Boolean(false) == "object"
So the description of coercion to boolean ought to state that certain TYPES
have values that coerce to false:
object : null
number : 0 or NaN
string : ""
boolean : false
undefined : undefined
Here's where it gets fun. The following are true:
(0 == 0)
("0" == 0)
(new Boolean(0).valueOf() == new Boolean(0).valueOf())
While this is false:
(new Boolean("0").valueOf() == new Boolean(0).valueOf())
Think about that. The equality operator coerces "0" and 0 into the same type
for comparision**, so they "look" equal. But the Boolean Object coerces them
to opposite values.
**If you want to know the type, ask yourself what you will get if you try
this: alert(0.0=="0"). Then test it.
--
Dave Anderson
Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.