# Operators
# Basic Operators
Sample code:
var a = 10, b = 20;
// Addition
console.log(30 === a + b);
// Subtraction
console.log(-10 === a - b);
// Multiplication
console.log(200 === a * b);
// Division
console.log(0.5 === a / b);
// Complementation
console.log(10 === a % b);
- The addition operation (
+
) can also be used to concatenate strings.
var a = '.w' , b = 'xs';
// String concatenation
console.log('.wxs' === a + b);
# Unary Operators
Sample code:
var a = 10, b = 20;
// Auto increment
console.log(10 === a++);
console.log(12 === ++a);
// Auto decrement
console.log(12 === a--);
console.log(10 === --a);
// Positive value
console.log(10 === +a);
// Negative value
console.log(0-10 === -a);
// Not
console.log(-11 === ~a);
// Negate
console.log(false === !a);
// Delete
console.log(true === delete a.fake);
// Void
console.log(undefined === void a);
// Typeof
console.log("number" === typeof a);
# Bit Operators
Sample code:
var a = 10, b = 20;
// Left shift
console.log(80 === (a << 3));
// Unsigned right shift
console.log(2 === (a >> 2));
// Signed right shift
console.log(2 === (a >>> 2));
// AND
console.log(2 === (a & 3));
// XOR
console.log(9 === (a ^ 3));
// OR
console.log(11 === (a | 3));
# Relational Operators
Sample code:
var a = 10, b = 20;
// Less than
console.log(true === (a < b));
// Greater than
console.log(false === (a > b));
// Less than or equal to
console.log(true === (a <= b));
// Greater than or equal to
console.log(false === (a >= b));
# Equivalence Operators
Sample code:
var a = 10, b = 20;
// Equal sign
console.log(false === (a == b));
// Not equal sign
console.log(true === (a != b));
// Equivalent sign
console.log(false === (a === b));
// Not equivalent sign
console.log(true === (a !== b));
# Assignment Operators
Sample code:
var a = 10;
a = 10; a *= 10;
console.log(100 === a);
a = 10; a /= 5;
console.log(2 === a);
a = 10; a %= 7;
console.log(3 === a);
a = 10; a += 5;
console.log(15 === a);
a = 10; a -= 11;
console.log(-1 === a);
a = 10; a <<= 10;
console.log(10240 === a);
a = 10; a >>= 2;
console.log(2 === a);
a = 10; a >>>= 2;
console.log(2 === a);
a = 10; a &= 3;
console.log(2 === a);
a = 10; a ^= 3;
console.log(9 === a);
a = 10; a |= 3;
console.log(11 === a);
# Binary Logical Operators
Sample code:
var a = 10, b = 20;
// Logical AND
console.log(20 === (a && b));
// Logical OR
console.log(10 === (a || b));
# Other Operators
Sample code:
var a = 10, b = 20;
//Conditional operator
console.log(20 === (a >= 10 ? a + 10 : b + 10));
//Comma operator
console.log(20 === (a, b));
# Operator Priority
Priority | Operator | Description | Associativity |
---|---|---|---|
20 | ( ... ) | Brackets | n/a |
19 | ... . ... | Member access | Left to right |
... [ ... ] | Member access | Left to right | |
... ( ... ) | Function call | Left to right | |
17 | ... ++ | Postfix increment | n/a |
... -- | Postfix decrement | n/a | |
16 | ! ... | Logical not | Right to left |
~ ... | Bitwise not | Right to left | |
+ ... | Unary addition | Right to left | |
- ... | Unary subtraction | Right to left | |
++ ... | Prefix increment | Right to left | |
-- ... | Prefix decrement | Right to left | |
typeof ... | typeof | Right to left | |
void ... | void | Right to left | |
delete ... | delete | Right to left | |
14 | ... * ... | Multiplication | Left to right |
... / ... | Division | Left to right | |
... % ... | Modulo | Left to right | |
13 | ... + ... | Addition | Left to right |
... - ... | Subtraction | left to right | |
12 | ... << ... | Bitwise left shift | Left to right |
... >> ... | Bitwise right shift | Left to right | |
... >>> ... | Unsigned right shift | Left to right | |
11 | ... < ... | Less than | Left to right |
... <= ... | Less than or equal to | Left to right | |
... > ... | Greater than | Left to right | |
... >= ... | Greater than or equal to | Left to right | |
10 | ... == ... | Equal sign | Left to right |
... != ... | Not equal sign | Left to right | |
... === ... | Equivalent sign | Left to right | |
... !== ... | Not equivalent sign | Left to right | |
9 | ... & ... | Bitwise AND | Left to right |
8 | ... ^ ... | Bitwise XOR | Left to right |
7 | ... | ... | Bitwise OR | Left to right |
6 | ... && ... | Logical AND | Left to right |
5 | ... || ... | Local OR | Left to right |
4 | ... ? ... : ... | Conditional operator | Left to right |
3 | ... = ... | Value assignment | Left to right |
... += ... | Value assignment | Left to right | |
... -= ... | Value assignment | Left to right | |
... *= ... | Value assignment | Left to right | |
... /= ... | Value assignment | Left to right | |
... %= ... | Value assignment | Left to right | |
... <<= ... | Value assignment | Left to right | |
... >>= ... | Value assignment | Left to right | |
... >>>= ... | Value assignment | Left to right | |
... &= ... | Value assignment | Left to right | |
... ^= ... | Value assignment | Left to right | |
... |= ... | Value assignment | Right to left | |
0 | ... , ... | Comma | Left to right |