Reading history

JavaScript Operators: Arithmetic, ===, Logical and ??

4 min read 2 views

Learn JavaScript operators: arithmetic, assignment, == vs ===, logical && || !, the ternary operator, nullish coalescing ?? and precedence.

Operators are the symbols that do work on values: + adds, === compares, && combines conditions. Most behave as you would expect from maths, but JavaScript has a few of its own, including the difference between == and === and the ?? operator for default values. Both come up in nearly every program.

In this lesson you will learn

  • Arithmetic and assignment operators
  • Comparison, and why === is preferred over ==
  • Logical operators &&, || and !
  • The ternary operator ? :
  • The nullish coalescing operator ??

Arithmetic and Assignment

const a = 17, b = 5;
console.log("a + b =", a + b);
console.log("a - b =", a - b);
console.log("a * b =", a * b);
console.log("a / b =", a / b);
console.log("a % b =", a % b);   // remainder
console.log("a ** 2 =", a ** 2); // power

let count = 10;
count += 5;   // 15
count *= 2;   // 30
count--;      // 29
console.log("count =", count);

Output

a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a % b = 2
a ** 2 = 289
count = 29
  • / always gives the exact result, including decimals: 17 / 5 is 3.4. For whole-number division use Math.floor(a / b) or Math.trunc(a / b).
  • % gives the remainder. n % 2 === 0 is the usual test for an even number.
  • ** raises to a power.
  • Compound operators such as += and *= update a variable in place; ++ and -- add or subtract 1.

Comparison, Logical, Ternary and ??

console.log(5 == "5");    // true: converts types first
console.log(5 === "5");   // false: different types
console.log(0 == false);  // true
console.log(0 === false); // false
console.log(null == undefined, null === undefined);
console.log(10 > 9, "10" > "9"); // numbers vs. text comparison

const age = 20, hasId = true;
console.log("Can enter:", age >= 18 && hasId);
console.log("Discount:", age < 12 || age > 60);
console.log("Not logged in:", !true);

const marks = 72;
const result = marks >= 40 ? "Pass" : "Fail";
console.log("Result:", result);

const nickname = null;
console.log("Hello,", nickname ?? "Guest");
console.log(0 || 100, 0 ?? 100); // || treats 0 as false, ?? does not

Output

true
false
true
false
true false
true false
Can enter: true
Discount: false
Not logged in: false
Result: Pass
Hello, Guest
100 0
  • == (loose equality) converts the values to a common type before comparing, which produces results like 0 == false being true. === (strict equality) also checks the type. Use === and !== unless you have a specific reason not to.
  • Two strings are compared letter by letter, so "10" > "9" is false because the character "1" comes before "9".
  • && is true only if both sides are true; || is true if either side is; ! flips a boolean.
  • condition ? a : b picks one of two values. It is a compact replacement for a short if/else.
  • x ?? fallback uses fallback only when x is null or undefined. x || fallback also replaces 0, "" and false, which is often not what you want for numbers.

Operator Precedence

Higher runs firstOperators
1( ) grouping
2!, ++, --, typeof
3**
4*, /, %
5+, -
6<, >, <=, >=
7===, !==, ==, !=
8&&, then || and ??
9? :, then assignment =, +=…

When in doubt, add parentheses. They cost nothing and make the intent clear.

Common Mistakes

  1. Writing = instead of === in a condition. if (x = 5) assigns 5 and is always true.
  2. Relying on == and getting caught by type conversion.
  3. Using || for defaults when 0 is a valid value: quantity || 1 turns 0 into 1.
  4. Comparing numbers stored as strings, which compares them alphabetically.

Practice Exercises

Exercise 1. Given const n = 37;, print whether it is “even” or “odd” using the ternary operator.

Show answer
const n = 37;
console.log(n % 2 === 0 ? "even" : "odd"); // odd

Exercise 2. A year is a leap year if it divides by 4 but not by 100, or if it divides by 400. Write the condition.

Show answer
const year = 2024;
const isLeap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
console.log(isLeap); // true

Frequently Asked Questions

Is there an integer division operator in JavaScript?

No. Divide normally and then use Math.floor() (rounds down) or Math.trunc() (drops the decimal part).

What does the ?. operator do?

Optional chaining, a?.b, reads a property only if a is not null or undefined, and otherwise returns undefined instead of throwing an error. It is covered in the objects lesson.

Summary

  • Arithmetic: + - * / % **; compound assignment updates in place.
  • Prefer === and !== to avoid type-conversion surprises.
  • &&, ||, ! combine conditions; ? : chooses a value.
  • ?? supplies a default only for null and undefined.

Next: JavaScript if, else and switch.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *