logo To Foot
© J R Stockton, ≥ 2009-06-01

JavaScript Uses of Operators.

No-Frame * Framed Index * Frame This
Links within this site :-

See "About This JavaScript Site" in JavaScript Index and Introduction.

I need to consider the strict equality operators

Integer

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).

Conditional Operators

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

Uses of Obscure Combinations of Operators

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.

Obscure Operators

A is associativity, P is precedence.

Some Operators
TokenAPOperationResult TypeExplanation
+R14Unary PlusNumberConversion
-R14Unary MinusNumberNegation
~R14ComplementSigned IntegerInvert each bit
!R14NotBooleanTruth Inversion
<<L11Shift LeftSigned Integer× 2^n
>>L11Shift Right SignedSigned Integer÷; 2^n, truncated
>>>L11Shift Right UnsignedUnsigned Integer÷; 2^n, truncated
&L8AndSigned IntegerBits true in both
^L7XorSigned IntegerBits true in one
|L6OrSigned IntegerBits true in one/both

Note that the difference between "-" (minus) and "~" (tilde) is font-dependent.

Obscure Usages and Combinations

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
ExpressionResult TypeExplanation
+ SNumberConvert string to Number
- SNumberConvert string to Number
S | 0IntegerTruncate string to Integer
+ BNumberConvert boolean to 0/1
! ! QBooleanConvert to genuine Boolean
X | 0Signed IntegerTruncate towards zero
X >>> 0Unsigned IntegerTruncate towards zero
X >> 0Signed IntegerTruncate towards zero
X << 0Signed IntegerTruncate towards zero
~ JSigned IntegerFlip about -½
~ ~ XSigned IntegerTruncate towards zero
~ ~ XSigned IntegerSigned mod 232
S >> KSigned IntegerConvert, divide by 2K
J & KSigned IntegerMask out bits
J & 1Signed IntegerTest Odd/Even
J | KSigned IntegerMask in bits
J ^ KSigned IntegerToggle bits
(x>0) | - (x<0)Signed IntegerSgn(x) = +1 0 -1
etc.??

Operator spacing is added for legibility.

Some Assignments
StatementResult TypeExplanation
P = P || QanyQ is default for P undef or 0
d = (a/b) | 0IntegerDiv
d = (a - (m=a%b)) / bNumberDiv and Mod
m = a - (d=(a/b)|0)*bNumberMod and Div
X = !!document.getElementByIdBooleanExistence of d.gEBI
etc.??

 

Some Self-Assignments
StatementVariable TypeExplanation
K ^= 1IntegerToggle LSB 0/1
K ^= 1BooleanToggle false/true
etc.??

 

Operations on Booleans
StatementResult TypeExplanation
! BBooleanNOT
A == BBooleanXNOR
A != BBooleanXOR
A > BBooleanImplies
etc.??

Equivalences

not (A  or B)    =    (not A) and (not B)
not (A and B)    =    (not A)  or (not B)
Some Equivalences
ExpressionExpression
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 ) ...
??

Expression Tester

 
 
 
For larger expressions, use JavaScript/HTML/VBS Quick Trials.

Warnings

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.

Home Page
Mail: no HTML
© Dr J R Stockton, near London, UK.
All Rights Reserved.
These pages are tested mainly with MS IE 7 and Firefox 3.0 and W3's Tidy.
This site, http://www.merlyn.demon.co.uk/, is maintained by me.
Head.