See "About This JavaScript Site" in JavaScript Index and Introduction.
Characters ? : are used in conjunction within expressions: they essentially mean "if that then" and "otherwise". They are lower in priority than other operators apart from assignment and comma.
A = B + ( C ? D : E ) // if C means true, then D, else B, is evaluated
Use of the basic arithmetical and logical operators in JavaScript appears to be reasonably well understood, once the significance of numbers being represented as IEEE Doubles is realised. Priorities are important.
However, the integer operators seem to be little known; likewise the utility of some combinations of the common operators. For integer operations, the inputs are first converted to integer by rounding towards zero, which can be seen by testing the ~~ combination.
Results with negative numbers may be unexpected, and useful or otherwise.
WARNING :- All of these must be carefully verified before use.
Here, "Integer" means a value equivalent to a 32-bit signed integer, with the operation being carried out on the bit pattern. A is associativity, P is precedence.
| Some Operators | |||||
|---|---|---|---|---|---|
| Token | A | P | Operation | Result Type | Explanation |
| + | R | 14 | Unary Plus | Number | Conversion |
| - | R | 14 | Unary Minus | Number | Negation |
| ~ | R | 14 | Complement | Integer | Invert each bit |
| ! | R | 14 | Not | Boolean | Truth Inversion |
| << | L | 11 | Shift Left | Integer | × 2^n |
| >> | L | 11 | Shift Right Signed | Integer | ÷; 2^n, truncated |
| >>> | L | 11 | Shift Right Unsigned | Integer | ÷; 2^n, truncated |
| & | L | 8 | And | Integer | Bits true in both |
| ^ | L | 7 | Xor | Integer | Bits true in one |
| | | L | 6 | Or | Integer | Bits true in one/both |
Note that the difference between "-" (minus) and "~" (tilde) is font-dependent.
In the first column, X Y Z are Numbers, I J K are Numbers of integer value, A B C are Booleans, F G are Functions, S T are Strings, P Q might be anything, lower-case are whatever makes sense.
| Some Expressions | ||
|---|---|---|
| Expression | Result Type | Explanation |
| + S | Number | Convert string to Number |
| - S | Number | Convert string to Number |
| S | 0 | Integer | Truncate string to Integer |
| + B | Integer | Convert boolean to 0/1 |
| ! ! Q | Boolean | Convert to genuine Boolean |
| X | 0 | Integer | Truncate towards zero |
| X >> 0 | Integer | Truncate towards zero |
| X << 0 | Integer | Truncate towards zero |
| ~ ~ X | Integer | Truncate towards zero (slower?) |
| ~ ~ X | Integer | Signed mod 232 |
| S >> K | Integer | Convert, divide by 2K |
| J & K | Integer | Mask out bits |
| J & 1 | Integer | Test Odd/Even |
| J | K | Integer | Mask in bits |
| J ^ K | Integer | Toggle bits |
| (x>0) | - (x<0) | Integer | Sgn(x) = +1 0 -1 |
| etc. | ? | ? |
Operator spacing is added for legibility.
| Some Assignments | ||
|---|---|---|
| Statement | Result Type | Explanation |
| P = P || Q | any | Q is default for P undef or 0 |
| d = (a/b) | 0 | Integer | Div |
| d = (a - (m=a%b)) / b | Number | Div and Mod |
| m = a - (d=(a/b)|0)*b | Number | Mod and Div |
| K ^= 1 | Integer | Toggle LSB 0/1 |
| X = !!document.getElementById | Boolean | Existence of d.gEBI |
| etc. | ? | ? |
| Operations on Booleans | ||
|---|---|---|
| Statement | Result Type | Explanation |
| ! B | Boolean | NOT |
| A == B | Boolean | XNOR |
| A != B | Boolean | XOR |
| A > B | Boolean | Implies |
| etc. | ? | ? |
not (A or B) = (not A) and (not B) not (A and B) = (not A) or (not B)
| Some Equivalences | |
|---|---|
| Expression | Expression |
| A == true | A |
| A == false | ! A |
| ! ( A && B ) | ! A || ! B |
| ! ( A || B ) | ! A && ! B |
| ~ ( A & B ) | ~ A | ~ B |
| ~ ( A | B ) | ~ A & ~ B |
| ~ ( J ^ K ) | (J & K) | (~J & ~K) |
| [ P , Q ] [ + B ] | B ? Q : P |
| ? | ? |
Some of the above combinations, while not necessarily a priori obvious, are readily understandable in hindsight. Others are hard to read.
When more legible alternatives are available, combinations which are hard to read should be used only in cases where speed truly matters.
Substitutes for Div and Mod may give unexpected results if either input is negative.