Logical and Mathematical Operators

Operators in JavaScript are used to transform data and evaluate logic. Beyond basic arithmetic, JavaScript's operator set includes powerful tools for handling null values and managing complex conditional logic.


1. Equality: == vs ===

One of the most common pitfalls for developers is the distinction between loose and strict equality.

5 == "5";  // true (The string is coerced to a number)
5 === "5"; // false (Types are different: number vs string)

2. Arithmetic and Increment

JavaScript supports standard arithmetic operators (+, -, *, /, %) and the exponentiation operator (**).

Prefix vs Postfix Increment


3. Logical Operators and Short-Circuiting

The && (AND) and || (OR) operators use Short-Circuit Evaluation.

This behavior is frequently used for "Guard Clauses" or setting default values.


4. Modern Operators: Nullish and Optional

ES2020 introduced two operators that significantly simplify handling missing data:

const name = user?.profile?.firstName ?? "Anonymous";

Interview Pro-Tips: The + Operator Overloading

The + operator in JavaScript is Overloaded.

  1. If both operands are numbers, it performs Addition.
  2. If at least one operand is a string, it performs Concatenation, coercing the other operand to a string if necessary.

Technical Summary

  1. Coercion: Understand the rules of implicit conversion to avoid "Schrödinger's variables."
  2. Immutability: Operators like + return a new value; they do not modify the original operands.
  3. Precedence: Remember the order of operations (MDN has the definitive table).