The Sysadmin Notebook  

Sitemap

Javascript Operators

Core Javacript Operators

Contents

Javascript will generally apply implicit conversions on operands as required by operator used. The assignment operator (=) returns the value of its right-side operand, and is right to left associative: so multiple variables can be assigned the same value in one line:

a = b = c = 3;

In common with other languages, shortcut assignment-plus-operation operators also exist: +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=

Arithmetic

Top Bottom
Arithmetic Operators
Operator Operation Notes
+ addition or concatenation Adds numbers, concatenates strings. If one operator is a string, then the other is converted to a string and concatenation is applied. Operator has left-to-right associativity
- subtraction
* multiplication
/ division
% modulus division Returns the remainder of dividing the left operand by the right operand
++ increment
-- decrement

Comparison

Top Bottom
Comparison Operators
Operator Operation Notes
== is equal to Equality tests will generally allow type conversion to test for equality. "null" and "undefined" are "equal"
=== is identical to Both operands must have type. For primitive datatypes, values must match. For reference types, they must have the same reference. "null" is not identical to "undefined"
!= not equal to
!== not identical to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
in is a property of evaluates to true if left string operand is a property of right object operand
instanceof is an instance of evaluates to true if left object operand is an instance of the right-side class operand

Logical

Top Bottom
Logical and Bitwise Operators
Operator Operation Notes
&& Logical AND
|| OR
! Not
Sets bit to 1 if bit is set to 1 in both left and right operand & Bitwise AND
Sets bit to 1 if bit is set to 1 in either left and right operand | Bitwise OR
Sets bit to 1 if bit is set to 1 in either left and right operand but not both | Bitwise XOR
Reverses all bits in its argument ~ Bitwise NOT
Shifts bits in left operand to the left by the number specified in right operand. Equivalent to multiplication by 2 to the power of right operand << Shift left
Shifts bits in left operand to the right by the number specified in right operand. Equivalent to integer division by 2 to the power or right operand. Preserves sign of original left operand >> Shift right
Shifts bits in left operand to the right by the number specified in right operand. Fills left bits with ones. Does not preserve sign of original left operand >>> Shift right

Others

Top Bottom
Miscellaneous Operators
Operator Operation Notes
?: Conditional The ternary operator
typeof datatype of operand Return value can be one of "number", "string", "object", "boolean", "function", "undefined". Returns "object" if operand == null.
new object constructor and initialiser Returns an object of type specified with optional parenthesised argument list as initialisers
delete delete object property, array element or variable Will not delete explicitly declared variables, nor some built-in and client-side properties
void void discard operand value and return "undefined". Typically used in client-side scripts, to evaluate an expression for its side-effects, but avoid displaying the result on the client browser
, comma Evaluates arguments left-to-right, returns value of right argument
[] array element access
. object element access
() function call
in object property test if (x in o) then o.x = 1;

A pop-up window using 'void':Open New Window"