Ternary statement [was Re: [Javascript] Show and hide multiple div tags]

Triche Osborne wdlists at triche-osborne.com
Sat May 13 13:04:45 CDT 2006


J. Lester Novros II wrote:
> Triche Osborne wrote:
> 
>       [snip snip snip]
> 
>> well.) It's interesting to note that it isn't considered a complete 
>> statement in itself. The following isn't complete:
>>
>> ( var ) ? 'this' : 'that';
> 
> 
> FWIW, strictly speaking, that isn't altogether true. For example, I can 
> do this:
> 
>    document.getElementById('someID').title =
>       navigator.platform == 'MacPPC'
>       ? 'command click to do some stuff'
>       : 'ctrl click to do some stuff';
> 
> Or, assunming someNumber and someOtherNumber are numbers, even this:
> 
>    span.appendChild(
>       someNumber < someOtherNumber
>       ? document.createTextNode('some text')
>       : document.createTextNode('some other text')
>    );
> 
> IOW, the result of ternary statement dosn't necessarily have to be 
> assigned to a variable.

This is why I said "The returned value must be assigned to *something* 
or used in some manner."

The point was that it has to be part of a completed statement--that is, 
the returned results must be used in some way.

In both of your examples above, this is what's happening: In the first, 
the result is assigned to document.getElementById('someID').title; in 
the second, it determines which text is appended to span.appendChild. 
Neither this . . .

navigator.platform == 'MacPPC'
 >       ? 'command click to do some stuff'
 >       : 'ctrl click to do some stuff';

NOR this . . .

 >       someNumber < someOtherNumber
 >       ? document.createTextNode('some text')
 >       : document.createTextNode('some other text')

form a complete statement in and of themselves.

Triche




More information about the Javascript mailing list