1// native returns true if the variable does NOT contain a valid number
2isNaN(num)
3
4// can be wrapped for making simple and readable
5function isNumeric(num){
6 return !isNaN(num)
7}
8
9isNumeric(123) // true
10isNumeric('123') // true
11isNumeric('1e10000') // true (This translates to Infinity, which is a number)
12isNumeric('foo') // false
13isNumeric('10px') // false
1function isNumeric(str) {
2
3 // Check if input is string
4 if (typeof str != "string")
5 return false
6
7 // Use type coercion to parse the _entirety_ of the string
8 // (`parseFloat` alone does not do this).
9 // Also ensure that the strings whitespaces fail
10 return !isNaN(str) &&
11 !isNaN(parseFloat(str))
12}