Use Number.isFinite()
to check for a number in JavaScript
Need the best way to check for a number in JavaScript while excluding NaN or Infinity? Try Number.isFinite()
instead of typeof
.
Hi Octavian Theodor Nita, thanks for reminding me to take another look at Sumer Singh’s response — number.isFinite()
is great!
I really appreciate you reading my article and for your response to it!
Indeed, Number.isFinite()
is the most convenient way to check for a number in JavaScript, as it excludes NaN
, Infinity
, and -Infinity
:
I updated my article on checking for a number in JavaScript to recommend Number.isFinite()
and to note that there is also the global isFinite()
function, which will coerce values to numbers before the check.