Operators
Operators are the symbols that do work on values: adding numbers, comparing scores, combining conditions. Most look like ordinary maths, but JavaScript has a few traps, especially the difference between == and === and the way + treats strings.
Arithmetic operators
console.log(7 + 2); // addition console.log(7 - 2); // subtraction console.log(7 * 2); // multiplication console.log(7 / 2); // division (never rounds) console.log(7 % 2); // remainder console.log(2 ** 3); // exponent: 2 to the power 3 let count = 5; count++; // increase by 1 console.log(count);
The remainder operator % is more useful than it looks: n % 2 === 0 tells you a number is even, and minutes % 60 gives the minutes left over after whole hours. ++ and -- add or subtract 1. Normal maths precedence applies (** first, then *, /, %, then + and -), and brackets override it: 2 + 3 * 4 is 14 but (2 + 3) * 4 is 20.
Assignment operators
= stores a value. The combined forms update a variable using its current value:
| Operator | Example | Same as |
|---|---|---|
+= | total += 50 | total = total + 50 |
-= | stock -= 1 | stock = stock - 1 |
*= | price *= 2 | price = price * 2 |
??= | name ??= "Guest" | assign only if name is null or undefined |
+ with strings, and type coercion
If either side of + is a string, JavaScript converts the other side to a string and joins them. The other arithmetic operators only work on numbers, so they convert strings to numbers instead. This automatic conversion is called type coercion.
console.log("5" + 3);
console.log("5" - 3);
console.log("5" * "2");
console.log(true + 1);
console.log("KES " + 100 + 50);
console.log("KES " + (100 + 50));
The first result is the string "53"; true becomes 1 in arithmetic. The last two lines show why brackets matter: + works left to right, so "KES " + 100 becomes a string first and then 50 is glued on.
Comparison: === versus ==
Comparison operators return true or false: <, >, <=, >= and the equality operators. JavaScript has two kinds of equality:
- Strict equality
===(and!==): true only if both values have the same type and the same value. No conversion happens. - Loose equality
==(and!=): if the types differ, JavaScript first coerces them to a common type, then compares.
console.log(5 == "5");
console.log(5 === "5");
console.log(0 == false);
console.log(0 === false);
console.log("" == 0);
console.log(null == undefined);
console.log(null === undefined);
console.log(NaN === NaN);
=== stops as soon as the types differ. == converts first, which is why surprising pairs such as "" == 0 come out true.The coercion rules of == are long and hard to remember, which leads to bugs. Always use === and !==. If you need to compare a form value with a number, convert it yourself first: Number(input) === 5. The only common exception some developers allow is value == null, which is true for both null and undefined.
NaN is not equal to anything, not even itself, so x === NaN is always false. Use Number.isNaN(x). Also beware comparing strings that hold numbers: "10" < "9" is true, because strings are compared character by character and "1" comes before "9".
Logical operators
&& (AND) is true when both sides are true, || (OR) when at least one is, and ! (NOT) flips a value.
const age = 20; const hasId = true; console.log(age >= 18 && hasId); console.log(age < 13 || age > 65); console.log(!hasId);
In conditions JavaScript treats every value as truthy or falsy. The falsy values are false, 0, -0, 0n, "" (empty string), null, undefined and NaN. Everything else is truthy, including "0", "false", [] and {}.
&& and || actually return one of their operands, not necessarily true or false, and they short-circuit: they stop as soon as the answer is known. a || b returns a if it is truthy, otherwise b. The nullish coalescing operator ?? is similar but only falls back when the left side is null or undefined:
const nickname = ""; console.log(nickname || "Guest"); const stock = 0; console.log(stock || 10); console.log(stock ?? 10); const city = null; console.log(city ?? "Unknown");
Notice the difference for stock: || treats 0 as "missing" and replaces it, while ?? keeps 0 because it is a real value. Use ?? for defaults whenever 0 or an empty string are valid.
Optional chaining ?.
Reading a property of null or undefined crashes your code. Optional chaining ?. stops and returns undefined instead, and combines nicely with ??:
const user = { name: "Juma", address: null };
console.log(user.address?.town);
console.log(user.address?.town ?? "No town given");
console.log(user.address.town);
Key points
- Arithmetic:
+ - * / % **;/never rounds and%gives the remainder. +joins strings if either side is a string; other operators convert strings to numbers.- Always use
===and!==;==converts types first and surprises you. &&and||short-circuit and return an operand;??falls back only fornull/undefined.?.safely reads properties that might not exist.
Tip: use the ← and → keys to move between pages.
