[Javascript] Show and hide multiple div tags
Triche Osborne
wdlists at triche-osborne.com
Thu May 11 17:41:42 CDT 2006
Abyss - Information wrote:
> Yes I understand that.
>
> I was just wondering (to which i will yahoo a ternary statement in a
> minute)
>
> why and how does it work with the? and the :
>
The question mark is the ternary operator. Just as an equal sign (the
assignment operator) is parsed by the interpreter as "store this piece
of data at a place designated by this name," the question mark is
intepreted as "if the statement that preceeds this question mark . . ."
The question mark is always immediately followed by the true condition.
The colon is the "else" and is always followed by the false branch.
If you could see it as the interpreter does, it would be something like
this:
(1) var2 = var ? 'this' : 'that';
^ if var is
(2) var2 = var ? 'this' : 'that';
^ TRUE, this
(3) var2 = var ? 'this' : 'that';
^ else
(4) var2 = var ? 'this' : 'that';
^ else FALSE, then this
(5) var2 = var ? 'this' : 'that';
^ assign results to var2
As for the origins, I believe it comes from C, as do a number of
constructs in both JavaScript and PHP. (PHP supports ternary form as
well.) It's interesting to note that it isn't considered a complete
statement in itself. The following isn't complete:
( var ) ? 'this' : 'that';
If must be used as part of a complete statement such as this one:
var2 = (var) ? 'this' : 'that';
The returned value must be assigned to something or used in some manner.
Is this more what you were looking for?
Triche
More information about the Javascript
mailing list