Operators
You have already used some operators without much ceremony — + for addition, === for equality. This lesson covers the full set you need: arithmetic, comparison, and logical. Together, these power every calculation and condition in your code.
Arithmetic operators
Section titled “Arithmetic operators”You saw these in the Numbers lesson. Here is the complete set:
const tourPrice = 149;
console.log(tourPrice + 50); // 199 — additionconsole.log(tourPrice - 50); // 99 — subtractionconsole.log(tourPrice * 2); // 298 — multiplicationconsole.log(tourPrice / 2); // 74.5 — divisionconsole.log(tourPrice % 10); // 9 — remainderconsole.log(2 ** 8); // 256 — exponentiationIncrement and decrement
Section titled “Increment and decrement”++ adds 1 to a variable; -- subtracts 1:
let count = 0;count++;console.log(count); // 1count++;console.log(count); // 2count--;console.log(count); // 1Prefix vs postfix: ++count increments before returning the value; count++ returns the value before incrementing. In most practical cases, you are using ++ as a statement by itself (not inside an expression), so the difference does not matter. The postfix form (count++) is more common.
Assignment operators
Section titled “Assignment operators”Assignment operators combine an operation with assignment:
let price = 149;
price += 20; // price = price + 20 → 169price -= 20; // price = price - 20 → 149price *= 2; // price = price * 2 → 298price /= 2; // price = price / 2 → 149price %= 100; // price = price % 100 → 49These are shorthand — they make the code more concise without changing meaning.
Comparison operators
Section titled “Comparison operators”Comparison operators evaluate to true or false:
const tourPrice = 149;
console.log(tourPrice > 100); // trueconsole.log(tourPrice < 100); // falseconsole.log(tourPrice >= 149); // trueconsole.log(tourPrice <= 148); // falseconsole.log(tourPrice === 149); // true — strict equalityconsole.log(tourPrice !== 200); // true — strict inequality== vs ===
Section titled “== vs ===”== does type coercion before comparing. === requires both value and type to match.
console.log(149 == '149'); // true — coerces string to numberconsole.log(149 === '149'); // false — number vs string, different types
console.log(0 == false); // true — coercesconsole.log(0 === false); // false — number vs booleanAlways use ===. Type coercion produces results that look wrong and are hard to trace. The only deliberate exception is x == null, which catches both null and undefined in one check.
Logical operators
Section titled “Logical operators”Logical operators combine boolean expressions:
const tourAvailable = true;const tourPrice = 149;
// AND — true only if both sides are trueconsole.log(tourAvailable && tourPrice < 200); // trueconsole.log(tourAvailable && tourPrice > 200); // false
// OR — true if either side is trueconsole.log(tourAvailable || tourPrice > 200); // trueconsole.log(!tourAvailable || tourPrice > 200); // false
// NOT — inverts a booleanconsole.log(!tourAvailable); // falseconsole.log(!false); // trueShort-circuit evaluation
Section titled “Short-circuit evaluation”&& stops as soon as it finds a false value — no need to evaluate the rest.
|| stops as soon as it finds a true value.
// If tourAvailable is false, the right side never runstourAvailable && console.log('Booking is open');
// If tourName has a value, use it; otherwise fall backconst displayName = tourName || 'Tour name unavailable';The || fallback pattern (value || default) is extremely common in JavaScript for providing default values.
Operator precedence
Section titled “Operator precedence”JavaScript evaluates operators in this order (higher = first):
!(NOT)**(exponentiation)*,/,%+,->,>=,<,<====,!==,==,!=&&||
When in doubt, use parentheses. (a > b) && (c < d) is clearer than a > b && c < d, even though both are parsed the same way.
Exercise
Section titled “Exercise”Using the STO tour variables, evaluate these expressions and predict the result before running each one:
const tourName = 'Cascade Ridge Hike';const tourPrice = 149;const tourAvailable = true;
// Arithmeticconsole.log(tourPrice * 0.9); // 10% discountconsole.log(tourPrice + 20); // with equipment rental fee
// Comparisonconsole.log(tourPrice > 100);console.log(tourPrice === 149);console.log(tourPrice === '149'); // type coercion trap — try == too
// Logicalconsole.log(tourAvailable && tourPrice < 200);console.log(!tourAvailable || tourPrice > 100);
// Fallback patternconst tourGuide = null;const guideName = tourGuide || 'Guide TBD';console.log(guideName);- Arithmetic operators:
+,-,*,/,%,**. Use parentheses to control order of operations. ++and--increment and decrement. Postfix (x++) is most common when used as a statement.- Assignment shorthands:
+=,-=,*=,/=,%=. - Always use
===for equality —==does type coercion that produces surprising results. &&requires both sides truthy;||requires at least one;!inverts.- Short-circuit:
&&stops at the first falsy;||stops at the first truthy. Use||for fallback defaults.