Re: if question

From: Lithium (Lithium_at_discussions.microsoft.com)
Date: 03/14/05


Date: Mon, 14 Mar 2005 02:57:04 -0800


 

"Lasse Reichstein Nielsen" wrote:

> "=?Utf-8?B?TGl0aGl1bQ==?=" <Lithium@discussions.microsoft.com> writes:
>
> > why is there a difference between if (x) and if (x==true) i thought if (x)
> > is the short form of if (x==true)
>
> It isn't.
>
> When you write
> if (<expression>) ...
> then the expression is evaluated and automatically converted to a boolean
> (which is trivial if it's already a boolean).
>
> So, if you write
> ---
> var x = "test";
> if (x) ...
> ---
> then "x" is evaluted to the string "test", which is then converted to
> the boolean value true (the empty string converts to false, all other
> strings to true).
>
> If instead you write
> ---
> if (x == true) ...
> ---
> then "x == true" is evaluated. First "x" evalutes to the string "test",
> then, since "test" and true are different types, type conversion is
> performed before comparing. In this case, both are converted to numbers,
> which is NaN and 0 respectively, and these are compared and not equal.
>
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> 'Faith without judgement merely degrades the spirit divine.'
>

Let me see if i get it... an empty string to Boolean converted gives false
all other strings gives true.

because if (x) ...requires a boolean inside the string is automatically
converted to boolean..thats clear...

but
if (x==true) ...the string isnt converted to boolean although a boolean
stands on the right ... they are both converted to numbers..,,which in my
opinion makes no sense..
why convert both to numbers when you only have to convert one to the other
and boolean is the simplest type.

so there is nothing in my reference (downloaded in MSDN libary) about type
conversions in COMPARISONS.

can i say:
    String with Boolean ....both are converted to Numbers
    Number with Boolean .... both are converted to Numbers

    every type with Boolean...both converted to Numbers
    every type with Number...different type converted to Number



Relevant Pages

  • Re: if question
    ... >> performed before comparing. ... > and boolean is the simplest type. ... Why convert the string to boolean? ...
    (microsoft.public.scripting.jscript)
  • Re: No coercion in for-each loop?
    ... believer in explicitly marking conversions. ... Boolean (true/false) ... String ... Each has a standard value to which an empty node set automatically ...
    (comp.lang.java.programmer)
  • Re: array "sort()" question, weird php behavior ?
    ... > When comparing a boolean to a number, PHP converts the number to a ... > When comparing a number to a string, PHP converts the string to a ...
    (comp.lang.php)
  • Re: array "sort()" question, weird php behavior ?
    ... When comparing a boolean to a number, PHP converts the number to a ... When comparing a number to a string, PHP converts the string to a ...
    (comp.lang.php)
  • Re: Order of comparision
    ... The production EqualityExpression: EqualityExpression == ... If Typeis Number and Typeis String, ... If Typeis Boolean, return the result of the comparison ToNumber ... So there are quite explicit rules on how to do type conversions where ...
    (comp.lang.javascript)

Loading