Skip to content

Function: isNumber()

isNumber(val): val is number

Type guard to check if a value is a valid number (not NaN).

Parameters

val

unknown

The value to check

Returns

val is number

True if the value is a number and not NaN

Example

typescript
const values = [42, '42', NaN, Infinity]

values.forEach(val => {
  if (isNumber(val)) {
    console.log('Valid number:', val)
  }
})
// Output: 'Valid number: 42', 'Valid number: Infinity'

Released under the MIT License.