See "About This JavaScript Site" in JavaScript Index and Introduction.
Here, "Integer" means a 32-bit value, with the operation being carried out on the bit pattern. The result will be stored in a signed Number.
In most cases, the most significant bit of the 32 is taken to represent -231 for conversion to Number, and the range of the results is -2147483648 to 2147483647 (0x8000000 to 0x7FFFFFFF). For the operator >>>, the most significant bit is taken as +231, and the range of the results is 0 to 4294967296 (0x00000000 to 0xFFFFFFFF).
Characters ? and : 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.
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 | Signed Integer | Invert each bit |
! | R | 14 | Not | Boolean | Truth Inversion |
<< | L | 11 | Shift Left | Signed Integer | × 2^n |
>> | L | 11 | Shift Right Signed | Signed Integer | ÷; 2^n, truncated |
>>> | L | 11 | Shift Right Unsigned | Unsigned Integer | ÷; 2^n, truncated |
& | L | 8 | And | Signed Integer | Bits true in both |
^ | L | 7 | Xor | Signed Integer | Bits true in one |
| | L | 6 | Or | Signed 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, J K are Numbers of integer value, A B C are Booleans, F G are Functions, S is a numeric String, 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 | Number | Convert boolean to 0/1 |
! ! Q | Boolean | Convert to genuine Boolean |
X | 0 | Signed Integer | Truncate towards zero |
X >>> 0 | Unsigned Integer | Truncate towards zero |
X >> 0 | Signed Integer | Truncate towards zero |
X << 0 | Signed Integer | Truncate towards zero |
~ J | Signed Integer | Flip about -½ |
~ ~ X | Signed Integer | Truncate towards zero |
~ ~ X | Signed Integer | Signed mod 232 |
S >> K | Signed Integer | Convert, divide by 2K |
J & K | Signed Integer | Mask out bits |
J & 1 | Signed Integer | Test Odd/Even |
J | K | Signed Integer | Mask in bits |
J ^ K | Signed Integer | Toggle bits |
(x>0) | - (x<0) | Signed 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 |
X = !!document.getElementById | Boolean | Existence of d.gEBI |
etc. | ? | ? |
Some Self-Assignments | ||
---|---|---|
Statement | Variable Type | Explanation |
K ^= 1 | Integer | Toggle LSB 0/1 |
K ^= 1 | Boolean | Toggle false/true |
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 |
if ( ~ J ) ... | if ( J != -1 ) ... |
? | ? |
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.