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

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:
// Standard numeric comparison
5 <= 5;       // true
10 <= 5;      // false

// Lexicographical string comparison
"apple" <= "banana"; // true (UTF-16 code unit for 'a' is less than 'b')
"10" <= "2";         // true (UTF-16 code unit for '1' is less than '2')

// Mixed types (String coerced to Number)
"10" <= 20;   // true (String "10" becomes Number 10)
"a" <= 5;     // false (String "a" becomes NaN)

// Booleans (Coerced to Number: true -> 1, false -> 0)
true <= 1;    // true
false <= 0;   // true

// Null and Undefined
// null <= 0 evaluates as !(0 < null). null coerces to 0, so 0 < 0 is false. !(false) is true.
null <= 0;      // true 
null < 0;       // false
null == 0;      // false

undefined <= 0; // false (undefined coerces to NaN)

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.
NaN <= 5;          // false
5 <= NaN;          // false
NaN <= NaN;        // false
-0 <= +0;          // true

// BigInt and Number comparison
10n <= 10;         // true 

// String and BigInt comparison (StringToBigInt preserves precision)
"9007199254740993" <= 9007199254740992n; // false
Master JavaScript with Deep Grasping Methodology!Learn More