The JavaScript Diaries: Part 3 - Page 4 | WebReference

The JavaScript Diaries: Part 3 - Page 4


[previous] [next]

The JavaScript Diaries: Part 3

Logical (Boolean) Operators

Operator Name Description
! NOT Returns false if its single value can be converted to true; otherwise returns true
|| OR Returns true if either value is true
&& AND Returns true if both values are true

Logical operators (also know as Boolean operators) are used for comparing two conditional statements. They are useful for checking a condition and then directing the script to proceed based on the outcome of that decision.

NOT Operator (!)

This operator is used to reverse the logical value of an expression. For instance, !true would evaluate as false. The expression says, "NOT true," so it is false. Then also, !false would evaluate as true. !(25>13) would evaluate as false. The expression says, "the statement 25 is greater than 13 is false."

OR Operator (||)

This operator returns true if the expression on either side of the operator is true. For instance, 23>43 || 25==25 is true as the expression on the right side is true. Again, 23>43 || 23==43 is false because both expressions are false.

AND Operator (&&)

This operator returns true if the expressions on both sides of the operator are true. For instance, 5==5 && 16==16 is true as both expressions are true. 27>=32 && 33==33 is false because the first expression is false.

Bitwise Operators

Operator Name
& Bitwise And
^ Bitwise XOR
| Bitwise Or
~ Bitwise Not
<< Left Shift
>> Right Shift
>>> Zero Fill Right Shift

For the most part, these are used in advanced scripts in context with CGI scripts and Java applets. We won't go into detail about these here.

Special Operators

Operator Name Description
?: Conditional A type of if...else statement. A condition is placed before the (?) and a value is placed on each side of the (:)
, Comma Used to process a series of expressions
delete Delete Deletes an object, property, or element from an array
in In Used to inspect the contents of a specified object
instanceof Instanceof Tests an object to see if it is of a specified object.
new New Creates an instance of an object
this This Refers to the current object
typeof Typeof Identifies the type of value being evaluated
void Void Evaluates an expression without returning a value

These operators are used for specific tasks and can help eliminate unnecessary code. These will be discussed in greater detail in future lessons.

Order of Precedence

Mathematics and computer operations are very orderly. Everything has a certain function and an order in which it is performed. So it is in JavaScript, where each operation is performed in a prescribed manner. Below is a chart showing from the highest level of precedence (that performed first), to the lowest (that performed last), all the operators we have looked at in this series (plus a few others). Those operators grouped together have the same level of precedence. You don't need to memorize them but do remember that each operation does has a precedence. If you're unsure about the order of precedence, you can use parentheses to define the order yourself.

Operator Comments
( ) Overrides all others. Calculations done from the inside to the outside.
[ ]  
function()  
   
! Not (Boolean)
~ Not (Bitwise)
- Negation
++ Increment
-- Decrement
new  
typeof  
void  
delete  
   
* Multiplication
Division
% Modulus
   
+ Addition
- Subtraction
   
<< Left Shift (Bitwise)
> Greater Than
>> Right Shift (Bitwise)
   
< Less Than
<= Less Than or Equal To
>= Greater Than or Equal To
   
== Equals
!= Not Equal
   
& And (Bitwise)
   
^ XOR (Bitwise)
   
| Or (Bitwise)
   
&& And (Boolean)
   
|| Or (Boolean)
   
?  
   
= Assign
+= Add and Assign
-= Subtract and Assign
*= Multiply and Assign
⁄= Divide and Assign
%= Modulus and Assign
<<= Left Shift by Value (Bitwise)
>>= Right Shift by Value (Bitwise)
&= And by Value (Bitwise)
^= XOR by Value (Bitwise)
|= Or by Value (Bitwise)
   
, Comma

[previous] [next]

Created: May 13, 2005

URL: