Java script Dude wrote:
Now I can understand that 0 equal false, but should "" also equal
false?
Yes, it should.
Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .
Works as designed.
I assume this goes back to the original spec for JavaScript.
Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:
| 11.9.1 The Equals Operator ( == )
|
| The production EqualityExpress ion : EqualityExpress ion ==
| RelationalExpre ssion is evaluated as follows:
|
| 1. Evaluate EqualityExpress ion.
| 2. Call GetValue(Result (1)).
| 3. Evaluate RelationalExpre ssion.
| 4. Call GetValue(Result (3)).
| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)
| 6. Return Result(5).
|
| [...]
|
| 11.9.3 The Abstract Equality Comparison Algorithm
|
| The comparison x == y, where x and y are values, produces true or
| false. Such a comparison is performed as follows:
|
| 1. If Type(x) is different from Type(y), go to step 14.
| 5.2 Algorithm Conventions
|
| [...] Type(x) is used as shorthand for “the type of x”.
| [...]
| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.
| 18. If Type(x) is Boolean, return the result of the comparison
| ToNumber(x) == y.
| 19. If Type(y) is Boolean, return the result of the comparison
| x == ToNumber(y).
| 9.3 ToNumber
|
| The operator ToNumber converts its argument to a value of type Number
| according to the following table:
|
| Input Type Result
| --------------------------
| Undefined NaN
| Null +0
| Boolean The result is 1 if the argument is true.
| The result is +0 if the argument is false.
| Number The result equals the input argument (no conversion).
| String See grammar and note below.
| Object Apply the following steps:
| 1. Call ToPrimitive(inp ut argument, hint Number).
| 2. Call ToNumber(Result (1)).
| 3. Return Result(2).
Therefore ("" == +0):
| 1. If Type(x) is different from Type(y), go to step 14.
| [...]
| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.
| 9.3.1 ToNumber Applied to the String Type
|
| [...]
| A StringNumericLi teral that is empty or contains only white space is
| converted to +0.
Therefore (+0 == +0):
| 1. If Type(x) is different from Type(y), go to step 14.
| 2. If Type(x) is Undefined, return true.
| 3. If Type(x) is Null, return true.
| 4. If Type(x) is not Number, go to step 11.
| 5. If x is NaN, return false.
| 6. If y is NaN, return false.
| 7. If x is the same number value as y, return true.
| 5.2 Algorithm Conventions
|
| When an algorithm is to produce a value as a result, the directive
| “return x” is used to indicate that the result of the algorithm is
| the value of x and that the algorithm should terminate.
Not very intuitive
Yes, it is. For example, it allows for
if (stringValue)
without having to compare against the length of the string or to check for a
whitespace-only string value.
but I guess I can code around this.
Yes, you can. Use the Strict Equality Operator (`===') instead, which does
not perform implicit type conversion; it is well-supported:
http://PointedEars.de/es-matrix
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16