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 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).
leftOperand < rightOperand

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:
5 < 10;       // true
10 < 10;      // false (strictly less than, not equal)
-0 < +0;      // false
String Comparison (UTF-16 Lexicographical):
"apple" < "banana"; // true ('a' is 0x0061, 'b' is 0x0062)
"Zebra" < "apple";  // true ('Z' is 0x005A, 'a' is 0x0061 - uppercase precedes lowercase)
"10" < "2";         // true ('1' is 0x0031, '2' is 0x0032)
Mixed Type Comparison (Implicit Numeric Coercion):
"10" < 20;    // true  (String "10" coerces to Number 10)
false < 1;    // true  (Boolean false coerces to Number 0)
null < 1;     // true  (null coerces to Number 0)
NaN and Undefined:
NaN < 5;        // false
5 < NaN;        // false
undefined < 5;  // false (undefined coerces to NaN)
Object Coercion:
[2] < 3;        // true  ([2] coerces to "2", then to Number 2)
[2, 3] < 4;     // false ([2, 3] coerces to "2,3", then to NaN)
Master JavaScript with Deep Grasping Methodology!Learn More