Re: if question
From: Lithium (Lithium_at_discussions.microsoft.com)
Date: 03/14/05
- Next message: Arvind: "Slow Jscript objects response with large number of objects"
- Previous message: Jeff North: "Re: Store ListBox Values"
- In reply to: Lasse Reichstein Nielsen: "Re: if question"
- Next in thread: Steve Fulton: "Re: if question"
- Reply: Steve Fulton: "Re: if question"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Arvind: "Slow Jscript objects response with large number of objects"
- Previous message: Jeff North: "Re: Store ListBox Values"
- In reply to: Lasse Reichstein Nielsen: "Re: if question"
- Next in thread: Steve Fulton: "Re: if question"
- Reply: Steve Fulton: "Re: if question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|