JavaScript - Operators Overview
Overview
Estimated time: 20–30 minutes
Operators act on values and variables. This page orients you to the categories; subsequent pages cover each in depth.
Learning Objectives
- Recognize operator categories and their use cases.
- Know where to find details for each category.
Prerequisites
Categories
- Arithmetic:
+ - * / % ** ++ -- - Comparison:
== != === !== < > <= >= - Logical:
&& || !(and logical assignment&&= ||= ??=) - Bitwise:
& | ^ ~ << >> >>> - Assignment:
= += -= *= /= %= **=etc. - Conditional (ternary):
cond ? a : b - typeof and delete
- Spread/Rest:
... - Nullish coalescing:
?? - Precedence and associativity
Example
const a = 2 + 3 * 4; // 14 (multiplication before addition)
const b = (2 + 3) * 4; // 20
const user = { name: 'Ada', role: null };
const role = user.role ?? 'guest'; // 'guest'
Common Pitfalls
- Using
==instead of===can coerce values unexpectedly. - Confusing short-circuit
||with nullish coalescing??(which treats0and''as valid values). - Ignoring precedence can change results; use parentheses for clarity.
Checks for Understanding
- What’s the difference between
==and===? - When should you prefer
??over||?
Show answers
===compares without coercion;==allows type coercion and can yield surprising results.- Prefer
??when you want to treat onlynullandundefinedas “missing,” keeping 0/''/false as valid.
Exercises
- Show a case where
value || 'fallback'differs fromvalue ?? 'fallback'. - Rewrite an expression to make precedence explicit using parentheses.