Skip to main content
The less than or equal to operator (<=) is a binary relational operator that evaluates whether the left operand is less than or equal to the right operand. Internally, JavaScript evaluates x <= y by performing the Abstract Relational Comparison y < x and negating the boolean result (!(y < x)). If the underlying comparison evaluates to undefined (which occurs when either operand coerces to NaN), the <= operator explicitly returns false.

Evaluation Mechanics

When the JavaScript engine evaluates the <= operator, it executes the Abstract Relational Comparison algorithm. The process follows strict type coercion rules:
  1. ToPrimitive Conversion: Both operands are converted to primitive values. If an operand is an object, the engine first attempts to call its [Symbol.toPrimitive]() method. If this method is absent, it falls back to calling valueOf() and toString() in sequence until a primitive is yielded.
  2. String Comparison: If both resulting primitives are strings, they are compared lexicographically based on their sequence of 16-bit unsigned integer values (UTF-16 code units).
  3. String and BigInt Comparison: If one primitive is a String and the other is a BigInt, the string is converted directly to a BigInt via the StringToBigInt algorithm. This prevents the precision loss that would occur if a large numeric string were coerced to a standard Number.
  4. Numeric Comparison: For all other type combinations where at least one primitive is not a string, both operands are coerced to numeric values (via the ToNumeric algorithm) and compared mathematically.

Type Coercion Behavior

Because of implicit type coercion and the inverse evaluation model (!(y < x)), the operator exhibits specific behaviors depending on the operand types:

Special Numeric Values

The operator handles IEEE 754 special numeric values and BigInts according to specific rules:
  • NaN: Any relational comparison involving NaN evaluates to undefined internally, causing the <= operator to return false. This includes NaN <= NaN.
  • Zero: +0 and -0 are considered mathematically equal.
  • Infinity: +Infinity is greater than or equal to any number. -Infinity is less than or equal to any number.
  • BigInt: BigInt and Number types can be compared directly without throwing a TypeError.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More