From: Evertjan. on

Are ! and ==false equivalent? NO

<script type='text/javascript'>

alert('' == false); // true
alert(!''); // true

alert('000' == false); // true
alert(!'000'); // false

</script>

=================================

Could it be that ! and ===false are equvalent? NO

<script type='text/javascript'>

alert('' === false); // false
alert(!''); // true

alert('000' === false); // false
alert(!'000'); // false

</script>

=================================

So what the hell is the buggy logic of this?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: VK on
On Jun 26, 3:56 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> wrote:
> Are ! and ==false equivalent? NO
>
> <script type='text/javascript'>
>
> alert('' == false); // true
> alert(!''); // true
>
> alert('000' == false); // true
> alert(!'000'); // false
>
> </script>
>
> =================================
>
> Could it be that ! and ===false are equvalent? NO
>
> <script type='text/javascript'>
>
> alert('' === false); // false
> alert(!''); // true
>
> alert('000' === false); // false
> alert(!'000'); // false
>
> </script>
>
> =================================
>
> So what the hell is the buggy logic of this?

What exactly illogical do you see? http://www.jibbering.com/faq/notes/type-conversion/#tcBool
"Type conversion rules are even simpler for string to boolean
conversion as all non-empty strings always become true and empty
strings become false."

'000' === false means "are '000' and false of the same type and do
they contain the same value(s)?" What logic you want to apply to make
the answer yes (true)?
From: Stefan Weiss on
On 26/06/10 13:56, Evertjan. wrote:
> Are ! and ==false equivalent? NO
>
> <script type='text/javascript'>
>
> alert('' == false); // true
> alert(!''); // true
>
> alert('000' == false); // true
> alert(!'000'); // false
>
> </script>
>
> =================================
>
> Could it be that ! and ===false are equvalent? NO
>
> <script type='text/javascript'>
>
> alert('' === false); // false
> alert(!''); // true
>
> alert('000' === false); // false
> alert(!'000'); // false
>
> </script>
>
> =================================
>
> So what the hell is the buggy logic of this?

Short answer: none of these are equivalent, so you shouldn't expect them
to return identical results.

The rules for non-strict comparison can be very unintuitive. "000"
evaluates to true in a boolean context, but in your first example,
"000" == false. This can only happen if the string is converted to a
number before the actual comparison. Sure enough, section 11.9.3 in
E262-3 specifies that for a comparison x == y

1) If Type(x) is different from Type(y), go to step 14.
...
14-18) don't apply in our example
...
19) If Type(y) is Boolean, return the result of the comparison
x == ToNumber(y).

Starting again from the top for x == 0

1) If Type(x) is different from Type(y), go to step 14.
...
14-16) don't apply in our example
...
17) If Type(x) is String and Type(y) is Number, return the result of
the comparison ToNumber(x) == y.

Third time's the charm, starting again for 0 == 0

1-6) don't apply
...
7) If x is the same number value as y, return true.

And there you have it: "000" == false

I don't think these rules are very logical or intuitive, but that's how
the language was specified. Personally, I don't even bother to remember
all of this. Don't use non-strict comparison with booleans and the
problem disappears.


--
stefan
From: Evertjan. on
Stefan Weiss wrote on 26 jun 2010 in comp.lang.javascript:

> I don't think these rules are very logical or intuitive, but that's how
> the language was specified. Personally, I don't even bother to remember
> all of this. Don't use non-strict comparison with booleans and the
> problem disappears.

I don't think that is enough for me, Stefan.

(x ==false) returns a boolean based on x after conversion to a boolean.

(! x) shoud do exactly the same.

I showed it does not in the case of x = '000',
so the Q is

is this ment to be so by definition?

or

is this simply a definition mistake?

Since both IE and Chrome have the same irregularity it would not be an
oimplementation mistake.

<FAQENTRY>
Should there be a warning for this strange phenomenon?
</FAQENTRY>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Stefan Weiss on
On 26/06/10 16:48, Evertjan. wrote:
> Stefan Weiss wrote on 26 jun 2010 in comp.lang.javascript:
>
>> I don't think these rules are very logical or intuitive, but that's how
>> the language was specified. Personally, I don't even bother to remember
>> all of this. Don't use non-strict comparison with booleans and the
>> problem disappears.
>
> I don't think that is enough for me, Stefan.
>
> (x ==false) returns a boolean based on x after conversion to a boolean.

Assuming that x is still a string, it will never evaluated as a boolean
in this comparison. Its numerical value will be used instead.

> (! x) shoud do exactly the same.

The ! operator does evaluate its operand in a boolean context. Its
behavior is specified as (equivalent to) ToBoolean(GetValue(x)).

> so the Q is
>
> is this ment to be so by definition?
>
> or
>
> is this simply a definition mistake?

It's been specified and implemented like this. Whether that's good or
bad, or even a mistake, is not for me to decide. Anyway, it's too late
to change it without breaking existing code, so we just have to live
with it.

I think most people would intuitively expect x to be evaluated as a
boolean in the first example. That's why I recommended not to use the ==
operator with booleans; the ! or === operators are much better suited in
such cases.

It can certainly be confusing. There's too much DWIMmery going on with
non-strict comparisons for my taste.


--
stefan