Lodash _.lte Type Coercion: Boolean vs Number

When the Lodash library executes _.lte to compare a boolean against a number, it coerces the boolean operand into a numeric value before evaluating the relational condition. Internally, Lodash converts any non-string operands to numbers using JavaScript's unary plus (+) operator, causing true to become 1 and false to become 0. Once both values are converted to numeric primitives, _.lte performs standard numerical less-than-or-equal comparison (<=).

Lodash's Relational Operation Mechanism

Lodash implements relational methods like _.lte, _.lt, _.gte, and _.gt using an internal helper function called createRelationalOperation. This helper checks the types of both arguments before performing the comparison:

function createRelationalOperation(operator) {
  return function(value, other) {
    if (!(typeof value == 'string' && typeof other == 'string')) {
      value = +value;
      other = +other;
    }
    return operator(value, other);
  };
}

Because a boolean and a number do not satisfy the condition of both being strings, Lodash executes the else branch, applying +value and +other.

The Coercion Rules Applied

The unary + operator invokes ECMAScript's internal ToNumber abstract operation on each operand:

  1. Boolean Conversion:
    • true evaluates to 1
    • false evaluates to 0
  2. Number Conversion:
    • Numeric values remain unchanged (e.g., +5 evaluates to 5).

After coercion, Lodash evaluates the comparison using the native <= operator on the resulting numbers.

Practical Examples

Comparing true to Numbers

Since true coerces to 1:

Comparing false to Numbers

Since false coerces to 0:

Reversing Argument Order

The position of the operands determines which value is compared against which, but the coercion rules remain identical:

Behavior with NaN

If the number argument is NaN, coercion of NaN yields NaN. In JavaScript, any relational comparison involving NaN (such as 1 <= NaN or NaN <= 1) returns false. Consequently, both _.lte(true, NaN) and _.lte(false, NaN) will return false.