logo To Foot
© J R Stockton, ≥ 2007-06-13

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

Conditional Operators

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

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

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
TokenAPOperationResult TypeExplanation
+R14Unary PlusNumberConversion
-R14Unary MinusNumberNegation
~R14ComplementIntegerInvert each bit
!R14NotBooleanTruth Inversion
<<L11Shift LeftInteger× 2^n
>>L11Shift Right SignedInteger÷; 2^n, truncated
>>>L11Shift Right UnsignedInteger÷; 2^n, truncated
&L8AndIntegerBits true in both
^L7XorIntegerBits true in one
|L6OrIntegerBits 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, 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
ExpressionResult TypeExplanation
+ SNumberConvert string to Number
- SNumberConvert string to Number
S | 0IntegerTruncate string to Integer
+ BIntegerConvert boolean to 0/1
! ! QBooleanConvert to genuine Boolean
X | 0IntegerTruncate towards zero
X >> 0IntegerTruncate towards zero
X << 0IntegerTruncate towards zero
~ ~ XIntegerTruncate towards zero (slower?)
~ ~ XIntegerSigned mod 232
S >> KIntegerConvert, divide by 2K
J & KIntegerMask out bits
J & 1IntegerTest Odd/Even
J | KIntegerMask in bits
J ^ KIntegerToggle bits
(x>0) | - (x<0)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
K ^= 1IntegerToggle LSB 0/1
X = !!document.getElementByIdBooleanExistence of d.gEBI
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
??

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 6 and W3's Tidy.
This site, http://www.merlyn.demon.co.uk/, is maintained by me.
Head.