JS Operators

Arithmetic
var x = 3 Initialization It assigns the value specified on ots left to the variable on the right at the point of declaration. = Assignment Is a binary operator that assigns the value on the right to the variable its the left. + Addition It is a binary operator that returns the sum after adding the left operand the right. - Subtraction It is a binary operator that returns the value after subtracting the value of the right operand from the left. / Floating point division * Multiplication It is a binary operator that returns the product after multiplying two operands. % Modulus operator It is a binary operator that returns the remainder after dividing the the left operand with the right.

Increment (++)

The increment operator is used as follows: var++ or ++var

This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

For example, if x is 3, then the statement

y = x++

increments x to 4 and sets y to 3.

If x is 3, then the statement

y = ++x

increments x to 4 and sets y to 4.

Decrement (--)

The decrement operator is used as follows:

var-- or --var

This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example x--) then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing.

For example, if x is 3, then the statement

y = x--

decrements x to 2 and sets y to 3.

If x is 3, then the statement

y = --x

decrements x to 2 and sets y to 2. Unary negation (-)

The unary negation operator must precede its operand. It negates its operand. For example,

x = -x

negates the value of x; that is if x were 3, it would become -3.

Bitwise Operators
Bitwise operators treat their operands as a set of bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number 9 has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values. Bitwise Logical Operators

The bitwise logical operators work conceptually as follows:

The operands are converted to 32-bit integers, and expressed a series of bits (zeros and ones). Each bit in the first operand is paired with the corresponding bit in the second operand: first bit to first bit, second bit to second bit, and so on. The operator is applied to each pair of bits, and the result is constructed bitwise.

The bitwise operators are:

Bitwise AND & returns a one if both operands are ones. Bitwise OR | returns a one if either operand is one. Bitwise XOR ^ returns a one if one but not both operands are one.

For example, the binary representation of 9 is 1001, and the binary representation of 15 is 1111. So, when the bitwise operators are applied to these values, the results are as follows:

15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110)

Bitwise Shift Operators

The bitwise shift operators are:

Left Shift (<<) Sign-propagating Right Shift (>>) Zero-fill Right shift (>>>)

The shift operators take two operands: the first is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used.

Shift operators convert their operands to 32-bit integers, and return a result of the same type as the left operator. Left Shift (<<)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

For example, 9<<2 yields 36, because 1001 shifted two bits to the left becomes 100100, which is 36. Sign-propagating Right Shift (>>)

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.

For example, 9>>2 yields 2, because 1001 shifted two bits to the right becomes 10, which is 2. Likewise, -9>>2 yields -3, because the sign is preserved. Zero-fill right shift (>>>)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.

For example, 19>>>2 yields 4, because 10011 shifted two bits to the right becomes 100, which is 4. For postive numbers, zero-fill right shift and sign-propagating right shift yield the same result.

Relational
== Equal to === Exactly > Greater than Tests if value1 is greater than value2 and returns true other wise it returns false. < Less than Tests if value1 is less than value2 and returns true other wise it returns false. != Tests if value1 is not equal to value2 and returns true other wise it returns false.

Logical
&& - AND - It returns true if and only if both values in the condition evaluates to true otherwise it returns false. - It returns true if one or both of the values are true other otherwise it returns false ! Not It is a unary operator that negates the falsity of a condition.
 * - OR

String Operators
In addition to the comparison operators, which may be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example,

"my " + "string"

returns the string

"my string"

The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring is a string that has the value "alpha", then the expression

mystring += "bet"

evaluates to "alphabet" and assigns this value to mystring. Operator Precedence

The precedence of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.

The precedence of operators, from lowest to highest is as follows:

comma , assignment = += -= *= /= %= <<= >>= >>>= &= ^= |= conditional ?: logical-or || logical-and && bitwise-or | bitwise-xor ^ bitwise-and & equality == != relational < <= > >= bitwise shift << >> >>> addition/subtraction + - multiply/divide * / % negation/increment ! ~ - ++ -- call, member [].