JavaScript operators
essential math + logic + beyond (the "conjunctions")
Operators look at data and try to do something with them:
Due to the essential nature of operators, many of these operators will look similar to those used in other programming language: so if you learn it one language, you will learn it in most of 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!)
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 !==
😩😩
Linguistics has a term for "same symbols used in two different contexts but each context has a different meaning": false friends
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
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)
Examples
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
The programming language "C++" plays on the ++
operator to mean "the next version of C"
Examples
Last updated