Closed Thread Icon

Preserved Topic: Help understanding statement (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18014" title="Pages that link to Preserved Topic: Help understanding statement (Page 1 of 1)" rel="nofollow" >Preserved Topic: Help understanding statement <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-01-2001 04:44

Okay -

I see this statement:
document.write("blah"
+ (parent.show_captions ? parent.captions[parent.x - 1] : ""));


I understand the first line. The parent document contains show_captions = true;, so I'm assuming that the second line says "if show_captions is true, then add parent.captions[parent.x -1] to the document.write statement (I'm assuming that the "?" means if the statement is true to handle the rest). But I don't get the last little bit. What's with the : "" bit?

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 06-01-2001 06:37

This is one of those spiffy shorthand syntaxes carried over from c.

(condition) ? expr1 : expr2;

is the same as writing

if(condition)
expr1
else
expr2

So in that example the test is whether parent.show_captions is true.
If it is then it resolves to parent.captions[parent.x - 1]
else "" which is just a null string.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-01-2001 06:40

Yeah, I didn't know what to search on, cuz I didn't know what it was called. The more I looked around, the more I thought it might be an "undefined" false event.

Thanks, Bugimus!

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 06-01-2001 06:44

It's called a ternary operator, AFAIK. Because it takes three operands.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-01-2001 18:42

It's more specifically called the "conditional" operator.

« BackwardsOnwards »

Show Forum Drop Down Menu