Number.isFinite()
is the best way to check for a number in JavaScript
Need a simple way to check for a number in JavaScript, but don’t want NaN
or Infinity
? Use Number.isFinite()
instead of typeof
.
Hi Sumer Singh, thanks for taking the time to read my article and for your insightful response!
You are exactly right, Number.isFinite()
is the easiest way to check for a finite number, as it excludes NaN
, Infinity
, and -Infinity
:
I clarified my article on checking for a JavaScript number to recommend Number.isFinite()
and to point out that there is the global isFinite()
function, which does perform type coercion if that behavior is desired:
Thanks again for supporting my blog and sharing your insight!
It is much appreciated!!! 😁