Lesson 27 min read
Operators & Expressions
The math and logic superpowers that make your code actually do things
Arithmetic Operators — Math Class, But Fun
JavaScript can do all the math you'd expect, plus a few bonus tricks. Think of operators as action words — they tell JavaScript to do something with values.
+— addition (also concatenates strings!)-— subtraction*— multiplication/— division%— modulo (the remainder after division — super useful for checking even/odd)**— exponentiation (power of).2 ** 3means 2×2×2 = 8
The + operator is sneaky — when you use it with a string, it glues things together instead of adding them. This catches a lot of beginners off guard!
Arithmetic in Action
=== vs == — The Battle of Equality
This is one of the most important things to understand in JavaScript. There are two ways to check equality, and they behave very differently:
===(strict equality) — checks if the value AND the type are the same. No funny business. This is what you should use 99% of the time.==(loose equality) — tries to convert values to the same type before comparing. This leads to some truly weird results.
Same goes for !== (strict not-equal) vs != (loose not-equal). Always prefer the strict versions.
Strict vs Loose Equality — The Weirdness
Logical Operators & Modern Shortcuts
Logical operators let you combine conditions — they're the glue of if statements.
&&(AND) — both sides must be true||(OR) — at least one side must be true!(NOT) — flips true to false and vice versa
JavaScript also has two modern operators that experienced devs love:
??(nullish coalescing) — returns the right side ONLY if the left side isnullorundefined. Unlike||, it doesn't treat0,"", orfalseas "missing."?.(optional chaining) — safely access deeply nested properties without crashing if something in the chain isnullorundefined.
Logical Ops, Ternary, ??, and ?.
Note: Here's a mental shortcut: === asks "Are you the EXACT same thing?" while == asks "Could you maybe, possibly, if I squint hard enough, be the same thing?" Just always use === and you'll avoid an entire category of bugs.