Lodash _.isInteger() method
What you’ll learn
- How
_.isInteger(value)mirrorsNumber.isIntegerwhile fitting lodash pipelines. - Which numeric literals pass (whole primitives, including negatives and zero).
- Why floats, special numbers, strings, and boxed numbers fail.
- When to coerce input before calling lodash versus rejecting invalid types.
Prerequisites
You know JavaScript number primitives and the difference between 3 and "3".
- You understand
typeofversus boxed wrappers (new Number(1)). - Try-it labs load lodash from the CDN.
Overview
Use _.isInteger when validating counters, IDs parsed from APIs (already numeric), pagination bounds, or config fields that must be whole numbers—without accidental string acceptance.
Primitive numbers
typeof value === "number" gates everything else first.
Whole values
Equality with _.toInteger(value) rejects fractional parts safely.
No coercion
Strings and objects fail unless you normalize beforehand.
Syntax
_.isInteger(value) - value: any value to test.
- Returns:
truewhenvalueis a finite primitive number with no fractional part; otherwisefalse.
Positive and negative integers
Ordinary whole-number literals pass.
import isInteger from "lodash/isInteger";
console.log(
"three: " + isInteger(3) + "\n" + // true
"negSeven: " + isInteger(-7) // true
); Floats, MIN_VALUE, and Infinity
Fractional math numbers and non-finite extremes fail lodash’s integer test.
import isInteger from "lodash/isInteger";
console.log(
"float: " + isInteger(3.14) + "\n" + // false
"minVal: " + isInteger(Number.MIN_VALUE) + "\n" + // false
"inf: " + isInteger(Infinity) // false
); Zero, numeric strings, and boxed numbers
0 is an integer; strings and Number objects are not accepted as-is.
import isInteger from "lodash/isInteger";
console.log(
"zero: " + isInteger(0) + "\n" + // true
"numericStr: " + isInteger("3") + "\n" + // false
"boxedNum: " + isInteger(Object(42)) // false
); 📋 _.isInteger vs related checks
| API | Behavior |
|---|---|
_.isInteger(x) | Primitive number and mathematically integral (lodash aligns with Number.isInteger). |
Number.isInteger(x) | Same classification for finite integers; no string coercion. |
_.isFinite(x) | Broader—any finite number, including 3.5. |
parseInt(str, 10) | Parses strings; combine with separate validation—do not confuse with type guards. |
Pitfalls to avoid
Query parameters
req.query.page is usually a string—call Number() (and guard NaN) before _.isInteger.
Already parsed numbers
After JSON.parse, numeric fields are already primitive numbers—lodash works on them without extra coercion.
Beyond Number precision
Very large literals may lose precision; integer detection still follows IEEE rules—use BigInt when required.
❓ FAQ
Summary
- Purpose: detect finite primitive numbers without a fractional component.
- Remember: coerce or reject strings before lodash sees them.
- Next: explore more on Lodash _.isLength().
_.isInteger is implemented as typeof value === "number" plus value == _.toInteger(value)—so values must already be numbers; coerced strings like "42" and Object(7) fail immediately.
6 people found this page helpful
