Tip:
Highlight text to annotate it
X
JavaScript basically has five kinds of operators. They are almost the same in every major programming
language so we'll only look through them briefly.
First, we have the four basic arithmetic operations together with modulus to gets the division
remainder.
The equal sign itself belongs to the assignment operators and simply assigns a value to a
variable. We also have the combined assignment operators
that can perform arithmetic operations on the same variable that they assign to.
The assignment operators also have the increment (++) and decrement (--) operators, that can
increase or decrease a variable by one. The post or pre versions of the operators
have the same effect on the variable, but the post-operators returns the original value
before it changes the variable, while the pre-operators changes the variable first and
then returns.
The third group is the comparison operators, which compares two values and returns either
true or false. They are mostly used for creating conditions
in flow control statements, which we will look at later.
Notice the identical (==) and not identical (!==) operators for comparing both type and
value. These are necessary because with the normal equal to operator (==) will automatically
perform a type conversion before it compares.
The fourth group is the logical, or boolean, operators that are usually used together with
the comparison operators. Logical and (&&) returns true if the expressions
on both sides are true. Logical or (||) returns true if the left or
right sides are true. And logical not (!) simply inverts a boolean
value. Note, that logical and/or will not evaluate
the right side if the result is already decided by the left side.
The fifth and last group are the bitwise operators which work on the binary representation of
numbers. Notice that we have two right shift operators.
The first one (>>) leaves the sign bit and the second one (>>>) shifts it to the right
like any other bit. They both evaluate the same for positive numbers.
The bitwise operators also have their own combined assignment operators, but all in
all the bitwise operators are not that commonly used.
Out of all the operators we've looked at most of them work only for numbers and not for
strings. But an important exception is the addition operator (+) which can perform string
concatenations. It also works with its combined assignment
operator (+=), to append a string to the end of another string variable.
Another usage for the addition operator is to convert strings into numbers, by putting
the addition sign just before the string.
Finally, let's look at operator precedence. In JavaScript, expressions are normally evaluated
from left to right. However, different operators have different
precedents. For example, multiplication binds harder than
addition and is therefore evaluated first. To avoid ambiguous expressions like this we
can use parenthesis to decide the order of evaluation, because parenthesis has the highest
precedence of all operators.