JavaScript Operators

Like other computer language, JavaScript lets you perform operations on pieces of data. A lot of the operators are ones you already know!

Operator Category Operator Description
Arithmetic Operators

+
(Addition) Adds 2 numbers.

++
(Increment) Adds one to a variable representing a number (returning either the new or old value of the variable)

-
(Unary negation, subtraction) As a unary operator, negates the value of its argument. As a binary operator, subtracts 2 numbers.

--
(Decrement) Subtracts one from a variable representing a number (returning either the new or old value of the variable)

*
(Multiplication) Multiplies 2 numbers.

/
(Division) Divides 2 numbers.

%
(Modulus) Computes the integer remainder of dividing 2 numbers.

Comparison Operators

==

Returns true if the operands are equal.

!=

Returns true if the operands are not equal.

>

Returns true if left operand is greater than right operand.

>=

Returns true if left operand is greater than or equal to right operand.

<

Returns true if left operand is less than right operand.

<=

Returns true if left operand is less than or equal to right operand.

Logical Operators

&&

(Logical AND) Returns true if both logical operands are true. Otherwise, returns false.

||

(Logical OR) Returns true if either logical expression is true. If both are false, returns false.

!

(Logical negation) If its single operand is true, returns false; otherwise, returns true.

Bitwise Operators

&
(Bitwise AND) Returns a one in each bit position if bits of both operands are ones.

^
(Bitwise XOR) Returns a one in a bit position if bits of one but not both operands are one.

|
(Bitwise OR) Returns a one in a bit if bits of either operand is one.

~
(Bitwise NOT) Flips the bits of its operand.

<<
(Left shift) Shifts its first operand in binary representation the number of bits to the left specified in the second operand, shifting in zeros from the right.

>>
(Sign-propagating right shift) Shifts the first operand in binary representation the number of bits to the right specified in the second operand, discarding bits shifted off.

>>>
(Zero-fill right shift) Shifts the first operand in binary representation the number of bits to the right specified in the second operand, discarding bits shifted off, and shifting in zeros from the left.

Assignment Operators

=
Assigns the value of the second operand to the first operand.

+=
Adds 2 numbers and assigns the result to the first.

-=
Subtracts 2 numbers and assigns the result to the first.

*=
Multiplies 2 numbers and assigns the result to the first.

/=
Divides 2 numbers and assigns the result to the first.

%=
Computes the modulus of 2 numbers and assigns the result to the first.

&=
Performs a bitwise AND and assigns the result to the first operand.

^=
Performs a bitwise XOR and assigns the result to the first operand.

|=
Performs a bitwise OR and assigns the result to the first operand.

<<=
Performs a left shift and assigns the result to the first operand.

>>=
Performs a sign-propagating right shift and assigns the result to the first operand.

>>>=
Performs a zero-fill right shift and assigns the result to the first operand.

String Operators

+

(String addition) Concatenates 2 strings.

+=

Concatenates 2 strings and assigns the result to the first operand.

Special Operators

?:
Lets you perform a simple "if...then...else"

,
Evaluates two expressions and returns the result of the second expression.

delete
Lets you delete an object property or an element at a specified index in an array.

new
Lets you create an instance of a user-defined object type or of one of the built-in object types.

this
Keyword that you can use to refer to the current object.

typeof
Returns a string indicating the type of the unevaluated operand.

void
The void operator specifies an expression to be evaluated without returning a value.

Precedence

who goes first!
Operator type Individual operators
assignment

 = += -= *= /= %= <<= >>= >>>= &= ^= |=
conditional

?:
logical-or

 ||
logical-and

 &&
bitwise-or

 |
bitwise-xor

 ^
bitwise-and

 &
equality

 == !=
relational

 < <= > >=
bitwise shift

 << >> >>>
addition/subtraction

 + -
multiply/divide

 * / %
negation/increment

! ~ - ++ -- typeof void
call, member

() [] .