meaning of (toto)? true : false

U

Une Bévue

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 ???
 
U

Une Bévue

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...
 
M

Michael Haufe (TNO)

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
 
U

Une Bévue

Le 29/10/2011 23:47, Michael Haufe (TNO) a écrit :
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 !
 
H

Hans-Georg Michna

? 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
 
A

Andreas Bergmaier

Hans-Georg Michna said:
? 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
 
T

Thomas 'PointedEars' Lahn

Andreas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top