PART IV
Appendix A: JavaScript Quick Reference 571
Logical Operators
Logical operators operate upon Boolean values and are used to construct conditional
statements. Logical operators are short-circuited in JavaScript, meaning that once a logical
condition is guaranteed, none of the other subexpressions in a conditional expression are
evaluated. They are evaluated left to right. Table A-15 summarizes these operators.
Conditional Operator
The conditional operator is a ternary operator popular among C programmers. Its syntax is
( expr1? expr2 : expr3 )
where expr1 is an expression evaluating to a Boolean and expr2 and expr3 are expressions. If
expr1 evaluates true then the expression takes on the value expr2; otherwise, it takes on the
value expr3. The operator has gained some popularity in JavaScript, serving as a compact
simple conditional often used in feature detection.
var allObject = (document.all)? true : false;
Type Operators
Type operators generally operate on objects or object properties. The most commonly used
operators are new and typeof, but JavaScript supports a range of other type operators as
well, summarized in Table A-16.
Operator Example
>>>= var x = 4;
x >>>= 2;
//1
&= var x = 4;
x &= 5;
//4
|= var x = 4;
x |= 2;
//6
^= var x = 5;
x ^= 3;
//6
TABLE A-14 Binary and Self-Assignment Bitwise Operators (continued)
Operator Description Example
&& Logical AND true && false // false
|| Logical OR true || false // true
! Logical negation! true // false
TABLE A-15 Logical Operators