Operators
Operators are the symbols and keywords that do work on values in JavaScript: add numbers, compare them, join strings, assign results to variables and combine conditions. Every expression you write, from total = price * qty to if (age >= 18 && hasId), is built from operators and the values they act on, called operands.
The main families
| Family | Examples | Used for |
|---|---|---|
| Arithmetic | + - * / % **, ++ -- | Maths, remainders, powers, counters |
| Assignment | = += -= *= ??= ||= | Storing and updating values |
| Comparison | === !== < > <= >= | Producing true or false |
| Logical | && || ! | Combining conditions and defaults |
| Nullish and optional | ??, ?. | Handling null and undefined safely |
| String | +, += | Joining text |
| Other | typeof, instanceof, in, ? : | Type checks and choices |
Operators can also be grouped by how many operands they take: unary (!ok, -x), binary (a + b) and the single ternary operator (cond ? a : b).
Seeing them work together
const price = 250;
let qty = 3;
const subtotal = price * qty; // 750
const isBulk = qty >= 3; // true
const discount = isBulk ? 0.1 : 0; // 0.1
let total = subtotal - subtotal * discount; // 675
total += 40; // shipping: 715
qty++; // 4
const remainder = 17 % 5; // 2
const cube = 2 ** 3; // 8
const coupon = null;
const code = coupon ?? 'NONE'; // "NONE"
const canCheckout = total > 0 && !!code; // true
Precedence decides what runs first
Like maths, JavaScript evaluates some operators before others. Multiplication and division come before addition and subtraction, comparisons come before &&, and && comes before ||. Assignment comes almost last. So 2 + 3 * 4 is 14, not 20. When in doubt, add parentheses; they make your intent obvious to the next reader too. Mixing ?? directly with && or || without parentheses is a syntax error, precisely to avoid confusion.
The + operator and type coercion
The plus sign does two jobs. With two numbers it adds; if either side is a string, it joins them as text. This is where many beginner bugs start, because values from forms, prompts and URLs are strings.
'5' + 3; // "53" (string join)
'5' - 3; // 2 (minus converts to number)
Number('5') + 3; // 8
'5' == 5; // true (loose equality converts types)
'5' === 5; // false (strict equality does not)
0.1 + 0.2 === 0.3; // false, floating-point rounding
Convert input with Number() before doing maths, and use === and !== instead of == and != so you are never surprised by automatic conversion. For money, work in whole units such as paise to avoid floating-point rounding.
Prefix versus postfix increment
x++ returns the old value and then adds one; ++x adds one and returns the new value. On a line by themselves they behave the same. Inside a bigger expression, the difference matters, which is why many style guides prefer the clearer x += 1.
Short-circuiting is worth knowing too. a && b stops at the first falsy value and a || b stops at the first truthy one, so the right side may never run. That makes user && user.save() a guard, but it also means a function call placed on the right side can be skipped silently. The logical assignment operators build on this: count ||= 1 sets a value only when the current one is falsy, and count ??= 1 only when it is null or undefined.
What to keep in mind
- Know the families: arithmetic, assignment, comparison, logical and nullish.
- Use parentheses when precedence is not obvious.
- Convert strings to numbers before adding.
- Prefer
===and use??for defaults that should keep0and empty strings.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.