Page 343 - Webapplication11_C11_Flipbook
P. 343
<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 pushed x >> y
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.
Let us create a JavaScript program to use the bitwise operators.
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> JavaScript Bitwise Operators </TITLE>
</HEAD>
<BODY>
<H1> JavaScript Bitwise Operators </H1>
<SCRIPT>
JavaScript Part 1 341

