Page 197 - Web_Application_v2.0_C12_Fb
P. 197
document.write( " NOT operator result is"+"="+result);
</SCRIPT>
</BODY>
</HTML>
Output:
Conditional (Ternary) Operator (? :)
The conditional operator, also known as the ternary operator. A ternary operator (? :) determines whether a
condition is true or false and then executes a block of code based on a specified statement. The syntax of the
ternary operator is as follows:
Condition ? expression1 : expression2
The ternary operator evaluates the test condition.
If the condition is true, expression1 is executed.
If the condition is false, expression2 is executed.
The ternary operator takes three operands, hence, the name ternary operator. It is also known as a conditional
operator.
Example 8: To demonstrate the use of ternary operator
<HTML>
<HEAD>
<TITLE>JavaScript Conditional Operator </TITLE>
</HEAD>
<BODY>
<script>
var age = 23;
var result = age >= 21 ? "Can vote" : "Not allowed to vote";
document.write(result);
</SCRIPT>
</BODY>
</HTML>
Output:
JavaScript Part 2 195

