The Conditional Operator and if Statement : Conditional Operator « Operators « JavaScript Tutorial





An expression that evaluates to a Boolean is always placed to the left of the question mark (?) in the Conditional Operator.

If the expression evaluates to true, the value between the question mark and the colon (:) is returned.

If the expression evaluates to false, the value following the colon is returned.

<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
    mailFlag = "YES"
    var message1;
    var message2;

    if (mailFlag == "YES"){
        message1 = "A";
    }else{
        message1 = "B";
    }
    //Same statement using conditional operator
    message2 = (mailFlag == "YES") ? "A" : "B";

    document.write("The if statement returns: ",message1,"<BR>");
    document.write("The conditional operator returns: ",message2);
-->
</SCRIPT>
</html>










2.8.Conditional Operator
2.8.1.The Conditional Operator and if Statement
2.8.2.Tenary operator
2.8.3.Conditional Operator: ?
2.8.4.Finding the absolute value