Check if a value is a number in JavaScript and TypeScript

Written by Tom Wells on

With this code snippet, you'll be able to accurately check if a value is a number in JavaScript/TypeScript.

For JavaScript:

const isNumeric = (n) => {
    return !isNaN(parseFloat(n)) && isFinite(n)
}

and TypeScript:

const isNumeric = (n: any): boolean => {
    return !isNaN(parseFloat(n)) && isFinite(n)
}

This function will return true or false if the n value is a number or not, respectively.