Skip to main content

isNumber()

isNumber(value): value is number

Checks if a value is a number.

note

Includes NaN and Infinity. Use Number.isFinite() for finite numbers only.


Parametersโ€‹

value: unknownโ€‹

The value to check.


Returns: value is numberโ€‹

true if the value is a number, false otherwise.


See Alsoโ€‹

isFinite


Sinceโ€‹

1.0.0


Exampleโ€‹

isNumber(42);       // => true
isNumber(3.14); // => true
isNumber(NaN); // => true
isNumber(Infinity); // => true
isNumber('42'); // => false

How it works?โ€‹

Type guard that checks if a value is a number (excluding NaN).

Type Narrowingโ€‹

Common Checksโ€‹

ValueResult
123checkmark true
0checkmark true
-1.5checkmark true
Infinitycheckmark true
NaNcross false
'123'cross false
new Number(123)cross false (boxed)

Noteโ€‹

Unlike typeof x === 'number', this guard excludes NaN for safer number handling.


Use Casesโ€‹

Validate numeric inputs ๐Ÿ“Œโ€‹

Ensure a value is a number before calculations. Essential for form handling and data validation.

if (isNumber(input) && !isNaN(input)) {
const total = input + tax;
}

Filter valid numbers from mixed dataโ€‹

Extract numeric values from arrays containing mixed types.

const values = [1, "two", 3, null, 4];
const numbers = values.filter(isNumber);
// numbers: number[] = [1, 3, 4]