Ternary statement [was Re: [Javascript] Show and hide multiple div tags]
Triche Osborne
wdlists at triche-osborne.com
Sat May 13 16:10:06 CDT 2006
liorean wrote:
> On 13/05/06, Triche Osborne <wdlists at triche-osborne.com>
>
>> The point was that it has to be part of a completed statement--that is,
>> the returned results must be used in some way.
>
>
> No, they don't. Expressions may be Statements in and of themselves if
> they appear as ExpressionStatements. The difference is that then the
> return value is automatically discarded.
>
> Wrong. They are both fully valid ExpressionStatements, and thereby
> they do form complete statements. They are pointless since they
> discard the return value, but they are complete statements.
>
Forgive me. I'm not trying to be pugnacious, and I do recognize that
everything you've said is technically correct. However, I teach new
scripters and their biggest problem with this form is in distinguishing
between a useful statement that performs some action and an expression
that produces disregarded results. That is, they do something like this
. . .
navigator.platform == 'MacPPC'
? 'command click to do some stuff'
: 'ctrl click to do some stuff';
and expect something to happen, only, of course, nothing does.
Perhaps I should not have assumed so, but since the questioner was
unfamiliar with the form, I thought he might run into the same problem,
though perhaps I shouldn't have used such absolute terms.
>
> Oh, and just for the record: "ternary operator" means
> operator-with-three-operands.
I'm aware of that. The word "ternary" isn't even a programming-specific
term, but a general term for anything with three parts, whether that
thing is an operator or a musical chord. (I suppose that if any one
"owns" it, musicians have dibs.)
I have heard and seen the terms "ternary," "ternary conditional" and
"conditional" used interchageably to describe this operator, and not
just in JavaScript. I was first introduced to it as the "ternary
operator" (this was not in JavaScript), and so tend to call it that.
Triche
More information about the Javascript
mailing list