Skip to main content
The less than (<) operator is a binary relational operator that evaluates whether the left operand is strictly smaller in mathematical or lexicographical value than the right operand, returning a boolean (true or false).

Evaluation Algorithm (Abstract Relational Comparison)

When the JavaScript engine evaluates x < y, it executes the Abstract Relational Comparison algorithm. This dictates how different data types are coerced and compared:
  1. Primitive Coercion (ToPrimitive): The engine converts both operands to primitive values. If an operand is an object, it invokes its [Symbol.toPrimitive](), valueOf(), or toString() methods, with a hint of Number.
  2. String vs. String: If both resulting primitives are strings, the operator performs a lexicographical comparison. It compares the strings character by character from left to right, based on their 16-bit unsigned integer UTF-16 code unit values.
  3. Numeric Conversion (ToNumeric): If at least one of the resulting primitives is not a string, the engine coerces both operands to numeric values (either Number or BigInt) before comparing.

Type Coercion Rules and Edge Cases

  • NaN (Not-a-Number): If either operand is NaN, or coerces to NaN (e.g., undefined, unparseable strings, or arrays with multiple elements), the operator always returns false.
  • Booleans: true coerces to 1 and false coerces to 0.
  • Nullish Values: null coerces to 0. undefined coerces to NaN.
  • Zeroes: +0 and -0 are considered mathematically equal, so -0 < +0 evaluates to false.
  • BigInt vs Number: When comparing a BigInt to a Number, JavaScript compares their exact mathematical values without implicit conversion, preventing precision loss.

Syntax Visualization

Numeric Comparison:
String Comparison (UTF-16 Lexicographical):
Mixed Type Comparison (Implicit Numeric Coercion):
NaN and Undefined:
Object Coercion:
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More