Lodash Floor vs Bitwise Shift for Negative Numbers

While both Lodash's _.floor and JavaScript bitwise shifting can eliminate fractional values, they handle negative numbers with opposing logic. The core difference lies in the direction of rounding: _.floor performs true mathematical flooring by rounding downward toward negative infinity, whereas bitwise shift operations perform truncation by discarding the fractional part and rounding toward zero. This guide breaks down the functional differences, mechanics, and edge cases when applying both methods to negative values.

Direction of Rounding

The mathematical definition of a floor operation requires rounding down to the nearest less-than-or-equal integer. Lodash’s _.floor mirrors JavaScript's native Math.floor, moving left on the number line.

Bitwise shifts (such as the zero-fill right shift >>> 0, right shift >> 0, or left shift << 0) convert their operands into 32-bit integers. During this conversion (specifically the ECMAScript ToInt32 operation), the JavaScript engine simply drops the fractional digits, pulling negative numbers rightward toward zero.

// Using Lodash _.floor (rounds toward -Infinity)
_.floor(-5.2); // => -6
_.floor(-5.9); // => -6

// Using Bitwise Shift (truncates toward 0)
-5.2 >> 0;     // => -5
-5.9 >> 0;     // => -5

Comparison with Native Methods

To put this distinction into native JavaScript terminology:

For positive numbers, both operations produce the same result (e.g., _.floor(5.7) is 5, and 5.7 >> 0 is 5). The divergence occurs exclusively when the operand is negative.

32-Bit Integer Constraints

Another critical difference when using bitwise shifts instead of _.floor is integer overflow.

// A large negative number
const val = -3000000000.7;

_.floor(val); // => -3000000001 (correct)
val >> 0;     // => 1294967295 (overflow/wrapped to positive)

Summary of Differences