Introduction to operators
Operators are the symbols that tell JavaScript to do something with values: add them, compare them, combine conditions or assign a result to a variable. In total = price * qty, the * multiplies and the = stores the answer. The values operators work on are called operands. Nearly every line of a program uses at least one operator, so knowing the main groups pays off quickly.
The main operator families
| Group | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % ** | 10 % 3 gives 1 |
| Assignment | = += -= *= /= ??= | score += 5 |
| Comparison | === !== < > <= >= | age >= 18 |
| Logical | && || ! | isAdmin || isOwner |
| Increment / decrement | ++ -- | count++ |
| Nullish and optional | ?? ?. | user?.name ?? 'Guest' |
| Ternary | condition ? a : b | n > 0 ? 'positive' : 'not positive' |
| Type | typeof instanceof | typeof 42 gives "number" |
Operators are also grouped by how many operands they take: unary ones such as ! and typeof take one, binary ones such as + take two, and the ternary operator is the only one that takes three.
Trying them out
const price = 250;
let qty = 3;
const total = price * qty; // 750
const evenQty = qty % 2 === 0; // false
qty += 2; // qty is now 5
const squared = 4 ** 2; // 16
console.log(5 === '5'); // false: different types
console.log(5 == '5'); // true: loose equality converts types
const name = '';
console.log(name || 'Anonymous'); // 'Anonymous' (empty string is falsy)
console.log(name ?? 'Anonymous'); // '' (only null/undefined fall back)
const user = null;
console.log(user?.email); // undefined, no error
let a = 1;
console.log(a++); // 1 (returns old value, then adds)
console.log(++a); // 3 (adds first, then returns)
Operators that behave differently with strings
The + operator does two jobs. With two numbers it adds; if either side is a string, it joins them as text. So 2 + 3 is 5, but '2' + 3 is '23'. The other arithmetic operators convert strings to numbers, so '6' * 2 is 12. This mismatch is the source of many bugs, especially with values read from form inputs, which are always strings. Convert them with Number(input.value) before adding.
Short-circuit logic
&& and || do not always return true or false. They return one of their operands and stop evaluating as soon as the answer is known. a || b returns a if it is truthy, otherwise b. a && b returns a if it is falsy, otherwise b. That is why they are often used for default values and guarded calls, such as callback && callback().
Pitfalls to avoid early
- Using
=in a condition.if (role = 'admin')assigns instead of comparing, and the condition is always true. Use===. - Using
==. Loose equality applies confusing conversions, such as0 == ''being true. Prefer===and!==. - Choosing
||for defaults when 0 or an empty string are valid.quantity || 1turns a real0into1;quantity ?? 1keeps it. - Decimal surprises.
0.1 + 0.2is not exactly0.3because of how numbers are stored; round results for display.
Where to go from here
Learn the families, use strict equality, convert input strings before maths, and pick ?? over || when falsy values are meaningful. When several operators appear in one expression, the order they run in follows precedence rules, which is exactly what the next lesson explains.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.