The ternary operator is a substitute for an if statement where both the if and else clauses assign different values to the same field - like this:
result = 'something';
result = 'somethingelse';
The ternary operator shortens this if/else statement into a single statement.
result = (condition) ? 'something' : 'somethingelse';
While this particular substitution can only be done where the original if statement follows the format shown above this is in fact one of the more common situations where an if statement is needed and the ternary operator is the more efficient way of coding this construct since when coded using the ternary operator it is obvious that one of two values is to be loaded into the result field where the content of an if statement can be anything at all.
Note that you can nest ternary operators since the second and third positions can have anything that returns a value substituted for them.
Note that to test this example try adding a ? followed by a colour to the end of the address of the page - example13.html?red
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
var colour, favcol;
colour = window.location.search.substring(1);
favcol = ('green' === colour) ? 'is green.' : 'is not green.';
document.getElementById('ex').innerHTML = colour + favcol;