-2
function togglePageElementVisibility(what)  
{  
    var obj = typeof what == 'object'  
    ? what : document.getElementById(what);  

    if (obj.style.display == 'none')  
        obj.style.display = 'block';  
    else  
        obj.style.display = 'none';  
    return false;  
} 

I got this code from a website to hide and unhide part of a webpage. I've been trying to wrap my head around it but im not sure how to change the first part to an if and else statement instead of what it is. Can you help please?

  • 1
    https://msdn.microsoft.com/en-us/library/be21c7hw(VS.94).aspx – suvroc Dec 15 '15 at 14:35
  • It is an 'if' (ternary conditional operator): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – jolmos Dec 15 '15 at 14:36
  • 3
    Possible duplicate of [How to use the ?: (ternary) operator in JavaScript](http://stackoverflow.com/questions/6259982/how-to-use-the-ternary-operator-in-javascript) – Reinard Dec 15 '15 at 14:36

1 Answers1

3

In your example Ternary Operator is used.

condition ? if-true : if-false

var obj;
if (typeof what === 'object') {
  obj = what;
} else {
  obj = document.getElementById(what);
}

is equal to

var obj = (typeof what === 'object') ? what : document.getElementById(what);
J. Kovacevic
  • 183
  • 16