Page 300 - Web Applications (803) Class 11
P. 300
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 : experience2
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.
Let us create a JavaScript program to use the ternary operator.
<HTML>
<HEAD>
<TITLE>JavaScript Ternary 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 of the above code is as follows:
Bitwise Operators
Bitwise operators deal with bits and perform operations on them one by one. The following table shows the bitwise
operators provided by JavaScript:
Operator Name Description Example
& Bitwise AND If both bits are 1, sets each bit to 1. x & y
| Bitwise OR If one of two bits is 1, sets each bit to 1. x | y
^ Bitwise XOR If just one of two bits is 1, sets each bit to 1. x ^ y
~ Bitwise NOT All bits are inverted x ~ y
<< Left shift Shifts left by bringing zeros in from the right and dropping the leftmost bits. x << y
>> Right shift Allows the rightmost bits to fall off and copies of the leftmost bit to be x >> y
pushed in from the left.
>>> Zero fill right shift By pushing zeros in from the left, it shifts right and lets the rightmost pieces x >>> y
fall off.
298 Touchpad Web Applications-XI

