Let's start with an example
var a = 1 + 1;
Here is an example of the addition operator. It looks familiar to normal arithmatic because JavaScipt uses infix notation, such that the operator '+' is placed inbetween its parameters. If you are familiar with JavaScripts function notation, it may help to think of operators as a special set of functions, written syntactically different than then normal function notation.
function +(a, b){
return c;
}
// where c is the sum of a and b
Operators have a precedence, this is the order in which they are executed. We learn of this principle in early arithmatic classes, if you are familiar with BIDMAS then you are also familar with basic operator precedence.
var b = 1 + 2 * 3;
The multiplication operator '*' has a higher precedence than the addtion '+' operator. In example 3. JavaScript will multiply 2 and 3, then add 1. Hence the varible b evaluates to 7.
Let's list JavaScript's arithmetical operators and place them in order of their precedence...
Syntax | Type |
---|---|
a++ |
Postfix Incrument |
a-- |
Postfix Decriment |
++a |
Prefix Increment |
--a |
Prefix Decrement |
a ** b |
Exponentiation |
a * b |
Multiplication |
a / b |
Division |
a % b |
Remainder |
a + b |
Addition |
a - b |
Subtraction |
Incruments and decrements operators simply increase or decrease the value of a by 1, the difference between the postfix and prefix is when this operator is applied.
// Postfix
var a = 1;
b = a++;
// In this case b = 1 and a = 2
// Prefix
var a = 1;
b = ++a;
// In this case b = 2 and a = 2
This demonstrates that postfixs are applied after a variable is evaluated, prefixes are applied before a varible is evaluated.
We have already used the assigment operator within this gist, in its most basic form we use the = symbol for the assigment operator. This operator assignes a value to a varible, in turn creating a value pair that maps a name to a unique value and holds it in memory.
Let's list JavaScript's assigment operators and place them in order of their precidence...
Syntax | Type |
---|---|
a = b |
Assignment |
a += b |
Addition Assigment |
a -= b |
Subtraction Assigment |
a **= b |
Exponential Assigment |
a *= b |
Multiplcation Assigment |
a /= b |
Division Assigment |
a %= b |
Remainder Assigment |
a <<= b |
Bitwise Left Shift Assigment |
a >>= b |
Bitwise Right shift Assigment |
a >>>= b |
Bitwise Unsigned Right shift Assigment |
a &= b |
Bitwise AND Assigment |
a ^= b |
Bitwise XOR Assigment |
a |= b |
Bitwise OR Assigment |
You may notice that all of JavaScript's arithmatic and bitwise operators can be used as a prefixes for the assigment opperator. All of these prefixes cause the assigment operator to be evaluated in the same way. This can demonstrated as such...
var a += b;
// Equivelant to a = a + b
Syntax | Type |
---|---|
(a) |
Grouping |
a.b |
Member Access |
a[b] |
Computed Member Access |
new a(b) |
new, with arguments |
a(b) |
Function Call |
new a |
new, without arguments |
a++ |
Postfix Incrument |
a-- |
Postfix Decriment |
!a |
Logical Not |
~a |
Bitwise Not |
+a |
Unary Plus |
-a |
Unary Negation |
++a |
Prefix Increment |
--a |
Prefix Decrement |
typeof a |
typeof |
void a |
void |
delete a |
delete |
a ** b |
Exponentiation |
a * b |
Multiplication |
a / b |
Division |
a % b |
Remainder |
a + b |
Addition |
a - b |
Subtraction |
a << b |
Bitwise Left Shift |
a >> b |
Bitwise Right Shift |
a >>> b |
Bitwise Unsigned Right Shift |
a < b |
Less Than |
a <= b |
Less Than Or Equal To |
a > b |
Greater Than |
a >= b |
Greater Than Or Equal To |
a in b |
in |
a instanceof b |
instanceof |
a == b |
Equality |
a != b |
Inequality |
a ==== b |
Strict Equality |
a !== b |
Strict Inequality |
a & b |
Bitwise AND |
a ^ b |
Bitwise XOR |
a | b |
Bitwise OR |
a && b |
Logical AND |
a || b |
Logical OR |
a ? b : c |
Conditional |
a = b |
Assignment |
a += b |
Addition Assigment |
a -= b |
Subtraction Assigment |
a **= b |
Exponential Assigment |
a *= b |
Multiplcation Assigment |
a /= b |
Division Assigment |
a %= b |
Remainder Assigment |
a <<= b |
Bitwise Left Shift Assigment |
a >>= b |
Bitwise Right shift Assigment |
a >>>= b |
Bitwise Unsigned Right shift Assigment |
a &= b |
Bitwise AND Assigment |
a ^= b |
Bitwise XOR Assigment |
a |= b |
Bitwise OR Assigment |
yield b |
yield |
yield* b |
yeild* |
... a |
Spread |
a , b |
Sequence |