Changes

Jump to: navigation, search

JavaScript Operators

1,412 bytes added, 13:36, 30 May 2007
Logical Operators
!(10 > 1) // retruns ''false'' because we have inverted the result with the logical NOT
 
</pre>
 
== Operator Precedence ==
 
When using expressions that containing a mixture of operators it is important to be aware that the expression is evaluated in a particular order depending on operator priority. This priority order is outlined in the following table which lists operators from highest priority to lowest:
 
<table border="1">
<tr>
<th>Operator<th>Description<th>Priority</th>
<tr>
<td>* / % <td>Multiplication, Division, Modulus<td>Highest</td>
<tr>
<td>+ - <td>Addition, Subtraction<td></td>
<tr>
<td><< >> >>><td>Bitwise Shift<td></td>
<tr>
<td>< <= > >=<td>Comparison<td></td>
<tr>
<td>== !=<td>Equality<td></td>
<tr>
<td>&&<td>Logical AND<td></td>
<tr>
<td>||<td>Logical OR<td></td>
<tr>
<td>?:<td>Conditional<td></td>
<tr>
<td>= += -= *= /= %=<td>Assignment<td></td>
<tr>
<td>,<td>Comma<td>Lowest</td>
<table>
 
Given the above table then clearly the following expression will multiply x by before adding the result to z because the multiplication operate has a higher precedence than the addition operator:
 
<pre>
 
k = z + x * y;
 
</pre>
 
The use of parentheses can be used to increase the precendence of part of an expression. For example if we put parentheses around the addition in the expression from above that part of the expression will be evaluated first. In the following example:
 
<pre>
 
k = (z + x) * y;
 
</pre>
 
z will be added to x and the result will then be multiplied by y.
== Operator Precedence ==

Navigation menu