Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The === (strict equality) operator evaluates whether two operands are identical in both value and type without performing implicit type coercion. In TypeScript, it enforces compile-time type safety by rejecting comparisons between mutually exclusive types, while behaving identically to JavaScript’s strict equality at runtime.
operand1 === operand2

Compile-Time Behavior

TypeScript performs static analysis on the Left-Hand Side (LHS) and Right-Hand Side (RHS) operands. If the compiler determines that the types of the two operands have no overlap (they are mutually exclusive), it will throw a ts(2367) error. By default, TypeScript does not block code emission on type errors; it will still compile and emit the resulting JavaScript file unless the noEmitOnError flag is explicitly enabled in the tsconfig.json.
const val1: string = "5";
const val2: number = 5;

// @ts-expect-error
val1 === val2; // TypeScript Error: This condition will always return 'false' since the types 'string' and 'number' have no overlap.
If the types share an overlap (e.g., a union type compared to a primitive, or any/unknown types), the compiler permits the operation and defers to runtime evaluation.

Runtime Evaluation Mechanics

When evaluating ===, the JavaScript engine executes the following strict equality comparison algorithm:
  1. Type Comparison: If the runtime types of the LHS and RHS differ, the operator immediately returns false.
  2. Primitive Values: If both operands are primitives of the same type (string, number, boolean, bigint), it returns true if their underlying values are identical.
  3. Symbols: Although symbol is a primitive type, every symbol is uniquely instantiated. Two symbols evaluate to true if and only if they are the exact same symbol reference. Symbols created with identical descriptions (e.g., Symbol('text') === Symbol('text')) evaluate to false because they possess unique identities.
  4. Null and Undefined: null === null returns true. undefined === undefined returns true.
  5. Referential Equality (Objects): If both operands are objects (including arrays and functions), the operator evaluates to true if and only if both operands reference the exact same memory location. It does not perform deep value comparison.
// Primitive evaluation
const str1: string = "text";
const str2: string = "text";
str1 === str2; // true

// Symbol evaluation
const sym1: symbol = Symbol("id");
const sym2: symbol = Symbol("id");
sym1 === sym2; // false (distinct identities)
sym1 === sym1; // true (identical reference)

// Referential equality evaluation
const objA = { id: 1 };
const objB = { id: 1 };
const objC = objA;

objA === objB; // false (distinct memory addresses)
objA === objC; // true (shared memory address)

IEEE 754 Exceptions

The strict equality operator adheres to the IEEE 754 standard for floating-point arithmetic, resulting in two notable mechanical exceptions regarding numbers:
  • NaN (Not-a-Number): NaN is never strictly equal to anything, including itself. NaN === NaN evaluates to false.
  • Signed Zeros: Positive zero and negative zero are considered strictly equal. +0 === -0 evaluates to true.
Master TypeScript with Deep Grasping Methodology!Learn More