requireNumber
function requireNumber(value: unknown, message: string = "Expected a number."): number;
The test is typeof value === "number", which NaN and both infinities satisfy. Use isFloat, isInteger or isNaN when those cases have to be told apart.
isX and nonX answer a question and never interrupt control flow; requireX and requireNonX hand the value back or throw. The scheme is explained in Validation conventions.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The value to validate. |
|
| The message of the exception. A default is used when omitted. |
Returns
number — the same value, typed as a number.
Throws
Exception | Condition |
|---|---|
| the value is not a number. |
Examples
What passes and what does not
requireNumber(0); // => 0
requireNumber(1.5); // => 1.5
requireNumber("1"); // throws IllegalArgumentException
requireNumber(null); // throws IllegalArgumentException
See also
Source
23 September 2026
This documentation was generated with AI (Claude) from the library's source code.