Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7804 > unrolled thread

meaning of (toto)? true : false

Started byUne Bévue <unbewusst.sein@fai.invalid>
First post2011-10-29 07:46 +0200
Last post2011-10-31 18:06 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  meaning of (toto)? true : false Une Bévue <unbewusst.sein@fai.invalid> - 2011-10-29 07:46 +0200
    Re: meaning of (toto)? true : false Une Bévue <unbewusst.sein@fai.invalid> - 2011-10-29 07:53 +0200
    Re: meaning of (toto)? true : false "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-29 14:47 -0700
      Re: meaning of (toto)? true : false Une Bévue <unbewusst.sein@fai.invalid> - 2011-10-30 05:37 +0100
    Re: meaning of (toto)? true : false Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2011-10-30 21:38 +0100
      Re: meaning of (toto)? true : false Andreas Bergmaier <andber93@web.de> - 2011-10-30 21:55 +0100
        Re: meaning of (toto)? true : false Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-30 22:00 +0100
      Re: meaning of (toto)? true : false Dr J R Stockton <reply1144@merlyn.demon.co.uk> - 2011-10-31 18:06 +0000

#7804 — meaning of (toto)? true : false

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2011-10-29 07:46 +0200
Subjectmeaning of (toto)? true : false
Message-ID<4eab9340$0$27929$426a34cc@news.free.fr>
if I have:
   var toto;
   console.log("    typeof toto = "+(typeof toto));
   console.log("    toto = "+(toto)? true : false );
   toto=null;
   console.log("    typeof toto = "+(typeof toto));
   console.log("    toto = "+(toto)? true : false );

the result is 'true' in both cases :

typeof toto = undefined
toto = true
typeof toto = object
toto = true

then, if I use the test :

if( toto )
what could I expect in both preceeding cases ?

normaly, afaik, I'd expect first case being undefined to false
second case being null to false also, isn't it ???

[toc] | [next] | [standalone]


#7805

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2011-10-29 07:53 +0200
Message-ID<4eab94d4$0$27929$426a34cc@news.free.fr>
In reply to#7804
Le 29/10/2011 07:46, Une Bévue a écrit :
> if I have:
> var toto;
> console.log(" typeof toto = "+(typeof toto));
> console.log(" toto = "+(toto)? true : false );
> toto=null;
> console.log(" typeof toto = "+(typeof toto));
> console.log(" toto = "+(toto)? true : false );
>
> the result is 'true' in both cases :
>
> typeof toto = undefined
> toto = true
> typeof toto = object
> toto = true
>
> then, if I use the test :
>
> if( toto )
> what could I expect in both preceeding cases ?
>
> normaly, afaik, I'd expect first case being undefined to false
> second case being null to false also, isn't it ???

In fact, if i do, adding parenthesis :
console.log(" toto = "+((toto)? true : false) );

i get the correct respons, as antissipated...

[toc] | [prev] | [next] | [standalone]


#7814

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2011-10-29 14:47 -0700
Message-ID<4f19cb89-b72f-47da-b69d-0762cbe98b1b@hv4g2000vbb.googlegroups.com>
In reply to#7804
On Oct 29, 12:46 am, Une Bévue <unbewusst.s...@fai.invalid> wrote:
> if I have:
>    var toto;
>    console.log("    typeof toto = "+(typeof toto));
>    console.log("    toto = "+(toto)? true : false );
>    toto=null;
>    console.log("    typeof toto = "+(typeof toto));
>    console.log("    toto = "+(toto)? true : false );
>
> the result is 'true' in both cases :
>
> typeof toto = undefined
> toto = true
> typeof toto = object
> toto = true
>
> then, if I use the test :
>
> if( toto )
> what could I expect in both preceeding cases ?
>
> normaly, afaik, I'd expect first case being undefined to false
> second case being null to false also, isn't it ???

Look at this:

toto ? true : false  //is false
"    toto = "+(toto)? true : false   //is true

The problem is operator precedence. The second conditional is
interpreted as the following:

("    toto = "+(toto))? true : false

Which is:

" toto = null" ? true : false //which is true

[toc] | [prev] | [next] | [standalone]


#7818

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2011-10-30 05:37 +0100
Message-ID<4eacd4a0$0$10716$426a74cc@news.free.fr>
In reply to#7814
Le 29/10/2011 23:47, Michael Haufe (TNO) a écrit :
> On Oct 29, 12:46 am, Une Bévue<unbewusst.s...@fai.invalid>  wrote:
>> if I have:
>>     var toto;
>>     console.log("    typeof toto = "+(typeof toto));
>>     console.log("    toto = "+(toto)? true : false );
>>     toto=null;
>>     console.log("    typeof toto = "+(typeof toto));
>>     console.log("    toto = "+(toto)? true : false );
>>
>> the result is 'true' in both cases :
>>
>> typeof toto = undefined
>> toto = true
>> typeof toto = object
>> toto = true
>>
>> then, if I use the test :
>>
>> if( toto )
>> what could I expect in both preceeding cases ?
>>
>> normaly, afaik, I'd expect first case being undefined to false
>> second case being null to false also, isn't it ???
>
> Look at this:
>
> toto ? true : false  //is false
> "    toto = "+(toto)? true : false   //is true
>
> The problem is operator precedence. The second conditional is
> interpreted as the following:
>
> ("    toto = "+(toto))? true : false
>
> Which is:
>
> " toto = null" ? true : false //which is true

clear, fine thanks !

[toc] | [prev] | [next] | [standalone]


#7835

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2011-10-30 21:38 +0100
Message-ID<a6dra7132v6ul7qp4lbsaofmek24geomnv@4ax.com>
In reply to#7804
? true : false
is another way to write,
"I don't understand JavaScript."

If you really have to transform something into a boolean that
isn't already one, just write !! in front of it, but think again
whether that is really needed.

Hans-Georg

[toc] | [prev] | [next] | [standalone]


#7842

FromAndreas Bergmaier <andber93@web.de>
Date2011-10-30 21:55 +0100
Message-ID<j8kdke$uqs$1@news.albasani.net>
In reply to#7835
Hans-Georg Michna schrieb:
> ? true : false
> is another way to write,
> "I don't understand JavaScript."
>
> If you really have to transform something into a boolean that
> isn't already one, just write !! in front of it, but think again
> whether that is really needed.

I'm sure in his application he uses other values like strings. This 
console.log-thing was just to show his problem. Of course he could have 
written:
  console.log("Why is this "+foo ? "true" : "false" + "?")
but does it matter?

  Bergi

[toc] | [prev] | [next] | [standalone]


#7847

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-10-30 22:00 +0100
Message-ID<2198638.IIGME3H9Hf@PointedEars.de>
In reply to#7842
Andreas Bergmaier wrote:

> Hans-Georg Michna schrieb:
>> ? true : false
>> is another way to write,
>> "I don't understand JavaScript."
>>
>> If you really have to transform something into a boolean that
>> isn't already one, just write !! in front of it, but think again
                           ^^^^^^^^^^^^^^^^^^^^^^^
>> whether that is really needed.
> 
> I'm sure in his application he uses other values like strings. This
> console.log-thing was just to show his problem. Of course he could have
> written:
>   console.log("Why is this "+foo ? "true" : "false" + "?")

You appear to have missed the point:

  console.log("Why is this " + !!foo + "?");

> but does it matter?

If it leads to inefficient, error-prone code, then yes.


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#7894

FromDr J R Stockton <reply1144@merlyn.demon.co.uk>
Date2011-10-31 18:06 +0000
Message-ID<483hG3BJOurOFw$e@invalid.uk.co.demon.merlyn.invalid>
In reply to#7835
In comp.lang.javascript message <a6dra7132v6ul7qp4lbsaofmek24geomnv@4ax.
com>, Sun, 30 Oct 2011 21:38:21, Hans-Georg Michna <hans-
georgNoEmailPlease@michna.com> posted:

>? true : false
>is another way to write,
>"I don't understand JavaScript."
>
>If you really have to transform something into a boolean that
>isn't already one, just write !! in front of it, but think again
>whether that is really needed.

One may need more; for example,
        !!   "0"        // true
        !! + "0"        // false

-- 
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web