JavaScript operators
essential math + logic + beyond (the "conjunctions")
Operators look at data and try to do something with them:
Arithmetic operators
The mathematical operations we know from grade school, with slight variations, e.g.:
*
for multiplication, to avoid confusion with the letter x/
for division, because÷
keys don't exist much on keyboards%
for modulo (remainders), somehow not avoiding confusion with "percents"
+
add
-
subtract
*
multiply
/
divide
%
modulo (not percent!)
**
exponent ("to the power of")
Examples
A popular use of modulo determines whether a number x
is even (0) or odd (1):
Comparison operators
As we advanced from second grade, we came across these concepts:
>
greater than
>=
greater than or equal to
<
less than
<=
lesser than or equal to
==
equal to
===
equal to or equal type to
!=
not equal to
!==
not equal or not equal type to
?
ternary (see below)
A confusing phenomenon for the beginner in JavaScript, we use:
=
to mean "assignment" or "re-assignment"==
to mean "equal value"===
to mean "equal value" and "equal type"
So why ==
and ===
? Well:
==
compares a number like17
regardless if it is aNumber
or aString
===
ensures that a number like17
is aNumber
and not anything else
Also, note the amount of equal symbols in !=
and !==
:
We see comparison operators used mostly in branching (decision) statements:
Examples
Logical operators
Later on in school, we had concepts like these:
&&
"and"
||
"or"
!
"not"
Examples
Unary operators
We use unary operators if we want to:
change a
String
into aNumber (+variable)
negate a
Number (-variable)
flip the boolean (
!variable
) = swap the true to a false (and vice versa)typeof
is also considered a unary operator and it simply returns the data type
Ternary operators
We use ternary operators as a shorthand for if
and else
statements with the format:
For example:
Increment/decrement operators
++
increment the existing value (add 1)
--
decrement the existing value (subtract 1)
Note the subtle but important difference between postfix and prefix operators when dealing with the increment and decrement operators!
Postfix
An easy way to remember this is that the variable returns first, and then increments:
Prefix
An easy way to remember this is that the increment or decrement happens first, and then returns/displays the new value:
Assignment operators
These basically function as shorthand to the aforementioned arithmetic operators:
+=
add to the existing value
-=
subtract from the existing value
*=
multiply by the existing value
/=
divide by the existing value
Examples
Order of operators
JavaScript has an order of operations just like mathematics (remember the BEDMAS rule?):
brackets
exponents
if two or more exponent operators exist, then the calculation will happen right to left
division
multiplication
addition / subtraction
if both operators exist, then the calculation will happen from left to right
so unlike BEDMAS, addition does necessarily NOT come before subtraction!
If we want to ensure addition happens first, then we need to place brackets:
Last updated