Data

Operations

JavaScript

 

Arithmetic Operators

ECMAScript 5
// assume a=5, b=2 for each line.

val = a + b;    // val -> 7
val = a - b;    // val -> 3
val = a * b;    // val -> 10
val = a / b;    // val -> 2.5
val = a % b;    // val -> 1
val = a++;      // val -> 5 then a -> 6
val = ++a;      // a -> 6 then val -> 6
val = b--;      // val -> 2 then b -> 1
val = --b;      // b -> 1 then val -> 1

+     sums left and right
-     subtracts right from left
*     multiplies left and right
/     divides left and right
%     reminder of the left divided by right
x++   returns x and then increments x by 1
++x   increments x by 1 and then returns x
x--   returns x and then decrements x by 1
--x   decrements x by 1 and then returns x

 

Comparison Operators

ECMAScript 5
// assume a=5, b=2 for each line.

val = (a == b);     // val -> false
val = (a === b);    // val -> false
val = (a != b);     // val -> true
val = (a !== b);    // val -> true
val = (a > b);      // val -> true
val = (a >= b);     // val -> true
val = (a < b);      // val -> false
val = (a <= b);     // val -> false

==    is left and right equal
===   is left and right have same type and equal
!=    is left and right different
!==   is left and right have different type or value
>     is left bigger than right
>=    is left bigger or equal than right
<     is left smaller than right
<=    is left smaller or equal than right

JavaScript does implicit conversions between value types when necessary. By using === and !== you can make sure that on comparing values, the types of the values are considered too.

"2" == 2   // true  
"2" === 2  // false  
 

Logical Operators

ECMAScript 5
// assume yes=true, no=false for each line.

val = (no && no);       // val -> false
val = (no && yes);      // val -> false
val = (yes && no);      // val -> false
val = (yes && yes);     // val -> true

val = (no || no);       // val -> false
val = (no || yes);      // val -> true
val = (yes || no);      // val -> true
val = (yes || yes);     // val -> true

val = (!yes);           // val -> false
val = (!no);            // val -> true

&&    logical and of left and right
||    logical or of left and right
!     logical not of its right operand

 

Bitwise Operators

ECMAScript 5
var a=5;       // 0000 0101
var b=12;      // 0000 1100

val = (a & b);  // val -> 4  (0000 0100)
val = (a | b);  // val -> 13 (0000 1101)
val = (a ^ b);  // val -> 9  (0000 1001)
val = (~b);     // val ->-13 (1111 0011)
val = (b<<2);   // val -> 48 (0011 0000)
val = (b>>2);   // val -> 3  (0000 0011)

&    bitwise and of left and right
|    bitwise or of left and right
^    bitwise xor or left and right
~    bitwise one complement of right operand
<<   bitwise left shift of left in amount of right
>>   bitwise right shift of left in amount of right

 

Assignment Operators

ECMAScript 5
// assume a=5, b=2 for each line.

val = (a = b);      // val -> a -> 2
val = (a += b);     // val -> a -> 7
val = (a -= b);     // val -> a -> 3
val = (a *= b);     // val -> a -> 10
val = (a /= b);     // val -> a -> 2.5
val = (a %= b);     // val -> a -> 1
val = (a <<= b);    // val -> a -> 20
val = (a >>= b);    // val -> a -> 1
val = (a &= b);     // val -> a -> 0
val = (a |= b);     // val -> a -> 7
val = (a ^= b);     // val -> a -> 7

=    assigns left to right
+=   assigns left to sum of left and right
-=   assigns left to subtract of right from left
*=   assigns left to multiplication of left and right
/=   assigns left to division of left to right
%=   assigns left to reminder of left divided to right
<<=  assigns left to rightShift of left to value right
>>=  assigns left to leftShift of left to value right
&=   assigns left to bitwise and left and right
|=   assigns left to bitwise or left and right
^=   assigns left to bitwise xor left and right