toInteger
function toInteger(value: unknown): number | null;
A string is read with parseInt, which takes the leading digits and stops: "12abc" gives 12. Text with no leading digits, an empty string, null and undefined all give null, so the failure is a value you can check rather than a NaN that spreads.
A number passes through untouched — including a fractional one, which is why toInteger(1.9) is 1.9 and not 1. Reach for Math.trunc or requireInteger when a whole number is what the call actually needs.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The value to read. |
Returns
number | null — the number, or null when there is none to read.
Examples
Reading a number
toInteger(" 42 "); // => 42
toInteger("12abc"); // => 12
toInteger("abc"); // => null
toInteger(null); // => null
toInteger(1.9); // => 1.9
See also
Source
23 September 2026
This documentation was generated with AI (Claude) from the library's source code.