Skip to content

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.

You saw these in the Numbers lesson. Here is the complete set:

const tourPrice = 149;
console.log(tourPrice + 50); // 199 — addition
console.log(tourPrice - 50); // 99 — subtraction
console.log(tourPrice * 2); // 298 — multiplication
console.log(tourPrice / 2); // 74.5 — division
console.log(tourPrice % 10); // 9 — remainder
console.log(2 ** 8); // 256 — exponentiation

++ adds 1 to a variable; -- subtracts 1:

let count = 0;
count++;
console.log(count); // 1
count++;
console.log(count); // 2
count--;
console.log(count); // 1

Prefix 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 combine an operation with assignment:

let price = 149;
price += 20; // price = price + 20 → 169
price -= 20; // price = price - 20 → 149
price *= 2; // price = price * 2 → 298
price /= 2; // price = price / 2 → 149
price %= 100; // price = price % 100 → 49

These are shorthand — they make the code more concise without changing meaning.

Comparison operators evaluate to true or false:

const tourPrice = 149;
console.log(tourPrice > 100); // true
console.log(tourPrice < 100); // false
console.log(tourPrice >= 149); // true
console.log(tourPrice <= 148); // false
console.log(tourPrice === 149); // true — strict equality
console.log(tourPrice !== 200); // true — strict inequality

== does type coercion before comparing. === requires both value and type to match.

console.log(149 == '149'); // true — coerces string to number
console.log(149 === '149'); // false — number vs string, different types
console.log(0 == false); // true — coerces
console.log(0 === false); // false — number vs boolean

Always 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 combine boolean expressions:

const tourAvailable = true;
const tourPrice = 149;
// AND — true only if both sides are true
console.log(tourAvailable && tourPrice < 200); // true
console.log(tourAvailable && tourPrice > 200); // false
// OR — true if either side is true
console.log(tourAvailable || tourPrice > 200); // true
console.log(!tourAvailable || tourPrice > 200); // false
// NOT — inverts a boolean
console.log(!tourAvailable); // false
console.log(!false); // true

&& 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 runs
tourAvailable && console.log('Booking is open');
// If tourName has a value, use it; otherwise fall back
const displayName = tourName || 'Tour name unavailable';

The || fallback pattern (value || default) is extremely common in JavaScript for providing default values.

JavaScript evaluates operators in this order (higher = first):

  1. ! (NOT)
  2. ** (exponentiation)
  3. *, /, %
  4. +, -
  5. >, >=, <, <=
  6. ===, !==, ==, !=
  7. &&
  8. ||

When in doubt, use parentheses. (a > b) && (c < d) is clearer than a > b && c < d, even though both are parsed the same way.

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;
// Arithmetic
console.log(tourPrice * 0.9); // 10% discount
console.log(tourPrice + 20); // with equipment rental fee
// Comparison
console.log(tourPrice > 100);
console.log(tourPrice === 149);
console.log(tourPrice === '149'); // type coercion trap — try == too
// Logical
console.log(tourAvailable && tourPrice < 200);
console.log(!tourAvailable || tourPrice > 100);
// Fallback pattern
const 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.