CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isNumber

Checks whether a value is a number primitive.

isNumber(value):value is number

Behavior

The function checks the value using typeof and returns true when JavaScript classifies the value as number. This includes integers, floating-point numbers, Infinity, -Infinity, and NaN.

Type Guard

The return type is value is number, allowing TypeScript to narrow the value to number inside a conditional branch.

Special Number Values

NaN and Infinity are both considered numbers by typeof, so isNumber returns true for them.

Parameters

valueunknownrequired

The value to check.

Returns

value is number

Returns true when the value has the JavaScript number type; otherwise, returns false.

Examples

01

Basic usage

Check whether different values are numbers.

TypeScript
import { isNumber } from "@cyref/js";

isNumber(42);
// true

isNumber(3.14);
// true

isNumber(NaN);
// true

isNumber("42");
// false

isNumber(null);
// false
02

Type narrowing

isNumber acts as a TypeScript type guard and narrows the value to number when it returns true.

TypeScript
const value: string | number = 42;

if (isNumber(value)) {
  // value is number
  console.log(value.toFixed(2));
}
Built with@cyref/js·Official documentation