Operators (JavaScript)

JavaScript has a full range of operators, including arithmetic, logical, bitwise, assignment, as well as some miscellaneous operators. For explanations and examples, see the topics on specific operators.

Computational Operators

Description Symbol
Unary negation -
Increment ++
Decrement --
Multiplication *
Division /
Modulus arithmetic %
Addition +
Subtraction -

Logical Operators

Description Symbol
Logical NOT !
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Equality ==
Inequality !=
Logical AND &&
Logical OR ||
Conditional (ternary) ?:
Comma ,
Strict Equality ===
Strict Inequality !==

Bitwise Operators

Description Symbol
Bitwise NOT ~
Bitwise Left Shift <<
Bitwise Right Shift >>
Unsigned Right Shift >>>
Bitwise AND &
Bitwise XOR ^
Bitwise OR |

Assignment Operators

Description Symbol
Assignment =
Compound Assignment OP= (such as += and &=)

Miscellaneous Operators

Description Symbol
delete delete
typeof typeof
void void
instanceof instanceof
new new
in in

Equality and Strict Equality

The difference between == (equality) and === (strict equality) is that the equality operator will coerce values of different types before checking for equality. For example, comparing the string "1" with the number 1 will compare as true. The strict equality operator, on the other hand, will not coerce values to different types, and so the string "1" will not compare as equal to the number 1.

Primitive strings, numbers, and Booleans are compared by value. If they have the same value, they are equal. Objects (including Array, Function, String, Number, Boolean, Error, Date and RegExp objects) compare by reference. Even if two variables of these types have the same value, they are equal only if they refer to exactly the same object.

For example:

// Two strings with the same value.  
var string1 = "Hello";  
var string2 = "Hello";  

// Two String objects with the same value.  
var StringObject1 = new String(string1);  
var StringObject2 = new String(string2);  

if (string1 == string2)  
    document.write("string1 is equal to string2 <br/>");  

if (StringObject1 != StringObject2)  
    document.write("StringObject1 is not equal to StringObject2 <br/>");  

// To compare the values of String objects, use the toString() or valueOf() methods.  
if (StringObject1.valueOf() == StringObject2.valueOf())  
    document.write("The value of StringObject1 is equal to the value of StringObject2");  

// string1 is equal to string2   
// StringObject1 is not equal to StringObject2   
// The value of StringObject1 is equal to the value of StringObject2