| 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
|
|
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
|
|
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
|
|
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
|
|
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.
|
 |
 |
|
|
subject: Conditional (Ternary) Operator (?:)
|
|
|