Operators

What is an operator?

A symbol used to perform a built-in function on one - it is then called unary operator- or more operands (data values).

JavaScript has the following operators:

Assignment:
An assignment operator assigns a value to its left operand based on the value of its right operand or both left and right operands.

Comparison:
A comparison operator compares its operands of various data types and returns a Boolean value (true or false.)

Arithmetic
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.

String:
String operators take string values (either literals or variables) as their operands and return a single string value.

Logical:
Logical operators take Boolean (logical) values as operands and return a Boolean value. 

Bitwise:
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. Bitwise operators work on the bits representing these numerical values . Those numerical values are all represented as 32 bit integers.

Special operators:
Each special operator is entirely different from the rest.

Operators Example


Assignment Operators

Operator Operation Description Usage
=
  Assigns the value of the second operand to the first operand.
a = b
+=
  Adds 2 numbers and assigns the result to the first.
a += b
-=
  Subtracts 2 numbers and assigns the result to the first.
a -= b
*=
  Multiplies 2 numbers and assigns the result to the first.
a *= b
/=
  Divides 2 numbers and assigns the result to the first.
a /= b
%=
  Computes the modulus of 2 numbers and assigns the result to the first.
a %= b
&=
  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.  
 

Comparison Operators

Operator Operation Description Usage
==   Returns true if the operands are equal.
a == b
!=   Returns true if the operands are not equal.
a != b
>   Returns true if left operand is greater than right operand.
a > b
>=   Returns true if left operand is greater than or equal to right operand.
a >= b
<   Returns true if left operand is less than right operand.
a < b
<=   Returns true if left operand is less than or equal to right operand.
a <= b
 

Arithmetic Operators

Operator Operation Description Usage
+
Addition Adds 2 numbers.
a + b
++
Increment Adds one to a variable representing a number (returning either the new or old value of the variable)
a++ or ++a
-
Unary negation, subtraction As a unary operator, negates the value of its argument. As a binary operator, subtracts 2 numbers.
a = -b
--
Decrement Subtracts one from a variable representing a number (returning either the new or old value of the variable)
a-- or --a
*
Multiplication Multiplies 2 numbers.
a * b
/
Division Divides 2 numbers.
a/b
%
Modulus Computes the integer remainder of dividing 2 numbers.
a % b
 

String Operators

Operator Operation Description Usage

+

String addition Concatenates 2 strings.
a + b
+= String assignment and addition Concatenates 2 strings and assigns the result to the first operand.
a += b 
 

Logical Operators

Operator Operation Description Usage
&& Logical AND Returns true if both logical operands are true. Otherwise, returns false.
expr1 && expr2
|| Logical OR Returns true if either logical expression is true. If both are false, returns false.
expr1 || expr2
! Logical negation If its single operand is true, returns false; otherwise, returns true. !expr
 

Bitwise Operators

Operator Operation Description Usage
&
Bitwise AND Returns a one in each bit position if bits of both operands are ones.
a & b
^
Bitwise XOR Returns a one in a bit position if bits of one but not both operands are one.
a ^ b
|
Bitwise OR Returns a one in a bit if bits of either operand is one.
a | b
~
Bitwise NOT Flips the bits of its operand.
~ a
<<
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.
a << b
>>
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.
a >> b
>>>
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. a >>> b
 

Special Operators

Operator Operation Description Usage
?:
  Lets you perform a simple "if...then...else"
condition ? expr1 : expr2
 
,
  Evaluates two expressions and returns the result of the second expression.
expr1, expr2
delete
  Lets you delete an object property or an element at a specified index in an array.
delete object.property

delete object[index]
new
  Lets you create an instance of a user-defined object type or of one of the built-in object types.
objectName = new objectType

(param1 [,param2] ...[,paramN])
this
  Keyword that you can use to refer to the current object.
this[.propertyName]
typeof
  Returns a string indicating the type of the unevaluated operand.
typeof operand
typeof (operand)
void
  The void operator specifies an expression to be evaluated without returning a value.
javascript:void (expression)
javascript:void expression