JavaScript Operators
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Logical Operators
- String Operators
- Member Operators
- Special Operators
Arithmetic Operators
operator | description | |||||||
---|---|---|---|---|---|---|---|---|
+ |
Unary addition operator. Converts a value to a numeric value. Same as the Number constructor.
result = +value;
Sample:
|
|||||||
+ |
Addition operator. Adds the value of two numeric expressions.
result = num1 + num2;
Sample:
|
|||||||
++ |
Increment operator. Increments the value of a numeric expression.
result = num++;
result = ++num; Sample:
|
|||||||
- |
Unary negation operator. Negates the value of a numeric expression.
result = -num;
Sample:
|
|||||||
- |
Subtraction operator. Subtracts the value of one numeric expression from another.
result = num1 - num2;
Sample:
|
|||||||
-- |
Decrement operator. Decrements the value of a numeric expression.
result = num--;
result = --num; Sample:
|
|||||||
* |
Multiplication operator. Multiplies a numeric expression by another.
result = num1 * num2;
Sample:
|
|||||||
/ |
Division operator. Divides a numeric expression by another.
result = num1 / num2;
Sample:
|
|||||||
% |
Modulus operator. Returns the integer remainder of division of one number by another.
result = num1 % num2;
Sample:
|
Assignment Operators
operator | syntax | meaning | description | |||||||
---|---|---|---|---|---|---|---|---|---|---|
= | x = y |
x = y |
Assignment operator. Assigns the value of an expression (y) to another (x). | |||||||
+= | x += y |
x = x + y |
Addition assigment operator. Performs an addition operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x). | |||||||
-= | x -= y |
x = x - y |
Subtraction assignment operator. Performs a subtraction operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x). | |||||||
*= | x *= y |
x = x * y |
Multiplication assignment operator. Performs a multiplication operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x). | |||||||
/= | x /= y |
x = x / y |
Division assignment operator. Performs a division operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x). | |||||||
%= | x %= y |
x = x % y |
Modulus assignment operator. Performs a modulus operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x). | |||||||
<<= | x <<= y |
x = x << y |
Left Shift assignment operator.
Shifts the value of the variable (x) left by the number of specified bits (y) and fills the missing bits on the right side with 0's.
The x <<= y expression is identical to the x *= Math.pow (2, y) expression. Sample:
|
|||||||
>>= | x >>= y |
x = x >> y |
Signed Right Shift assignment operator.
Shifts the value of the variable (x) right by the number of specified bits (y) and fills the missing bits on the left side with the sign bit of the original value.
The sign bit of a value is the leftmost bit.
When the sign bit is 1, then the represented value is negative, else non-negative.
The x >>= y expression is identical to the x = Math.floor (x / Math.pow (2, y)) expression. Sample:
|
|||||||
>>>= | x >>>= y |
x = x >>> y |
Unsigned Right Shift assignment operator.
Shifts the value of the variable (x) right by the number of specified bits (y) and fills the missing bits on the left side with 0's.
The sign bit of a value is the leftmost bit.
When the sign bit is 1, then the represented value is negative, else non-negative.
Sample:
|
|||||||
&= | x &= y |
x = x & y |
Bitwise AND assignment operator. Performs a bitwise AND operation on the numeric value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
|
|||||||
^= | x ^= y |
x = x ^ y |
Bitwise XOR assignment operator. Performs a bitwise XOR operation on the numeric value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
|
|||||||
|= | x |= y |
x = x | y |
Bitwise OR assignment operator. Performs a bitwise OR operation on the value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
|
Bitwise Operators
Bitwise operators work on integers in JavaScript. Floating point values will be converted to integer values (cutting off the decimal part) before the operation.
operator | syntax | description | |||||||
---|---|---|---|---|---|---|---|---|---|
<< | x << y |
Left Shift operator.
Returns the value of an integer (x) shifted left by the number of specified bits (y).
This shift operation fills the missing bits on the right side with 0's.
The x << y expression is identical to the x * Math.pow (2, y) expression. Sample:
|
|||||||
>> | x >> y |
Signed Right Shift operator.
Returns the signed value of an integer (x) shifted right by the number of specified bits (y).
This shift operation fills the missing bits on the left side with the sign bit of the value of x.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative. The x >> y expression is identical to the Math.floor (x / Math.pow (2, y)) expression. Sample:
|
|||||||
>>> | x >>> y |
Unsigned Right Shift operator.
Returns the unsigned value of an integer (x) shifted right by the number of specified bits (y).
This shift operation fills the missing bits on the left side with 0's.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative. Sample:
|
|||||||
& | x & y |
Bitwise AND operator. Performs an AND operation on every digit of two integers (x, y) in binary representation. If both integers have 1 at a position, the result will be 1 at that position. Otherwise, the result will be 0 at that position.
Sample:
|
|||||||
^ | x ^ y |
Bitwise XOR operator. Performs an XOR operation on every digit of two integers (x, y) in binary representation. If the two integers have a different value at a position, the result will be 1 at that position. Otherwise, the result will be 0 at that position.
Sample:
|
|||||||
| | x | y |
Bitwise OR operator. Performs an OR operation on every digit of two integers (x, y) in binary representation. If both integers have 0 at a position, the result will be 0 at that position. Otherwise, the result will be 1 at that position.
Sample:
|
|||||||
~ | ~ x |
Bitwise NOT (complement) operator. Performs a bitwise complement operation on every digit of the integer (x) in binary representation. If the integer has 0 at a position, the result will be 1 at that position. If the integer has 1 at a position, the result will be 0 at that position.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative. When all bits of a value are 0, the value is 0. When all bits of a value are 1, the value is -1.
Conclusion:
Sample:
|
Comparison Operators
operator | name | description |
---|---|---|
== | Equal | Returns true if the left and right expressions are equal. If the types of the two expressions are different, JavaScript attempts to convert them to the same type. |
!= | Not equal | Returns true if the two expressions are not equal. If the types of the two expressions are different, JavaScript attempts to convert them to the same type. |
=== | Strict equal | Returns true if the left and right expressions and their types are both equal. |
!== | Strict not equal | Returns true if the left and right expressions are not equal, or their types are different. |
< | Less than | Returns true if the left expression is lower than the right expression. |
<= | Less than or equal | Returns true if the left expression is lower than or equal to the right expression. |
> | Greater than | Returns true if the left expression is greater than the right expression. |
>= | Greater than or equal | Returns true if the left expression is greater than or equal to the right expression. |
Logical Operators
operator | name | syntax | description | |||||||
---|---|---|---|---|---|---|---|---|---|---|
&& | logical AND | exp1 && exp2 |
If the value of the first expression (converted to boolean if needed) is false, returns the first expression, otherwise returns the second expression.
Sample:
|
|||||||
|| | logical OR | exp1 || exp2 |
If the value of the first expression (converted to boolean if needed) is true, returns the first expression, otherwise returns the second expression.
Sample:
|
|||||||
! | logical NOT | !exp |
Returns false if the value of the first expression (converted to boolean if needed) is true, otherwise returns true.
Sample:
|
String Operators
operator | syntax | description | |||||||
---|---|---|---|---|---|---|---|---|---|
+ | str1 + str2 |
Addition operator. Returns the concatenation of two strings.
Sample:
|
|||||||
+= | str1 += str2 |
Addition assignment operator. Appends the value of one string to another. Same as str1 = str1 + str2
Sample:
|
Member Operators
operator | name | description | |||||||
---|---|---|---|---|---|---|---|---|---|
. | Dot notation |
Provides access to the properties and methods of a JavaScript object.
Sample:
|
|||||||
[] | Bracket notation |
Provides access to the properties and methods of a JavaScript object.
Sample:
|
Special Operators
operator | name | support | description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
? : | Conditional operator |
Returns exp1 if condition is true, else returns exp2. Typically used instead
of the if...else statement.
Syntax:
condition ? exp1 : exp2
Sample:
|
|||||||||||||||||||||||||||||||
delete | delete operator |
Removes an element from an Array, or removes a property from an Object.
Sample:
|
|||||||||||||||||||||||||||||||
get | get operator |
Creates a getter method for the specified property.
Syntax:
get prop [funcname]() {
[code]
};
Sample:
|
|||||||||||||||||||||||||||||||
in | in operator |
Returns a Boolean value that indicates whether the given property is exists in the given object.
Syntax:
result = property in object
Sample:
|
|||||||||||||||||||||||||||||||
instanceof | instanceof operator |
Returns a Boolean value that indicates whether an object is an instance of the specified object type, or not.
Syntax:
result = object instanceof type
Sample:
|
|||||||||||||||||||||||||||||||
new | new operator |
Creates a new instance of the specified object type. It can be used to create an instance of any predefined
JavaScript type, or a user defined type. It is always possible to add properties and methods to a newly created
object.
Syntax:
objName = new objType ([arguments])
Sample:
|
|||||||||||||||||||||||||||||||
set | set operator |
Creates a setter method for the specified property.
Syntax:
set prop [funcname]() {
[code]
};
Sample:
|
|||||||||||||||||||||||||||||||
typeof | typeof operator |
Returns the data type of an expression as a String.
Syntax:
typeof [(]expression[)] ;
Sample:
|
|||||||||||||||||||||||||||||||
void | void operator |
Evaluates the given expression, and returns undefined. The void operator is useful if you want to evaluate an expression without using the return value.
Sample:
|
User Contributed Comments