Operators are simple when used one at a time:
let b = 2
b + 2
You can combine multiple operators in the same expression:
const three = 1 + 2
three + 2 * 3
In this case you need to apply some rules to know which operation is executed first.
Take this example:
let a = 1 * 2 + ((5 / 2) % 2)
The result is 2.5, but why?
Some operations have higher precedence than the others. The precedence rules are:
*
/
%
(multiplication/division/remainder)+
-
(addition/subtraction)=
(assignment)
We have more than those, but it’s a good start to understand precedence rules.
Operations on the same level (like +
and -
) are executed in the order they are found, from left to right.
Following these rules, the operation
let a = 1 * 2 + ((5 / 2) % 2)
can be solved in this way:
let a = 1 * 2 + ((5 / 2) % 2)
let a = 2 + ((5 / 2) % 2)
let a = 2 + (2.5 % 2)
let a = 2 + 0.5
let a = 2.5
And if you feel brave, you can omit the parentheses altogether, and the result will be the same:
let a = 1 * 2 + 5 / 2 % 2 //2.5
Lessons in this unit:
Are you intimidated by Git? Can’t figure out
merge vs rebase? Are you afraid of screwing up
something any time you have to do something in
Git? Do you rely on ChatGPT or random people’s
answer on StackOverflow to fix your problems?
Your coworkers are tired of explaining Git to
you all the time? Git is something we all need
to use, but few of us really master it. I
created this course to improve your Git (and
GitHub) knowledge at a radical level. Launching
May 21, 2024. Join the waiting list!