File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Conditional (Ternary) Operator (?:) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Conditional (Ternary) Operator (?:)" Watch "Conditional (Ternary) Operator (?:)" New topic
Author

Conditional (Ternary) Operator (?:)

Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Hi,
Can anybody explain me the foll lines :
display = display?display !row.style.display?"none" row.style.display == "inline"?"none":"inline"));
row.style.display = display;
img.src = display == "inline"?"'images/minus.gif'":"'images/plus.gif'";

Thanks,
Shriya

[edit - disbaled smiles]
[ July 15, 2008: Message edited by: Eric Pascarello ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15362
    
    6
break it up into if else

<blockquote>code:
<pre name="code" class="core">

if(display){}
else if( !row.style.display ){ display = "none" }
else if( row.style.display == "inline"){ display = "none" }
else display = "inline";

</pre>
</blockquote>

Does that make sense?

All it is doing is checking to see if a row is visible or not and flips it to the opposite value.

Eric
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Thanks Eric.
It was very helpful.

Y cant they just write simple if statements
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15362
    
    6
I prefer

<blockquote>code:
<pre name="code" class="core">
var foo = variable1 || variable2;
</pre>
</blockquote>



Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56529
    
  14

Originally posted by Shriya Kishore:
Y cant they just write simple if statements

Please use real words when posting to the forums. Abbreviations such as "y" in place of "why" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56529
    
  14

Originally posted by Shriya Kishore:
Y cant they just write simple if statements
The ternary operator can be very useful and terse when making a simple decision in an expression. It can also be confusing when used as shown in your example. A judicious mix of if statements and the ternary operator can be used to achieve code that is as brief as possible, while still being readable and clear.

A good dose of thoughtful code formatting can also go a long way to enhancing the clarity of the code.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Conditional (Ternary) Operator (?:)
 
Similar Threads
Hiding Relatively Positioned Layer
How to hide a cell w/ background image until a function call
How to create displaytag table border
Make Active n Inactive button using JavaScript
Help in javascript