I want a concise expression to tell me if a given value is an integer or float. When provided the input value NaN
or Infinity
, I want the output value to be false, so this rules out simply checking whether typeof(val) == 'number'
Desired input / output:
1
=>true
-42
=>true
3.14
=>true
NaN
=>false
Infinity
=>false
null
=>false
undefined
=>false
isFinite()
is almost perfect, but there's one gotcha: isFinite(null)
returns true, when I actually want it to return false.
Short of always checking if ((val != nul) && isFinite(val))
, is there another more concise technique?