Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

06 Operators


Arithmetic
+  addition for numbers
-  substraction
/  division
*  multiply
%  modulus division, returning only the remainder
++     pre - increment or post - increment
--  pre - decrements or postdecrements

Comparison
=  equal (assignment)
<  less than
>  greater than
<>  not equal
>=  greater than or equal to
<=  less than or equal to
Is two variables / objects are the same
Like variable matches a pattern

Concatenation
&  combines two strings into one
+  combines two strings into one (is addition for numbers)

Boolean :
False = any expression that evaluates to 0
True   = any expression that evaluates to a number other than 0

4 false values are :   0      ""      null      undefined
all else are true

Logical
And logical conjunction (both values True)
Or logical disjunction (either value True)
Not logical negation (reverses True / False value)
Xor logical exclusion (True if one, and only one, is True)
Imp logical implication (e9e9
Eqv logical equivalence (True if both have same logical value)

Comparison Operators ( return boolean results) :
==  Equal to
===  Equal to (without type conversion)
!=  Not equal
!==  Not equal (without type conversion)
>  greater than
>=  greater than or equal to
<  less than
<=  less than or equal to

Boolean Operators
&&   boolean "and" function
||   boolean "or" function
!   boolean negation

Bitwise Operators
&  bitwise AND
|  bitwise OR
^  bitwise XOR
~  bitwise NOT
<<  shift left
>>  shift right
>>>  shift right zero file

Advanced assignment operators
+=   x += y  x = x + y
-=   x -= y  x = x - y
*=   x *= y  x = x * y
/=   x /= y  x = x / y
%=   x %= y  x = x % y
<<=   x <<= y  x = x << y
>>=   x >>= y  x = x >> y
>>>=  x >>>= y  x = x >>> y
&=   x &= y  x = x & y   //bitwise AND
|=   x |= y  x = x | y   //bitwise OR
^=   x ^= y  x = x ^ y   //bitwise XOR

Special Operators :
?         - (condition) ? trueValue : falseValue
typeof    - typeof myvariable
new  creates new objects

Example of using ? operator :
result = (cats > dogs) ? "yes" : "no"

Example of using typeof operator
if ( typeof myvariable = "number" ) ...

Example of using new operator
var X = new Array (5)

Short Circuit Function
(a = 0) && (b = 12)   // if left is false, right side is not evaluated (short circuit function)